Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Excel coredumps with each call to a dll - for function RegQueryValueExA in advapi32.dll

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\\ComputerN ame\
\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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Excel coredumps with each call to a dll - for function RegQueryValueExA in advapi32.dll

On 18 Sep, 19:39, Tom Med wrote:
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\\ComputerN ame\
\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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Excel coredumps with each call to a dll - for function RegQueryValueExA in advapi32.dll

On 18 Sep, 19:49, Tom Med wrote:
On 18 Sep, 19:39, Tom Med wrote:



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 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\\ComputerN ame\
\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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Excel coredumps with each call to a dll - for function RegQueryValueExA in advapi32.dll


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)


"Tom Med" wrote in message
oups.com...
On 18 Sep, 19:49, Tom Med wrote:
On 18 Sep, 19:39, Tom Med wrote:



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 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\\ComputerN ame\
\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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Excel coredumps with each call to a dll - for function RegQueryValueExA in advapi32.dll

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.


On 18 Sep, 21:58, "Chip Pearson" wrote:
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 Consultingwww.cpearson.com
(email on the web site)

"Tom Med" wrote in message

oups.com...

On 18 Sep, 19:49, Tom Med wrote:
On 18 Sep, 19:39, Tom Med wrote:





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CALL .NET FUNCTION FROM EXCEL VSTO Beginner Excel Worksheet Functions 0 February 27th 08 06:57 AM
Hashing function CryptAcquireContext Lib "advapi32" not working in new computer. Stephen Rasey[_3_] Excel Programming 5 December 25th 06 04:05 AM
How can I call a JavaScript Function from Excel VBA [email protected] Excel Programming 0 April 17th 06 03:33 PM
call a excel function from a vb project Alex281 Excel Programming 2 March 29th 06 05:22 AM
Help with VB Call function from Excel Lawrence M. Seldin, CMC, CPC Excel Programming 0 June 3rd 05 06:13 PM


All times are GMT +1. The time now is 08:03 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"