• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 587
  • Last Modified:

Windows API call to write to registry crashes VB6

We have a VB6 application that uses windows API calls to read and write to the registry. To open the key to be able to write to it, we use the call...

    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

Dim hKey As Long                    ' receives handle to the registry key
Dim neworused As Long           ' receives flag for if the key was created or opened
Dim retVal As Long                  ' return value
' Set the name of the new key and the default security settings
secattr.nLength = Len(secattr)
secattr.lpSecurityDescriptor = 0
secattr.bInheritHandle = 1

retval = RegCreateKeyEx(&H80000002, sSubKey, 0, "", 0, &H20006, secattr, hKey, neworused)

This works fine most of the time. However, with a particular customer, their ADMIN users do not have write privaliges to HKEY_LOCAL_MACHINE, and when our app tries to create a new key as above, the app just vanishes, no error messages. Is there any way we can test whether or not we can create a key, everything we have tried seems to have the same effect of crashing VB.

Thanks in advance,
  • 3
  • 2
1 Solution
Can you show code after RegCreateKeyEx call? RegCreateKeyEx returns 0 if succeded, or some other value if failed. Does application check return value?
NickFinchAuthor Commented:
Yes, we are checking it. The crash actually happens on this line, subsequent lines are....

    If retVal <> 0 Then
        pamsgbox LoadResString(40129), vbExclamation
        Exit Sub
    End If
    ' Write the string to the registry.  Note the use of ByVal in the second-to-last
    ' parameter because we are passing a string.
    retVal = RegSetValueEx(hKey, sValueName, 0, REG_SZ, ByVal sStringBuffer, Len(sStringBuffer))
    ' Close the registry key.
    retVal = RegCloseKey(hKey)

Try to change lpClass parameter: pass Long 0 to it instead of "".
Show VB6 definition of RegCreateKeyEx function.
NickFinchAuthor Commented:

Definition is ....

Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long

I'll try your suggestion. Will take a couple of hours because we need to try it on our customers site, but will get back to you.

NickFinchAuthor Commented:
Sorry for the delay Alex, our customer took a while to get back to me. Anyway, worked a treat, many thanks!!


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now