Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Dllregisterserver Entry Point in function with success failure returned 1

Status
Not open for further replies.

patriciaxxx

Programmer
Jan 30, 2012
277
GB
I am using DllRegisterServer Entry Point and not regsvr32.exe to register / unregister dll and ocx.
I have the following code on my Access 2003 Form.

My question and problem is that I can't seem to get the "Unsuccessful" msgbox to work. Let me further explain the code looks right to me, and if I enter the full path and name to a DLL or OCX file then it will indeed register or unregister that file and return the "Successful" msgbox. However if I enter an incorrect path or file name whilst no registration / unregistration occurs I still get the "Successful" msgbox when I should get the "Unsuccessful" msgbox.

Please if anyone can see what's wrong with my code I would be grateful.

Code:
Option Compare Database

        Option Explicit

        Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
        Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
        Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
        Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
        Private Const ERROR_SUCCESS = &H0

        Private Sub Form_Load()
        
        Text1.SetFocus
         Text1.Text = "C:\somepath\somedllorocx.OCX"
            Command1.Caption = "Register server"
            Command2.Caption = "Unregister server"
        End Sub

        Private Sub Command1_Click()
                Text1.SetFocus

            Call RegisterServer(Me.hWnd, Text1.Text, True)
        End Sub

        Private Sub Command2_Click()
                Text1.SetFocus

            Call RegisterServer(Me.hWnd, Text1.Text, False)
        End Sub

        Public Function RegisterServer(hWnd As Long, DllServerPath As String, bRegister As Boolean)
            On Error Resume Next


            Dim lb As Long, pa As Long
            lb = LoadLibrary(DllServerPath)

            If bRegister Then
                pa = GetProcAddress(lb, "DllRegisterServer")
            Else
                pa = GetProcAddress(lb, "DllUnregisterServer")
            End If

            If CallWindowProc(pa, hWnd, ByVal 0&, ByVal 0&, ByVal 0&) = ERROR_SUCCESS Then
                MsgBox IIf(bRegister = True, "Registration", "Unregistration") + " Successful"
           Else
                MsgBox IIf(bRegister = True, "Registration", "Unregistration") + " Unsuccessful"
            End If
            'unmap the library's address
            FreeLibrary lb
        End Function
 
From an initial glance, probably because you are testing for whether CallProcWindow is successful or not, not DllRegisterServer (or DllUnRegisterServer). And your On Error Resume Next has masked where the actual error would arise.

This technique is a bit of a hack, to be honest (given that neither of the DllRegisterServer/DllUnRegisterServer functions are actually Windows procs). So let's just find out: WHY do you think you need to do this?
 
Thank you strongm for your reply

>And your On Error Resume Next has masked where the actual error would arise

I removed On Error Resume Next however if I enter an incorrect path or file name whilst no registration / unregistration occurs I still get the "Successful" msgbox when I should get the "Unsuccessful" msgbox and no Error is raised.

> So let's just find out: WHY do you think you need to do this

I am trying to find a way to register / unregister dll / ocx in VBA without the need for any dependent files like regsvr32.exe and this method was what I found.

> because you are testing for whether CallProcWindow is successful or not, not DllRegisterServer (or DllUnRegisterServer).

If you know how to fix this problem I would be grateful to see how you would do that.

Also if you have a better way to accomplish what I am trying to do I would also be grateful to hear that.


 
>lb = LoadLibrary(DllServerPath)

Check the value of lb. If it is 0, then LoadLibrary failed.

>pa = GetProcAddress(lb, "DllRegisterServer")

Check the value of pa. If it is 0, then GetProcAddress failed (i.e. function not found either because it does not exist, or because we failed to load library)

>without the need for any dependent files like regsvr32.exe

Are you envisioning a scenario where regsvr isn't available? And where you don't know the name of your OCX ahead of runtime?
 
>Check the value of lb. If it is 0, then LoadLibrary failed.

Spot on. Thank you.

Am I correct in thinking then that the following and all related code is not needed in my example?

Code:
 Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Const ERROR_SUCCESS = &H0

>Are you envisioning a scenario where regsvr isn't available

I had a situation where regsvr.exe was not allowed and this was my solution.

Thank you very much for help and support.




 
>that the following and all related code is not needed

No, no, the CallWindowProc is the important hack that allows you to run a function in a DLL - so if LoadLibrary (and GetProcAddress) both succeed, then you need CallWindwoProc (or CreateThread, but that's a more complex call) to run the function.

Of course, if you actually know the name of the dll ahead of time, you have the somewhat easier option of:

Public Declare Function Registersomedllorocx Lib "C:\somepath\somedllorocx.OCX" Alias "DllRegisterServer" () as Long
Public Declare Function UnRegistersomedllorocx Lib "C:\somepath\somedllorocx.OCX" Alias "DllUnregisterServer" () as Long



 
>the CallWindowProc is the important hack that allows you to run a function in a DLL - so if LoadLibrary (and GetProcAddress) both succeed, then you need CallWindwoProc (or CreateThread, but that's a more complex call) to run the function

Thank you for taking the time to reply to my question and too for explaining it so that it makes sense to me.

Your continued help and support are very much appreciated.

Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top