CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Aug 2013
    Posts
    52

    Unhappy GetPrivateProfileString Not working

    I am trying to use ini file for configuring my app.
    But GetPrivateProfileString is not working.
    I am using Windows 7. I tried "Wow64DisableWow64FsRedirection" also.

    Not only this-ATLPath::FileExists is also not working.It gives an error code 0x80070002 which means:
    "system could not find the file"

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: GetPrivateProfileString Not working

    But GetPrivateProfileString is not working.
    How is it not working? How are you using it? It would be helpful if you posted the code in which it is used.

    Note that according to Microsoft, "GetPrivateProfileString is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry."
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: GetPrivateProfileString Not working

    1. As stated by MSFT more than 1.5 decade back
      This function is provided only for compatibility with 16-bit versions of Windows. Win32-based applications should store initialization information in the registry.
      Why don't you want to use registry?
    2. What exactly "is not working"? What does it return? Did you read the MDN documentation about it?
    3. WHat ATLPath::FileExists do you mean and how do you use it? MSDN shows the following signature for it:
      Code:
      inline BOOL FileExists(
         const char* pszPath 
      );
      inline BOOL FileExists(
         const wchar_t* pszPath 
      );


    //Edit: Ohh, 2kaud was faster!
    Last edited by VictorN; August 23rd, 2013 at 09:24 AM.
    Victor Nijegorodov

  4. #4
    Join Date
    Aug 2013
    Posts
    52

    Re: GetPrivateProfileString Not working

    OK, Now I wrote code for writing to registry. But what if in the target machine user doesn't have enough previlages?
    I hope usually all users will have access to HKEY_LOCAL_MACHINE\SOFTWARE. Is it OK?

    ATLPath::FileExists() returns FALSE even though I gave an existing path as paramater. Why?
    I think the problem is that platform is 64 bit. Please try the code in windows 7 and tell me whether it works!

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: GetPrivateProfileString Not working

    Quote Originally Posted by shjdr7 View Post
    OK, Now I wrote code for writing to registry. But what if in the target machine user doesn't have enough previlages?
    I hope usually all users will have access to HKEY_LOCAL_MACHINE\SOFTWARE. Is it OK?
    It is OK as long as user reads the registry. User cannot write in HKEY_LOCAL_MACHINE.

    Quote Originally Posted by shjdr7 View Post
    ATLPath::FileExists() returns FALSE even though I gave an existing path as paramater. Why?
    I think the problem is that platform is 64 bit. Please try the code in windows 7 and tell me whether it works!
    Where is your code?
    Victor Nijegorodov

  6. #6
    Join Date
    Aug 2013
    Posts
    52

    Re: GetPrivateProfileString Not working

    I will give the code later. If user cannot write to registry how he will configure the application? Which registry key I should use?

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: GetPrivateProfileString Not working

    User usually saves application settings in HKCU (HKEY_CURENT_USER\Software\<Company name>\<application name>)
    In HKEY_LOCAL_MACHINE you could save some initial data/settings (while installing your app!) that are common to all the users supposed to use your app. And those data/settings will never been changed.
    Victor Nijegorodov

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: GetPrivateProfileString Not working

    Quote Originally Posted by VictorN View Post

    //Edit: Ohh, 2kaud was faster!
    It's not race!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: GetPrivateProfileString Not working

    I have some programs that use it. It works. You'll need to post code and be more specific about what's not working.

  10. #10
    Join Date
    Aug 2013
    Posts
    52

    Re: GetPrivateProfileString Not working

    //Try this code in windows 7 with 64 bit
    TCHAR strFileName[MAX_PATH] = {0};
    ::GetModuleFileName(ModuleHelper::GetModuleInstance(),strFileName,MAX_PATH);
    ATLPath::RemoveFileSpec(strFileName);
    ATLPath::Append(strFileName,_T("Config.INI"));

    ATLTRACE(strFileName);//File exists I tested the trace output
    ATLTRACE(_T("\n"));


    if(!ATLPath::FileExists(strFileName))
    {
    HRESULT hr = ATL::AtlHresultFromLastError();
    ATLTRACE("%x\n",hr);//system could not find the file specified!
    return;
    }

  11. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: GetPrivateProfileString Not working

    I've tested your code and it works fine (Windows 7, 64-bit).
    Are you sure you have double-checked the existence and the path of that file?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  12. #12
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: GetPrivateProfileString Not working

    Anyway, placing that .INI file in the application's folder is not a good idea.
    Instead, put it under a special folder dedicated for that purpose (would be Application Data), then call SHGetKnownFolderPath or the older SHGetFolderPath.

    For a list of special folders, have a look in MSDN Library: http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  13. #13
    Join Date
    Aug 2013
    Posts
    52

    Re: GetPrivateProfileString Not working

    Anyway I am using registry now. I hope using HKEY_CURENT_USER will be safe - User in target machine will have privilages to write in the registry

  14. #14
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: GetPrivateProfileString Not working

    Yes.
    Victor Nijegorodov

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: GetPrivateProfileString Not working

    Quote Originally Posted by shjdr7 View Post
    I am trying to use ini file for configuring my app.
    But GetPrivateProfileString is not working.
    I am using Windows 7. I tried "Wow64DisableWow64FsRedirection" also.

    Not only this-ATLPath::FileExists is also not working.It gives an error code 0x80070002 which means:
    "system could not find the file"
    The most frequent beginner's mistake is to not provide a fully specified path to .ini file.

    From MSDN:
    lpFileName [in]

    The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
    Sample: reading_ini_string.zip

    Config.ini:
    Code:
    [Settings]
    Value1=Test string
    Test run:
    Code:
    J:\Temp\15>15
    Value1 = Test string
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured