Sharing an instance of a VB DLL between 2 other VB apps

Using VB6, I have developed an Active X DLL and I wish to allow 2 other
VB Apps to share an instance of this DLL (so that only one instance of a form that is
contained in the Active X DLL will ever be displayed) I currently use new to create
both instances. Can I use getobject ? Anyone know how to do this???
LVL 1
dd021197Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_biggelaarCommented:
The only way to share an instance of a DLL is by using MTS, but I don't think that's what you want. (if the component is residing in MTS a call to create an instance of this DLL is caught by MTS. MTS will create the instance the first time it's called for and will try and use it as much as possible. If necessary it will create more instances. For the user/programmer though it will look as if they all created a new instance)

A DLL always runs in proces, that is in the same thread as your application, so you can't share an instance of a DLL with another app.

What you can do is make a ActiveX.exe instead of a dll. In that case you can use the GetObject function.

Good luck!
0
samopalCommented:
1) Create new ActiveX DLL
2) Rename Class1 to clsHold and set instancing to 1 - Private
Add code

Option Explicit
Dim colUsers As New Collection

Public Function GetCount() As Integer
    GetCount = colUsers.Count
End Function

Public Function AddConection(CompName As String) As Byte
    AddConection = True
    On Error GoTo MultiConnect
   
    colUsers.Add Item:=1, Key:=CompName
   
    Exit Function
   
MultiConnect:
    If Err.Number = 457 Then 'err 457 - dublicates key
        ' Do smth
    Else
        ' Unknow error
    End If
End Function

Public Sub CloseConection(CompName As String)
    On Error Resume Next
    colUsers.Remove (CompName)
End Sub

Public Sub CloseAllConnections()
    Set colUsers = Nothing
End Sub

3) Add another class module with
 name = clsMain
 instansing = 5 - MultiUse

Code

Option Explicit
Dim CurrentCompName As String
Dim Hold as clsHold

Public Function AddConection(CompName As String) As Long
    CurrentCompName = CompName ' remember for future unload
    On Error GoTo Er
    AddConection = -1
    If Hold.AddConection(CompName) Then
'   Do smth        
        AddConection = 0
    End If
    Exit Function
Er:
    MsgBox Err.Description
End Function

Public Sub CloseConection(CompName As String)
'  Do smth.
    Hold.CloseConection CompName
End Sub

Public Function GetCount()
    GetCount = Hold.GetCount
End Function

Public Sub ClearAllConnections() ' Set Computers Connections Counter to 0
    Hold.CloseAllConnections
End Sub

Public Function GetTime() As Date ' Return Computer Server  time
    GetTime = Time()
End Function


Private Sub Class_Initialize()
    If Hold Is Nothing Then
        Set Hold = New clsHold
    End If
End Sub

Private Sub Class_Terminate()
    On Error Resume Next
    Hold.CloseConection CurrentCompName 'Clear one connections from this CompName
End Sub

4) Compile your DLL
5) Create test programm
Put on the form textbox and 3 command buttons

Dim Server as new myDLL.clsMain


sub Command1_click()
'Add a connection
Server.AddConnection Text1
end sub

Sub Command2_Click()
'Check connection
msgbox "You have " & Server.GetCount & " open connections"
end sub

sub Command3_click()
'Disconnect
Server.CloseConnection Text1
end sub


Compile test programm and run as many times as you want
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
p_biggelaarCommented:
Shame on me!

Thanks samopal for this wise lesson!
0
adjenninCommented:
There is something similar to this in the coffeemonitor demo that comes with VB6
0
ssripathyCommented:
I tried the solution presented by "samopal" but it doesn't really solve what is being asked for.
When the component is  compiled as a DLL and referenced by a Exe, a new instance of the DLL is created when the Exe is run(unless the DLL is registered on an MTS).
I would like to know how "samopal" managed to share an in-process DLL among different EXEs. I hope this is what the questioner wanted!!!
I do would like to know what the answer is.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.