CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Question with GetPrivateProfileString

    I use it to retrieve value from .ini file but when on Form_load it reads the value, then it delete it so basically all the data from .ini file will be lost if I'll open then close my program and a second later open it again.. I read from INI when form loads and write to it when for unloads, but how can I prevent data (that was read from ini) from being deleted, I just want to read from INI and leave INI alone, but on form unload it will just overwrite existed value, without losing any data from INI

    Thank You


  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Question with GetPrivateProfileString

    GetPrivateProfileString doesn't delete value that it reads, you must be resetting/passing blank values to the WritePrivateProfileString API, check the Values you are passing to this API in your Forms Unload Event.

    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  3. #3
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: Question with GetPrivateProfileString

    But how can I fix the code so data inside .ini will remain after GetPrivateProfileString was used


  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Question with GetPrivateProfileString

    Like Aaron said, the GetPrivateProfileString doesn't remove items from your ini file - Are you sure that something else isn't removing them (Notepad etc ?)

    Try it with this simple form :


    option Explicit
    '
    private Declare Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringA" (byval lpApplicationName as string, _
    byval lpKeyName as Any, byval lpDefault as string, _
    byval lpReturnedString as string, byval nSize as Long, _
    byval lpFileName as string) as Long
    '
    private Sub Form_Load()
    Dim sBuffer as string
    Dim lRet as Long
    Dim lSize as Long
    '
    lSize = 255
    sBuffer = string$(lSize, vbNullChar)
    lRet = GetPrivateProfileString("me", "TEST", "empty !", sBuffer, _
    lSize, "c:\test.ini")
    If lRet > 0 then MsgBox Left$(sBuffer, lRet)
    '
    End Sub




    And this ini file in c:\test.ini :

    [ME]
    TEST=Chris Was Here

    - run the program and then close the form - you should see that the ini file hasn't been changed once the program has finished.


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

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