Determine Activex dll already running


Is there a way to determine if an activex dll is already instanced. I have a couple of client applications calling the same dll.

Who is Participating?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Here is some VB code (tested in MS Access since I'm not a VB person much) that lists the processes and the modules/dll's that are running. You should be able to take this and modify it to suit what you are doing.

Option Explicit

Private Declare Function OpenProcess Lib "Kernel32.dll" ( _
                                    ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
                                    ByVal dwProcId As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" ( _
                                      ByRef lpidProcess As Long, ByVal cb As Long, _
                                      ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" ( _
                                             ByVal hProcess As Long, ByVal hmodule As Long, _
                                             ByVal moduleName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" ( _
                                           ByVal hProcess As Long, ByRef lphModule As Long, _
                                           ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const PROCESS_ALL_ACCESS         As Long = &H1F0FFF

Public Function IsModuleRunning(ByVal theModuleName As String) As Boolean
    Dim aProcessess(1 To 1024)  As Long ' up to 1024 processess?'
    Dim bytesNeeded             As Long
    Dim i                       As Long
    Dim nProcesses              As Long
    Dim hProcess                As Long
    Dim found                   As Boolean

    EnumProcesses aProcessess(1), UBound(aProcessess), bytesNeeded
    nProcesses = bytesNeeded / 4
    For i = 1 To nProcesses

        hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, aProcessess(i))
        If (hProcess) Then
            Dim hmodule(1 To 1024)  As Long ' no more than 1024 modules per process?'
            bytesNeeded = 0
            If EnumProcessModules(hProcess, hmodule(1), 1024 * 4, bytesNeeded) Then
                Dim nModules    As Long
                Dim j           As Long
                Dim moduleName  As String
                Dim fileNameLen As Long

                nModules = bytesNeeded / 4
                For j = 1 To nModules

                    moduleName = Space(1024)   ' module name should have less than 1024 bytes'
                    fileNameLen = GetModuleFileNameExA(hProcess, hmodule(j), moduleName, 1024)
                    moduleName = Left(moduleName, fileNameLen)
                    Debug.Print moduleName
            End If
        End If
        CloseHandle hProcess
        If found Then Exit For
    IsModuleRunning = found
End Function

Private Sub Form_Load()
    IsModuleRunning ("explorer.exe")
End Sub

Open in new window

Let us know if you have any issues with it. (There may need to be a bit more done to it to ensure that there are no memory leaks, crashes, etc, but it should give you the idea)
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Are you looking for a solution in code? Or otherwise?

The example C++ code here...

.. should get you on the right track. You would just need to add code to filter for the particular dll that you are interested in.

If you are looking for a non-code solution, have a look at the windows console tasklist command. In particular the /m option can be used to list all modules loaded, or to filter the list for a particular dll (or dll that matches a pattern). tasklist /? gives more info.
Bob LearnedCommented:
COM libraries are registered in the Running Object Table (ROT), and you can query the table.  Sometimes tricks are needed, like when working with current versions of Excel and Word, as they don't register themselves in the ROT in a very friendly manner.

Iterating through the contents of the ROT (running objects table)
lxfdirsAuthor Commented:

Appreciate you comments. Actually, my code is in vb6. Can you suggest a solution from within vb6?

Bob LearnedCommented:
I haven't done VB6 since 2002, so I hope that you find a solution.
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.

All Courses

From novice to tech pro — start learning today.