CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    75

    Trying to add DLL references in ActiveX DLL

    OK, first, I followed the instructions on this website to let VB create usable DLLs from other apps: http://www.windowsdevcenter.com/pub/...reate_dll.html

    The DLL I want to create is for another app that absolutely requires the DLL file to be inside it's sandbox. Please don't judge me on this project. I want to create a simple DLL that will permit me to use registry functions that normally would not be possible. SO, I created an activeX DLL which I compiled and works fine until I use the registry functions. I get an error 6 every single time which is ERROR_INVALID_HANDLE. Here is my code:

    Code:
    Option Explicit
    
    Public Const RET_FALSE As Long = 0
    Public Const RET_TRUE As Long = 1
    
    Public Const ERROR_SUCCESS As Long = 0
    Public Const ERROR_INVALID_HANDLE As Long = 6
    
    ' Registry functions and calls
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_CONFIG = &H80000005
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_USERS = &H80000003
    
    Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Public 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
    Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
      ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, _
      ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
    Public Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long
    Public Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As Any) As Long
    
    Public Const DLL_PROCESS_DETACH = 0
    Public Const DLL_PROCESS_ATTACH = 1
    Public Const DLL_THREAD_ATTACH = 2
    Public Const DLL_THREAD_DETACH = 3
    
    Public ERR_NUMBER As Long
    Public ERR_DESC As String
    Public ERR_FUNC As String
    
    Public Function DllMain(hInst As Long, fdwReason As Long, lpvReserved As Long) As Boolean
      Select Case fdwReason
        Case DLL_PROCESS_DETACH
          ' No per-process cleanup needed
        Case DLL_PROCESS_ATTACH:
          DllMain = True
        Case DLL_THREAD_ATTACH
          ' No per-thread initialization needed
        Case DLL_THREAD_DETACH
          ' No per-thread cleanup needed
      End Select
    End Function
    
    ' Creates a key in the Windows Registry
    ' Returns 1 if no error occured or if key already exists
    Public Function RegCreateKeyGM(ByVal shKey As String, ByVal sSubKey As String) As Long
    
      Dim RetVal As Long
      Dim lhKey As Long
      
      lhKey = HKval(shKey)
       
      ' Check if key exists before creating it
      RetVal = RegKeyExists(shKey, sSubKey)
      
      ' Create key if it does not exist
      If (RetVal <> ERROR_SUCCESS) Then RetVal = RegCreateKey(lhKey, sSubKey, 0)
      
      '' Set correct return value
      If (RetVal <> ERROR_SUCCESS) Then RetVal = RET_FALSE Else RetVal = RET_TRUE
      
      RegCreateKeyGM = RetVal
      
    End Function
    
    ' Detects if a key in the registry exists or not
    ' Returns 0 if key does not exists, 1 if the key exists
    Public Function RegKeyExists(ByVal shKey, ByVal sSubKey As String) As Long
    
      Dim RetVal As Long
      Dim lhKey As Long
    
      lhKey = HKval(shKey)
        
      ' Open requested key to see if it exists
      RetVal = RegOpenKey(lhKey, sSubKey, 0&)
      ' Close key if it was opened
      RegCloseKey lhKey
      
      ' Return correct return value
      If (RetVal = ERROR_SUCCESS) Then RetVal = RET_TRUE Else RetVal = RET_FALSE
      
      RegKeyExists = RetVal
    
    End Function
    
    ' Convert HK** string to their long values
    ' Permits to use comprehensible strings instead of long values from external sources
    Public Function HKval(ByVal shKey As String) As Long
    
      Dim tStr As String
      Dim RetVal As Long
      
      RetVal = 0
      
      tStr = UCase(shKey)
      
      Select Case tStr
        Case "HKCU", "HKEY_CURRENT_USER": RetVal = HKEY_CURRENT_USER
        Case "HKLM", "HKEY_LOCAL_MACHINE": RetVal = HKEY_LOCAL_MACHINE
        Case "HKCR", "HKEY_CLASSES_ROOT": RetVal = HKEY_CLASSES_ROOT
        Case "HKCC", "HKEY_CURRENT_CONFIG": RetVal = HKEY_CURRENT_CONFIG
      End Select
      
      HKval = RetVal
    
    End Function
    Basically, If I create any other functions that require NO DLL checks, I get no error BUT when I call THIS compiled DLL and calling the registry functions, I get an error 6.

    NOTE: I tested the declared calls in a seperate VB executable and the calls work BUT when using this DLL, that's when the error 6 occurs. Can someone help me out please?
    David Richard

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Trying to add DLL references in ActiveX DLL

    How are you calling the function from code?
    Which function is giving the error?
    Have you tried using the debugger to locate which line is throwing the error?
    What OS are you using?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    75

    Re: Trying to add DLL references in ActiveX DLL

    Quote Originally Posted by DataMiser View Post
    How are you calling the function from code?
    Which function is giving the error?
    Have you tried using the debugger to locate which line is throwing the error?
    What OS are you using?
    I'm using Windows 10
    I redirected the output as a string initially to retrieve the error as a string instead of a number and the error occured on every DLL call. When I tried to RegOpenKey, RegCloseKey and RegCreateKey. I have the same error on all functions, error #6. I also tried skipping the string conversion process by simply using the LONG value of the HKEYs, just in case it could have an error when calling a function within the function call but that did not solve the issue, still the same problem.

    Basically If I use the same code in a module in a normal executable, it works absolutely fine, no problem. As soon as I convert it to a DLL, that's where I get the error.

    For your info, that's how I call my DLL (Which I named and stored in a folder under "c:\apps\dlls" GMRegDLL.dll) in VB:
    NOTE: I also regsvr32'd the DLL and Microsoft gave me NO error so the DLL is registered correctly.

    Code:
    Public Declare Function RegCreateKeyGM Lib "c:\apps\dlls\GMRegDLL.dll" (ByVal shKey As String, ByVal sSubKey As String) As String
    Public Declare Function RegKeyExists Lib "c:\apps\dlls\GMRegDLL.dll" (ByVal shKey As String, ByVal sSubKey As String) As Long
    Then I call the functions this way under a button in a form

    Code:
    RegCreateKeyGM("hkcu", "SOFTWARE\DaRic - Test")
    If I change these functions to anything that will not call another DLL, it works absolutely fine, the problem relies when I try to use the advapi32 dll functions.
    David Richard

  4. #4
    Join Date
    Jul 2005
    Location
    Quebec, Canada
    Posts
    75

    Re: Trying to add DLL references in ActiveX DLL

    OK, I seem to be on a track here. Everytime I call a DLL, the HKEY value is 0, which is not a correct handle BUT. The problem relies that when calling a DLL with string parameters, you absolutely need to finish your string with a null character "char(0)". Or else, never will it recognize your passed value. Now the question is:

    How can I pass a string value to a DLL call without the need of a null terminating character in the optics that the DLL is created by myself?
    Is there a way for me to not require a Null terminating character in my DLL?
    David Richard

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured