Results 1 to 6 of 6

Thread: WritePrivateProfileString, WritePrivateProfileSection, GetPrivateProfileString, Get..

  1. #1
    Guest
    Can these functions be used on Non-Ini files??
    I store a lot of my information for programs in some unrecognizable format(well it is Text inside, but with an extension of something like .zxy) so it will be harder for computer illiterate people to modify and possibly screw up the program.
    I usually store in this format

    Code:
    [sect]
    text
    [sect2]
    text
    I dont really want to use an INI File, but I really like using this format, so far I have been using InStr and Mid, I really dont like doing this. could I use these to write/read sections from any file??
    Code:
    Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Public 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
    Also, how do I do basic file operations in API?
    like read/write to a file? like these, but in API

    Code:
    Open FileNamePath For Output As FileNum
    Print #FileNum, "MyString Thing"
    Close FileNum
    
    Open FileNamePath For Input As FileNum
    Text1 = Input(LOF(FileNum), FileNum)
    Close FileNum

    ???

    Thanks,
    Dennis

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    First - yes, you can use the INI functions on any compatible file.

    Second - check out the CreateFile function, which returns an API handle. You can use this with ReadFile and WriteFile. I'll post an example in a bit.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    Thanks a lot!
    how do you read a file without using CreateFile first?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't. CreateFile acts like the CreateRegKey function. If you specify the correct options (documented on MSDN), then you can set whether it will create the file if it doesn't already exist. If you set it to never create, it will act like a hypothetical OpenFile function.

    Remember to use CloseFile...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Try this one dennis. Yes, you can write it to any file type.

    Code:
    Option Explicit
    
    Private Declare Function OSGetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Private Declare Function OSWritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    
    Private Sub CmdGet_Click()
    'PURPOSE: Get data from your text files
              'Text1.Text = The name of the bracket section
              'Text2.Text = The name of the field
              'An error message
              'Where to assign the answer to
              'The lengh of the answer
              'The location of the text file
       
      Dim str_Data As String * 50
      Call OSGetPrivateProfileString(Text1.Text, Text2.Text, "There is an error!", str_Data, Len(str_Data), CurDir & "\winzip32.ini")
      lblAnswer.Caption = str_Data
    End Sub
    
    Private Sub CmdWrite_Click()
    'PURPOSE: Write to your text file
              'Text4.Text = The name of the bracket section
              'Text5.Text = The name of the field
              'Text6.Text = The new value for that field
              'The location of the text file
             
      If Text1.Text <> "" Then
        Call OSWritePrivateProfileString(Text4.Text, Text5.Text, Text6.Text, CurDir & "\winzip32.ini")
      End If
      
      Text6.Text = ""
      lblAnswer.Caption = ""
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  6. #6
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    BTW... What is the difference between WritePrivateProfileSection and WritePrivateProfileString?

    Hummmm...... I will test it out!
    Chemically Formulated As:
    Dr. Nitro

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