Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Med - 18 Sep 2007 19:39 GMT
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
Tom Med - 18 Sep 2007 19:49 GMT
> I am using a call to the following function
>
[quoted text clipped - 13 lines]
> 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
Tom Med - 18 Sep 2007 20:09 GMT
> > I am using a call to the following function
>
[quoted text clipped - 13 lines]
> > 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\\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
Chip Pearson - 18 Sep 2007 21:58 GMT
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.

Signature

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

>> > I am using a call to the following function
>>
[quoted text clipped - 53 lines]
>    'RegCloseKey hkResult
> End Sub
Tom Med - 19 Sep 2007 15:37 GMT
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.

> You can use the GetComputerName API:
>
[quoted text clipped - 30 lines]
> Pearson Software Consultingwww.cpearson.com
> (email on the web site)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.