INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Using Dllregisterserver Entry Point in function with success failure returned

Using Dllregisterserver Entry Point in function with success failure returned

Using Dllregisterserver Entry Point in function with success failure returned

(OP)
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 

RE: Using Dllregisterserver Entry Point in function with success failure returned

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?

RE: Using Dllregisterserver Entry Point in function with success failure returned

(OP)
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.


RE: Using Dllregisterserver Entry Point in function with success failure returned

>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?

RE: Using Dllregisterserver Entry Point in function with success failure returned

(OP)
>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.




RE: Using Dllregisterserver Entry Point in function with success failure returned

>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



RE: Using Dllregisterserver Entry Point in function with success failure returned

(OP)
>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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close