Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetPrivateProfileString() Problem

GetPrivateProfileString() Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpvisual-studiowindows-adminjsonquestion
5 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Justin Cooke
    wrote on last edited by
    #1

    I'm having a really hard time getting this function to properly find a file. I know I'm supposed to use the registry for app initialization info, but I've got an old app and DLL that use an INI file and I need to continue supporting it. I'm just trying to read from a file called "api.ini" in the same directory as the application. Of course, that directory is not known at compile time. I determine the directory using GetModuleFileName() and append api.ini to the full path. Then I convert all '\' chars to '\\' since that is how the func needs it. This never works! If I put the api.ini file in the WINNT directory, and only pass "api.ini" as the last param to GetPrivateProfileString(), it properly finds the file. If I hard code the full path ("c:\\program files\\test\\api.ini"), the function finds the file IF the application is run from a different directory (the VS 6.0 dev environment.) However, if I place the app in the c:\program files\test\ directory, then it fails and can't find the file. Does anybody know why this is not working for me? It seems so simple...I assume I'm missing something really easy. I should be able to just pass the full path to the file and have it read properly. By the way, I have verified that the path I pass to GetPrivateProfileString() is the correct, full path. Any help would be greatly appreciated. :confused:

    A J N 3 Replies Last reply
    0
    • J Justin Cooke

      I'm having a really hard time getting this function to properly find a file. I know I'm supposed to use the registry for app initialization info, but I've got an old app and DLL that use an INI file and I need to continue supporting it. I'm just trying to read from a file called "api.ini" in the same directory as the application. Of course, that directory is not known at compile time. I determine the directory using GetModuleFileName() and append api.ini to the full path. Then I convert all '\' chars to '\\' since that is how the func needs it. This never works! If I put the api.ini file in the WINNT directory, and only pass "api.ini" as the last param to GetPrivateProfileString(), it properly finds the file. If I hard code the full path ("c:\\program files\\test\\api.ini"), the function finds the file IF the application is run from a different directory (the VS 6.0 dev environment.) However, if I place the app in the c:\program files\test\ directory, then it fails and can't find the file. Does anybody know why this is not working for me? It seems so simple...I assume I'm missing something really easy. I should be able to just pass the full path to the file and have it read properly. By the way, I have verified that the path I pass to GetPrivateProfileString() is the correct, full path. Any help would be greatly appreciated. :confused:

      A Offline
      A Offline
      Anders Molin
      wrote on last edited by
      #2

      Justin Cooke wrote: Then I convert all '\' chars to '\\' since that is how the func needs it. You don't need to convert \ to \\, it's only for hardcoded strings you do that. Try ´not to, and see if it don't help... - Anders Money talks, but all mine ever says is "Goodbye!"

      J 1 Reply Last reply
      0
      • A Anders Molin

        Justin Cooke wrote: Then I convert all '\' chars to '\\' since that is how the func needs it. You don't need to convert \ to \\, it's only for hardcoded strings you do that. Try ´not to, and see if it don't help... - Anders Money talks, but all mine ever says is "Goodbye!"

        J Offline
        J Offline
        Justin Cooke
        wrote on last edited by
        #3

        Thanks for the suggestion, but it didn't seem to help. It's very odd, now that I'm not modifying the string at all (leaving the single slashes alone), I'm passing the same file path string to GetPrivateProfileString() that I pass to CStdioFIle::Open(). The file opens properly, but GetPrivateProfileString() can't find it (it returns the default value.) Does anyone have any other ideas? Could it have something to do with the fact that this is MFC? Thanks.

        1 Reply Last reply
        0
        • J Justin Cooke

          I'm having a really hard time getting this function to properly find a file. I know I'm supposed to use the registry for app initialization info, but I've got an old app and DLL that use an INI file and I need to continue supporting it. I'm just trying to read from a file called "api.ini" in the same directory as the application. Of course, that directory is not known at compile time. I determine the directory using GetModuleFileName() and append api.ini to the full path. Then I convert all '\' chars to '\\' since that is how the func needs it. This never works! If I put the api.ini file in the WINNT directory, and only pass "api.ini" as the last param to GetPrivateProfileString(), it properly finds the file. If I hard code the full path ("c:\\program files\\test\\api.ini"), the function finds the file IF the application is run from a different directory (the VS 6.0 dev environment.) However, if I place the app in the c:\program files\test\ directory, then it fails and can't find the file. Does anybody know why this is not working for me? It seems so simple...I assume I'm missing something really easy. I should be able to just pass the full path to the file and have it read properly. By the way, I have verified that the path I pass to GetPrivateProfileString() is the correct, full path. Any help would be greatly appreciated. :confused:

          J Offline
          J Offline
          Jeremy Falcon
          wrote on last edited by
          #4

          Have you debugged the app to see what GetModuleFileName() is returning? It should retrun a fully qualified path (i.e., the filename is included). You have to remove the filename before appending another filename.

          char szBuffer[MAX_PATH] = {0};
          GetModuleFileName(hInstance, szBuffer, sizeof(szBuffer));

          Something like the above will give you C:\My Path\myfile.exe. You'll have to drop myfile.exe before you add the other filename or else you'll get C:\My Path\myfile.exe\api.ini. Oh, and like it was stated, you don't need to escape the backslash unless it's a string literal (i.e., you're typing it in the code directly). Jeremy Falcon "so be it, threaten no more, to secure peace is to prepare for war" - Metallica

          1 Reply Last reply
          0
          • J Justin Cooke

            I'm having a really hard time getting this function to properly find a file. I know I'm supposed to use the registry for app initialization info, but I've got an old app and DLL that use an INI file and I need to continue supporting it. I'm just trying to read from a file called "api.ini" in the same directory as the application. Of course, that directory is not known at compile time. I determine the directory using GetModuleFileName() and append api.ini to the full path. Then I convert all '\' chars to '\\' since that is how the func needs it. This never works! If I put the api.ini file in the WINNT directory, and only pass "api.ini" as the last param to GetPrivateProfileString(), it properly finds the file. If I hard code the full path ("c:\\program files\\test\\api.ini"), the function finds the file IF the application is run from a different directory (the VS 6.0 dev environment.) However, if I place the app in the c:\program files\test\ directory, then it fails and can't find the file. Does anybody know why this is not working for me? It seems so simple...I assume I'm missing something really easy. I should be able to just pass the full path to the file and have it read properly. By the way, I have verified that the path I pass to GetPrivateProfileString() is the correct, full path. Any help would be greatly appreciated. :confused:

            N Offline
            N Offline
            Navin
            wrote on last edited by
            #5

            I think the two suggetions mentioned will work for you. That being said, I feel it is my duty to point out one thing: GetPrivateProfileString is EVIL. If you ever run on a 9X box and your INI file gets to be bigger than 64K, it will mysteriously fail, and you will spend a lot of time tracking it down. I'd personally recommend using an INI clas (I'm sure there's one here on CP - or you can write your own), or write some simple routines to parse a text file and get the info you need. Yes, it IS more work to do it that way, but you have more control, and aren't subject to 64K limitations. :) "When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups