Thread: How to use GetPrivateProfileString function ?

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    20

    Question How to use GetPrivateProfileString function ?

    Code:
    char* pResult;
    GetPrivateProfileString(m_strSectionName, m_strKeyName, "", pResult, 255, m_strFileName);
    When I build the application, there're no errors or warnings, but it crashes when I run it. I deleted the GetPrivateProfileString function call and then it works well. So there must be something wrong with my function call but I can't make out what it is.

    Please help me !

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You didn't allocate any space for pResult.

    I assume 255 is the maximum length of the resulting string (I'm not familiar with GetPrivateProfileString()). If so do this:
    Code:
    char pResult[255]; // maybe 256 if GetPrivateProfileString expects you to make room for '\0'
    GetPrivateProfileString(m_strSectionName, m_strKeyName, "", pResult, 255, m_strFileName);
    or this:
    Code:
    char* pResult = new char[255];
    GetPrivateProfileString(m_strSectionName, m_strKeyName, "", pResult, 255, m_strFileName);
    // use pResult ...
    delete [] pResult;

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are passing it an empty buffer to copy data to, and then you are telling the function the buffer is actually 255 bytes in size. Try the following:

    Code:
    char Result[255];
    GetPrivateProfileString(m_strSectionName, m_strKeyName, NULL, pResult, 255, m_strFileName);
    EDIT: jlou beat me to it.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    20
    I've tried, but it still crashes !

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Try passing NULL instead of "" for the third parameter.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    MSDN says that parameter cannot be NULL.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No it doesnt.

    lpDefault
    [in] Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. If this parameter is NULL, the default is an empty string, "".
    Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.

    Windows Me/98/95: Although lpDefault is declared as a constant parameter, the system strips any trailing blanks by inserting a null character into the lpDefault string before copying it to the lpReturnedString buffer.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    20
    When the section name and the key name doesn't exist, the third parameter will be the returned value. I'm afraid that this is not the problem.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Hmmm, it does in mine (Oct 2001):
    lpDefault
    [in] Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be NULL.
    Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.




    skywing, Make sure all the strings (m_str...) are valid, and after that I don't know, you'll just have to debug it. By the way, you said it still crashes, but you didn't say which option you tried.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    20
    Before the function call , I used a MessageBox to show the strings in the three inputs and they were right.

    I think the problem may be the size of the buffer to which my pResult points. 255 may be a wrong number ??

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's odd jlou. I was looking at the online version of MSDN which (I hope anyways) is probably more up to date. Strange that they would go from NULL being invalid to NULL being the default string.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    20
    Are there any examples of using this function ?

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    skywing:
    Post a small example which demonstrates the problem you are having.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    20
    OK, assume that I have a file like this
    [Header]
    Font=Times New Roman
    Size=12
    Text="Welcome!"

    [Footer]
    Font=Times New Roman
    Size=12
    Text="2004"

    [Text1]
    Font=Tahoma
    Size=12
    Text="This is the 1st slide"

    [Text2]
    Font=Tahoma
    Size=12
    Text="This is the 2nd slide"

    .....
    I have to read the information in this file and make a slideshow (like PowerPoint) in which each slide has a header, a footer and some text. I think this file structure is somehow like an INI file so I decided to use GetPrivateProfileString function.

  15. #15
    Registered User
    Join Date
    Apr 2004
    Posts
    20
    By the way, I've tried to call this function in VB and it works well. But when I call it in VC++ 6.0, it crashes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM