Writing to the Registry via API -- HELP! 
Author Message
 Writing to the Registry via API -- HELP!

Take the '* 128' off those string declarations?
Quote:

> Need some help writing to the registry.  I have successfully read from, but
> when I write to the registry the data gets filled with 128 whitespaces plus
> the text I apply.  What is wrong ?
> Included is my source-

> -----------------------------------
> Dim strval As String * 128
>  Dim strval1 As String * 128
>  Dim strval2 As String * 128
>  Dim strvals1 As String * 128
>  Private Const REG_SZ = 1
>  Private Const HKEY_LOCAL_MACHINE = &H80000002
>  Private Const MAX_FILENAME_LEN = 256
>  Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"
> (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
>  Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
> "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal
> lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
>  ' Note that if you declare the lpData parameter as String, you must pass it
> By Value.
>  Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As
> Long) As Long
>  Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias
> "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal
> Reserved As Long, ByVal dwType As Long, ByVal lpData As Any, ByVal cbData As
> Long) As Long
>  Private Declare Function GetVolumeInformation& Lib "kernel32" _
>     Alias "GetVolumeInformationA" _
>    (ByVal lpRootPathName As String, ByVal pVolumeNameBuffer As String, _
>     ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, _
>     lpMaximumComponentLength As Long, lpFileSystemFlags As Long, _
>     ByVal lpFileSystemNameBuffer As String, _
>     ByVal nFileSystemNameSize As Long)

> Private Sub cmdCancel_Click()
> End
> End Sub

> Private Sub cmdGetCurrent_Click()
> Call GetCurrent
> Call GetSerialNumber("C")
> End Sub

> Public Sub SetRegistry()
> Dim chk As String
> strvals1 = txtNameChange.Text
> ress = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows
> NT\CurrentVersion", Handle)
> domains1 = Left$(strvals1, Len(strvals1) - 1)
> poss1 = InStr(1, domains1, Chr$(0), vbBinaryCompare)
>     If poss1 > 1 Then
>         chk = Left$(domains1, poss1 - 1)
>     End If
> chk = RegSetValueEx(Handle, "RegisteredOwner", 0, REG_SZ, ByVal strvals1,
> 128)
> ress = RegCloseKey(Handle)
> End Sub

> Private Sub cmdOK_Click()
> Call SetRegistry
> txtName.Text = ""
> txtCompany.Text = ""
> txtCurrentPIN.Text = ""
> Call GetCurrent
> End Sub

> Private Sub Form_Activate()
> txtNameChange.SetFocus
> sb1.Panels(2).Style = sbrDate
> sb1.Panels(3).Style = sbrTime
> sb1.Panels(2).Bevel = sbrInset
> sb1.Panels(3).Bevel = sbrInset
> End Sub

> Private Sub Form_Load()
> tmrReg.Enabled = False
> End Sub

> Private Sub tmrReg_Timer()
>     Dim v As Integer
>     v = 1
>     pbFinish.Value = 1
>     p = pbFinish.Value
>     Do While v < 1000
>         p = p + 1
>         pbFinish.Value = p
>         v = v + 1
>     Loop
>     pbFinish.Value = 0
> End Sub

> Public Sub GetCurrent()
> Dim Name As String
> txtName.Text = ""
> txtCompany.Text = ""
> txtCurrentPIN.Text = ""
> res = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows
> NT\CurrentVersion", Handle)
> res = RegQueryValueEx(Handle, "RegisteredOwner", 0, REG_SZ, ByVal strval,
> 128)
> domain2 = Left$(strval, Len(strval) - 1)
> pos = InStr(1, domain2, Chr$(0), vbBinaryCompare)
>  If pos > 1 Then
>    Name = Left$(domain2, pos - 1)
>  End If
> res1 = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows
> NT\CurrentVersion", Handle)
> res1 = RegQueryValueEx(Handle, "RegisteredOrganization", 0, REG_SZ, ByVal
> strval1, 128)
> domain3 = Left$(strval1, Len(strval1) - 1)
> pos2 = InStr(1, domain3, Chr$(0), vbBinaryCompare)
>  If pos2 > 1 Then
>    Company = Left$(domain3, pos2 - 1)
>  End If
> res2 = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows
> NT\CurrentVersion", Handle)
> res2 = RegQueryValueEx(Handle, "ProductID", 0, REG_SZ, ByVal strval2, 128)
> domain4 = Left$(strval2, Len(strval2) - 1)
> pos3 = InStr(1, domain4, Chr$(0), vbBinaryCompare)
>  If pos3 > 1 Then
>    CPIN = Left$(domain4, pos3 - 1)
>  End If
> tmrReg.Enabled = True
> Call tmrReg_Timer
> txtName.Text = Name
> txtCompany.Text = Company
> txtCurrentPIN.Text = CPIN
> res = RegCloseKey(Handle)
> res = RegCloseKey(Handle)
> res1 = RegCloseKey(Handle)
> tmrReg.Enabled = False
> txtNameChange.SetFocus
> End Sub

> Public Function GetSerialNumber(sDrive As String) As Long
>    Dim ser As Long
>    Dim s As String * MAX_FILENAME_LEN
>    Dim s2 As String * MAX_FILENAME_LEN
>    Dim i As Long
>    Dim j As Long

>    Call GetVolumeInformation(sDrive + ":\", s, MAX_FILENAME_LEN, ser, _
>       i, j, s2, MAX_FILENAME_LEN)

>    GetSerialNumber = ser
>    sb1.Panels(1).Text = "C:\ SN# = " & ser
>    sb1.Panels(1).Bevel = sbrRaised
>    sb1.Panels(1).Alignment = sbrCenter
> End Function

> Thank you




Fri, 27 Apr 2001 03:00:00 GMT  
 Writing to the Registry via API -- HELP!

Quote:

> Need some help writing to the registry.  I have successfully read from, but
> when I write to the registry the data gets filled with 128 whitespaces plus
> the text I apply.  What is wrong ?
> Included is my source-

[snip]
Quote:
>  Dim strvals1 As String * 128
> strvals1 = txtNameChange.Text
> chk = RegSetValueEx(Handle, "RegisteredOwner", 0, REG_SZ, ByVal strvals1,
> 128)

[snip]

For the last arg of RegSetValueEx, give the actual length of the
data, i.e., Len(txtNameChange.Text). If you use a normal string
instead of a fixed-length one, you can put Len(strvals1). For that
matter, I think you could put the text property directly as the 2nd
last arg.

--



Fri, 27 Apr 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Read data from DB, write to registry via VBscript

2. reading/writing to Registry from ASP via dll

3. ScreenSaver Password in Registry - access via VB 4.0 APIs

4. How can I manipulate registry via API remotely?

5. How to change Registry via API?

6. How can I manipulate registry via API remotely?

7. PROBLEM: Adding a printer via API, then creating a printer Share via API (source included)

8. unable to write to MDB via VB + ODBC API

9. can't write to DB via ODBC API

10. Writing to registry using API

11. How to write to *.ini file via API?

12. Api to write registry value ???

 

 
Powered by phpBB® Forum Software