Results 1 to 3 of 3

Thread: Dynamic ocx into a program

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Taipei
    Posts
    318
    How can I dynamically register a custom ocx into my program?

    If I can, then how to handle for the events and property?

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Here the sample from www.AllApi.net"

    Code:
    ' Add 2 Commandbuttons and a textbox to the form, and paste this code into the form
    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.Text = "C:\WINDOWS\SYSTEM\COMCTL32.OCX"
        Command1.Caption = "Register server"
        Command2.Caption = "Unregister server"
    End Sub
    
    Private Sub Command1_Click()
        Call RegisterServer(Me.hWnd, Text1.Text, True)
    End Sub
    
    Private Sub Command2_Click()
        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
    
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'We're going to call an API-function, without declaring it!
    
        ' Modified by G. Kleijer
        ' [email protected]
        ' going to call the DllRegisterServer/DllUnRegisterServer API of the specified library.
        ' there's no need to use the Regsvr32.exe anymore.
    
        ' Make sure the path is correct and that the file exists, otherwise VB will crash.
    
        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

  3. #3
    Guest
    You can load an unreferenced OCX by using the CreateObject function with the prgrammatic ID:
    Code:
    CreateObject("MSComCtl.TreeView")
    As to how to capture the events, well, you need to access the OCX through an interface. If the OCX or DLL is one you've written, no problem, If it's not, then it's going to be far more difficult (you'll probably have to wrap it in another OCX).

    You need to create an interface DLL, and make your OCX implement the DLL. The application has a reference to the DLL (but it doesn't need to be dstributed with the application). The interface DLL contains a map of all the functions and events you want to expose, but contains no actual code.

    So, in the code where you call the CreateObject function, you neet to put it into an Object variable, then cast the variable to a second variable of the interface type. That way, you can get the events, anmd the whole thing is effectively early-bound (it's actually IDisp type binding, but at least it's not late-bound).

    If you want further info, reply to this post, and I'll provide a better description tomorrow (I've got to go home now - well, more like an hour ago).

    In the meantime, look through the vbpj.com archives for an article called "Dynamic MDI" or something similar by Francesco Balena. That explains this concept pretty well.

    - gaffa

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