Fog Creek Software
Discussion Board

VB6 ActiveX EXE Problem

I have an automation server written in VB6 which works fine.  But today I attempted to write some scripts using GetObject() to obtain a reference to an already active instance, but, I found out that GetObject() can't be used to obtain a reference to a class created with Visual Basic.

Has anyone faced this?  Any work-arounds?

Thursday, April 03, 2003


I would recommend checking out:

You might also want to consider using the Singleton pattern instead of GetObject - though it depends on your exact requirements.


Friday, April 04, 2003

You can use a Win32 API to put VB created classes into the Run Object Table (ROT).

Here's the code that I've been using for that:

Private Type GUIDs
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

'Declares needed to register object in the ROT (Run Object Table)
Private Declare Function CLSIDFromProgID Lib "ole32.dll" (ByVal ProgID As Long, rclsid As GUIDs) As Long
Private Declare Function CoDisconnectObject Lib "ole32.dll" (ByVal pUnk As IUnknown, pvReserved As Long) As Long
Private Declare Function RegisterActiveObject Lib "oleaut32.dll" (ByVal pUnk As IUnknown, rclsid As GUIDs, ByVal dwFlags As Long, pdwRegister As Long) As Long
Private Declare Function RevokeActiveObject Lib "oleaut32.dll" (ByVal dwRegister As Long, ByVal pvReserved As Long) As Long
Private OLEInstance As Long

Public Sub AddToROT()

    Dim mGuid As GUIDs
    Dim lp As Long

    'The magic happens here
    'This code is responsible for creating the entry in the ROT
    'Make sure to insert the correct qualified object (class) that
    'you want in the ROT.
    OLEInstance = 0
    lp = CLSIDFromProgID(StrPtr("YourActiveXServerName.YourClassName"), mGuid)

    If lp = 0 Then
        lp = RegisterActiveObject(Me, mGuid, ACTIVEOBJECT_WEAK, OLEInstance)
    End If

End Sub

Public Sub RemoveFromROT()

    'Once we are done with the main program, lets clean up the rot
    'by removing the entry for our ActiveX Server/DLL
    If OLEInstance <> 0 Then
        RevokeActiveObject OLEInstance, 0
    End If
    CoDisconnectObject Me, 0

End Sub



Friday, April 04, 2003

Thanks for the help.

Friday, April 04, 2003

*  Recent Topics

*  Fog Creek Home