Excel coredumps with each call to a dll - for function RegQueryValueExA in advapi32.dll

T

Tom Med

I am using a call to the following function

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

I then use this in code as follows, and each time I run it excel just
dies horribly. Is there something I am doing wrong? has anyone got
any working examples of RegQueryValueEx ?
I have checked the dll file exists and has this function call

Thanks
Tom

' Hello, World! for the Registry: gets this machine's name and prints
' it out.
Public Sub tempreg()

Dim pszName As String
Dim nNameLen As Long: nNameLen = 255
Dim hkResult As Long, hStartKey As Long
Dim nResult As Long
Dim nResult2
hStartKey = HKEY_LOCAL_MACHINE
nResult = ERROR_SUCCESS

nResult = RegOpenKeyEx(hStartKey, _
"SYSTEM\\CurrentControlSet\\Control\\ComputerName\
\ActiveComputerName", _
0&, KEY_READ, hkResult)
If (ERROR_SUCCESS = nResult) Then
nResult = RegQueryValueEx(hkResult, "ComputerName", 0, 0,
pszName, nNameLen)
If (ERROR_SUCCESS = nResult) Then
MsgBox "Hello, world, from " & pszName
Else
MsgBox "I don't even know my own name."
End If
End If
End Sub
 
T

Tom Med

I am using a call to the following function

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

I then use this in code as follows, and each time I run it excel just
dies horribly. Is there something I am doing wrong? has anyone got
any working examples of RegQueryValueEx ?
I have checked the dll file exists and has this function call

Thanks
Tom

Have now modified this to reserve memory in the result string, but it
still cores

Tom

Public Sub tempreg()

Dim pszName As String
Dim nNameLen As Long: nNameLen = 255
Dim hkResult As Long, hStartKey As Long
Dim nResult As Long
Dim nResult2

hStartKey = HKEY_LOCAL_MACHINE
pszName = Space$(nNameLen + 1)
nResult = ERROR_SUCCESS

nResult = RegOpenKeyEx(hStartKey, _
"SYSTEM\\CurrentControlSet\\Control\\ComputerName\
\ActiveComputerName", _
0&, KEY_READ, hkResult)
If (ERROR_SUCCESS = nResult) Then
nResult2 = RegQueryValueEx(hkResult, "ComputerName", 0, 0,
pszName, nNameLen)
If (ERROR_SUCCESS = nResult) Then
MsgBox "Hello, world, from " & pszName
Else
MsgBox "I don't even know my own name."
End If
End If
'RegCloseKey hkResult
End Sub
 
T

Tom Med

Have finally got this working, it seems you have to pass in an array
of bytes as your buffer. You have to love VBA for being so un-user
friendly when it meets errors

' Hello, World! for the Registry: gets this machine's name and prints
' it out.
Public Sub tempreg()

Dim pszName As String
Dim ByteArray() As Byte
Dim ByteString As String
Dim KeyType As Long

Dim nNameLen As Long: nNameLen = 255
Dim hkResult As Long, hStartKey As Long
Dim nResult As Long

ReDim ByteArray(nNameLen)

hStartKey = HKEY_LOCAL_MACHINE
pszName = Space$(nNameLen + 1)
nResult = ERROR_SUCCESS

nResult = RegOpenKeyEx(hStartKey, _
"SYSTEM\\CurrentControlSet\\Control\\ComputerName\
\ActiveComputerName", _
REG_OPTION_NON_VOLATILE, KEY_READ, hkResult)
If (ERROR_SUCCESS = nResult) Then
nResult = RegQueryValueEx(hkResult, "ComputerName", 0&,
KeyType, ByteArray(0), nNameLen)
CopyMemory ByVal pszName, ByteArray(0), nNameLen
If (ERROR_SUCCESS = nResult) Then
MsgBox "Hello, world, from " & pszName
Else
MsgBox "I don't even know my own name."
End If
End If
'RegCloseKey hkResult
End Sub
 
C

Chip Pearson

You can use the GetComputerName API:

Public Declare Function GetComputerName Lib "kernel32" Alias
"GetComputerNameA" ( _
ByVal lpBuffer As String, _
nSize As Long) As Long

Sub AAA()
Dim CompName As String
Dim N As Long
Dim R As Long
N = 255
CompName = String$(N, vbNullChar)
R = GetComputerName(CompName, N)
If R Then
CompName = Left(CompName, N)
End If
Debug.Print CompName
End Sub

or even use Environ:

Sub BBB()
Debug.Print Environ("ComputerName")
End Sub

This is much simpler than jumping through the hoops of the registry.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
T

Tom Med

Thanks, the variable from the registry was academic, I just took that
one from an example in c# that I knew worked. I thought if I gave the
example of the application specific value I actually needed it might
just confuse people in my post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top