• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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???
1 Solution
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!
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
    If Err.Number = 457 Then 'err 457 - dublicates key
        ' Do smth
        ' 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


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
    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
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()
Server.CloseConnection Text1
end sub

Compile test programm and run as many times as you want
Shame on me!

Thanks samopal for this wise lesson!
There is something similar to this in the coffeemonitor demo that comes with VB6
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now