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?