Windows API call to write to registry crashes VB6

Posted on 2006-05-08
Last Modified: 2013-11-13
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,
Question by:NickFinch
    LVL 48

    Expert Comment

    Can you show code after RegCreateKeyEx call? RegCreateKeyEx returns 0 if succeded, or some other value if failed. Does application check return value?

    Author Comment

    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)

    LVL 48

    Accepted Solution

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

    Author Comment


    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.


    Author Comment

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


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    matchUp  challenge 6 36
    seriesUp challenge 7 81
    mapShare challenge 13 41
    firstChar challenge 13 57
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now