PDA

Click to See Complete Forum and Search --> : AxtiveX EXE - Connecting


Wokawidget
Jan 31st, 2002, 06:03 AM
I have an ActiveX EXE on a server. How is it possible for all clients application to reference the same instance?

Basically I want 10-50 clients to use the same AxtiveX EXE which create only ONE connection to a database.

Is this possible, if so then how?

Cander
Jan 31st, 2002, 01:20 PM
Was the exe created in VB? If so this is not simple straight forward, but I wil provide the code. I assume this server is part of your local network.

First off in order to access a running instance of a class in an activex exe or dll you need the getobject command

Dim a as object
set a = GetObject("network location of activex exe","projectname.classname")

now normally that would let you call the functions of a existing instance like this

a.functionname

but here is the problem.
VB ActiveX projects do not write themselves to what is called the ROT..I forgot what this means unfortunatly.Some kind of temporary registry.

But there is a API workaround. Just add this code to the class


Option Explicit

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
Private Const ACTIVEOBJECT_STRONG = 0
Private Const ACTIVEOBJECT_WEAK = 1
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
Dim OLEInstance As Long


Private Sub Class_Initialize()
'This code is responsible for creating the entry in the rot

Dim mGUID As GUIDs
Dim lp As Long
OLEInstance = 0
lp = CLSIDFromProgID(StrPtr("UserModule.clsUserMod"), mGUID)
If lp = 0 Then
lp = RegisterActiveObject(Me, mGUID, ACTIVEOBJECT_WEAK, OLEInstance)
End If

End Sub

Private Sub Class_Terminate()
'Once we are done with the main program, lets clean up the rot
'by removing the entry for our ActiveX Server

If OLEInstance <> 0 Then
RevokeActiveObject OLEInstance, 0
End If

CoDisconnectObject Me, 0

End Sub



just change "UserModule.clsUserMod"

to your activex exe's projectname.classname
good luck

Wokawidget
Feb 1st, 2002, 02:58 AM
Errr...:confused:

I am not a big fan of API, purely due to the fact that I don't know it well enough yet. But that will soon change.

One question. What does the above do?

I have an AxtiveX EXE on my computer and when I run multiple programs, all different, and they access the EXE they can see all the same Public varibles and their values. Does this mean they are seeing the same "instance", or am I just talking crap and am confusing myself? :rolleyes:

Cheers for your help by the way...

Oh, one last thing. I create an ActiveX DLL and an ActiveX EXE, both with the same class modules, Office and Offices, where Offices is a collection of Office. When these loaded the data off the server for 600 offices and passed them to my UI the DLL took 2.1s to populate a grid, where as the EXE took 12s. That was with 1 application. Now when I ran 3 applications at the same time my DLL application still only took 2.5s each, where as the EXE took 15s, 30s and 34s respectively.
These have the same code and properties, just one is DLL and other is EXE.
I knew EXE's were slower, but I didn't think they were THAT slow...:(

Cander
Feb 1st, 2002, 09:10 AM
sorry if ti seems complex but that is what you have to do to access a running isnatnce if a VB object.

otherwise they may see the same functions, but they are of differnet instances.

For example lets say you have 2 clients conencted to your activex if one of them populates a collection, the second cannot access the data client 1 put into the collection. But using that code I gave you along with GetObject allows you to do that.