Results 1 to 9 of 9

Thread: Editing a file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    SF
    Posts
    93

    Question Editing a file

    Hi All,

    I've learned how to append a file. My problem is that I need to add a line to the middle of the file.

    example:

    Im trying to add the line ME=C:\WINDOWS\ME.PWL

    to the [Password Lists] section within system.ini


    I'm pretty sure that I need to read the system.ini file into a variable then use code to find the [Password Lists] section and then add the line ME=C:\WINDOWS\ME.PWL into it. Then close the file.

    Can someone please post the basic code/syntax that's needed.

    Thank you,

    Pat

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    when working with ini files use the Get and Write PrivateProfileString api

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetPrivateProfileString _
    3. Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal _
    4. lpApplicationName As String, ByVal lpKeyName As String, _
    5. ByVal lpDefault As String, ByVal lpReturnedString As _
    6. String, ByVal nSize As Long, ByVal lpFileName As String) As _
    7. Long
    8.  
    9. Private Declare Function WritePrivateProfileString _
    10. Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
    11. lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
    12. lpString As Any, ByVal lpFileName As String) As Long
    13.  
    14.  
    15.  
    16. Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
    17.    Dim strbuffer As String
    18.    Let strbuffer$ = String$(750, Chr$(0&))
    19.    Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    20. End Function
    21.  
    22. Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
    23.     Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
    24. End Sub
    25.  
    26. Private Sub Command1_Click()
    27.     'creates Test.ini that look like this:
    28.     '[DBParam]
    29.     'DBNAME=C:\myapp\db\mydb.mdb
    30.     '[Temp]
    31.     'TMPPATH=C:\myapp\tmp
    32.     WriteINI "DBParam", "DBName", "C:\myapp\db\mydb.mdb", "C:\TEST.INI"
    33.     WriteINI "Temp", "TMPPath", "C:\myapp\tmp", "C:\TEST.INI"
    34. End Sub
    35.  
    36. Private Sub Command3_Click()
    37.     'Deletes the DBNAME key entierly
    38.     'so that the ini looks like this:
    39.     '[DBParam]
    40.     '[Temp]
    41.     'TMPPATH=C:\myapp\tmp
    42.     WriteINI "DBParam", "DBName", vbNullString, "C:\TEST.INI"
    43. End Sub
    -= a peet post =-

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    in u'r case it should look something like this

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetPrivateProfileString _
    4. Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal _
    5. lpApplicationName As String, ByVal lpKeyName As String, _
    6. ByVal lpDefault As String, ByVal lpReturnedString As _
    7. String, ByVal nSize As Long, ByVal lpFileName As String) As _
    8. Long
    9.  
    10. Private Declare Function WritePrivateProfileString _
    11. Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
    12. lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
    13. lpString As Any, ByVal lpFileName As String) As Long
    14.  
    15.  
    16.  
    17. Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
    18.    Dim strbuffer As String
    19.    Let strbuffer$ = String$(750, Chr$(0&))
    20.    Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    21. End Function
    22.  
    23. Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
    24.     Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
    25. End Sub
    26.  
    27. Private Sub Command1_Click()
    28.     WriteINI "Password Lists", "ME", "C:\WINDOWS\ME.PWL", "SYSTEM.INI"
    29. End Sub
    -= a peet post =-

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    SF
    Posts
    93
    Thanks for responding Peet! I'm sorry, I should have gave the correct picture. It's actually a .dll file (what Im doing is replacing system.ini with a (copied system.ini file that has been renamed to viscbl.dll)

    so I need to open the viscbl.dll file add the line under the [Password Lists] section and then save the viscbl.dll file. The rest of the code then replaces the original system.ini file by deleting it and then renaming viscbl.dll to system.ini.

    Pat

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetPrivateProfileString _
    4. Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal _
    5. lpApplicationName As String, ByVal lpKeyName As String, _
    6. ByVal lpDefault As String, ByVal lpReturnedString As _
    7. String, ByVal nSize As Long, ByVal lpFileName As String) As _
    8. Long
    9.  
    10. Private Declare Function WritePrivateProfileString _
    11. Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
    12. lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
    13. lpString As Any, ByVal lpFileName As String) As Long
    14.  
    15.  
    16.  
    17. Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
    18.    Dim strbuffer As String
    19.    Let strbuffer$ = String$(750, Chr$(0&))
    20.    Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    21. End Function
    22.  
    23. Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
    24.     Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
    25. End Sub
    26.  
    27. Private Sub Command1_Click()
    28.     WriteINI "Password Lists", "ME", "C:\WINDOWS\ME.PWL", "viscbl.dll"
    29. End Sub
    -= a peet post =-

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    SF
    Posts
    93
    Here's what's up. Im creating a security program for 9x.

    I can't lock system.ini during a windows session because DUN looks to the file when it auto connects for a dialup session. It reads the [Password Lists] section for an existing username (to verify the existing username) and then autoconnects. If I lock system.ini then windows can't read the system.ini file and then the user has to type in the dun password everytime he/she wants to connect.





    so what I doing is copying system.ini and renaming it to the .dll

    problem is.. when the admin creates a new user profile (through my program the system.ini file is updated correctly in the password list section but then I copy the "new" system.ini file to replace the old .dll file the new user .pwl entry is not in the new .dll file. I think windows is still holding the original system.ini in memory. So I want to directly open the .dll file and add the entry directly.

    when the user then logs off or the pc is restarted my program deletes the current system.ini file and replaces it with the .dll file (which is renamed to system.ini)


    This is why I have to go directly to the .dll file.

    I've been running around in circles for about 3 weeks now trying to figure this out.

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    techsent, did u try my last post ?

    I replaced the name of the file with the name of the dll file ... it should work.

    If it does not, let me know what is going wrong.
    -= a peet post =-

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    SF
    Posts
    93
    IT WORKS PERFECTLY!!!!!!!!!!!!!! THANK YOU PEET!!

    You removed the final road block.

    Now I can finish the help file and get this program out (I must tell you that Ive been coding this thing for over a year now).

    As soon as I have the installer done I'll send off a copy to you.

    Thanks again,

    Pat

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Glad u got it working Pat
    -= a peet post =-

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