Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
CALL .NET FUNCTION FROM EXCEL | Excel Worksheet Functions | |||
Hashing function CryptAcquireContext Lib "advapi32" not working in new computer. | Excel Programming | |||
How can I call a JavaScript Function from Excel VBA | Excel Programming | |||
call a excel function from a vb project | Excel Programming | |||
Help with VB Call function from Excel | Excel Programming |