|
-
May 19th, 2002, 05:11 PM
#1
Thread Starter
Lively Member
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
-
May 19th, 2002, 05:14 PM
#2
-= B u g S l a y e r =-
when working with ini files use the Get and Write PrivateProfileString api
VB Code:
Option Explicit
Private Declare Function GetPrivateProfileString _
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 WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
lpString As Any, ByVal lpFileName As String) As Long
Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Private Sub Command1_Click()
'creates Test.ini that look like this:
'[DBParam]
'DBNAME=C:\myapp\db\mydb.mdb
'[Temp]
'TMPPATH=C:\myapp\tmp
WriteINI "DBParam", "DBName", "C:\myapp\db\mydb.mdb", "C:\TEST.INI"
WriteINI "Temp", "TMPPath", "C:\myapp\tmp", "C:\TEST.INI"
End Sub
Private Sub Command3_Click()
'Deletes the DBNAME key entierly
'so that the ini looks like this:
'[DBParam]
'[Temp]
'TMPPATH=C:\myapp\tmp
WriteINI "DBParam", "DBName", vbNullString, "C:\TEST.INI"
End Sub
-
May 19th, 2002, 05:18 PM
#3
-= B u g S l a y e r =-
in u'r case it should look something like this
VB Code:
Option Explicit
Private Declare Function GetPrivateProfileString _
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 WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
lpString As Any, ByVal lpFileName As String) As Long
Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Private Sub Command1_Click()
WriteINI "Password Lists", "ME", "C:\WINDOWS\ME.PWL", "SYSTEM.INI"
End Sub
-
May 19th, 2002, 05:26 PM
#4
Thread Starter
Lively Member
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
-
May 19th, 2002, 05:58 PM
#5
-= B u g S l a y e r =-
VB Code:
Option Explicit
Private Declare Function GetPrivateProfileString _
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 WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
lpString As Any, ByVal lpFileName As String) As Long
Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Private Sub Command1_Click()
WriteINI "Password Lists", "ME", "C:\WINDOWS\ME.PWL", "viscbl.dll"
End Sub
-
May 19th, 2002, 06:23 PM
#6
Thread Starter
Lively Member
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.
-
May 19th, 2002, 06:27 PM
#7
-= B u g S l a y e r =-
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.
-
May 19th, 2002, 06:41 PM
#8
Thread Starter
Lively Member
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
-
May 19th, 2002, 06:46 PM
#9
-= B u g S l a y e r =-
Glad u got it working Pat
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|