farhanzia,
I usualy use something like this. Sorry for the long post.
Private Declare Function LoadLibraryRegister Lib "KERNEL32" Alias "LoadLibraryA" _ (ByVal lpLibFileName As String) As Long Private Declare Function FreeLibraryRegister Lib "KERNEL32" Alias "FreeLibrary" _ (ByVal hLibModule As Long) As Long Private Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long Private Declare Function GetProcAddressRegister Lib "KERNEL32" Alias "GetProcAddress" _ (ByVal hModule As Long, ByVal lpProcName As String) As Long Private Declare Function CreateThreadForRegister Lib "KERNEL32" Alias "CreateThread" _ (lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, _ ByVal lpparameter As Long, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long Private Declare Function WaitForSingleObject Lib "KERNEL32" _ (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function GetExitCodeThread Lib "KERNEL32" _ (ByVal hThread As Long, lpExitCode As Long) As Long Private Declare Sub ExitThread Lib "KERNEL32" (ByVal dwExitCode As Long) Private Const WAIT_OBJECT_0 = &H0 Private Const NOERRORS As Long = 0
Public Enum stRegisterStatus stFileCouldNotBeLoadedIntoMemorySpace = 1 stNotAValidActiveXComponent = 2 stActiveXComponentRegistrationFailed = 3 stActiveXComponentRegistrationSuccessful = 4 stActiveXComponentUnRegisterSuccessful = 5 stActiveXComponentUnRegistrationFailed = 6 stNoFileProvided = 7 End Enum
Public Function Register(ByVal p_sFileName As String) As stRegisterStatus 'Variant Dim lLib As Long Dim lProcAddress As Long Dim lThreadID As Long Dim lSuccess As Long Dim lExitCode As Long Dim lThreadHandle As Long Dim lRet As Long On Error GoTo ErrorHandler p_sFileName = Trim(p_sFileName) If lRet = NOERRORS Then If p_sFileName = "" Then 'error lRet = stNoFileProvided Else lLib = LoadLibraryRegister(p_sFileName) If lLib = 0 Then 'error lRet = stFileCouldNotBeLoadedIntoMemorySpace Else lProcAddress = GetProcAddressRegister(lLib, "DllRegisterServer") If lProcAddress = 0 Then 'error lRet = stNotAValidActiveXComponent Else lThreadHandle = CreateThreadForRegister(0, 0, lProcAddress, 0, 0, lThreadID) If lThreadHandle <> 0 Then lSuccess = (WaitForSingleObject(lThreadHandle, 10000) = WAIT_OBJECT_0) If lSuccess = 0 Then 'error Call GetExitCodeThread(lThreadHandle, lExitCode) Call ExitThread(lExitCode) lRet = stActiveXComponentRegistrationFailed Else lRet = stActiveXComponentRegistrationSuccessful End If End If End If End If End If End If ExitRoutine: Register = lRet If lThreadHandle <> 0 Then Call CloseHandle(lThreadHandle) If lLib <> 0 Then Call FreeLibraryRegister(lLib) Exit Function ErrorHandler: lRet = Err.Number GoTo ExitRoutine End Function
|