Determine Activex dll already running

Posted on 2012-08-12
Medium Priority
Last Modified: 2012-08-31

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

Question by:lxfdirs
  • 2
  • 2
LVL 36

Expert Comment

ID: 38286504
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.
LVL 96

Expert Comment

by:Bob Learned
ID: 38287523
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)

Author Comment

ID: 38290323

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

LVL 96

Expert Comment

by:Bob Learned
ID: 38290392
I haven't done VB6 since 2002, so I hope that you find a solution.
LVL 36

Accepted Solution

mccarl earned 1500 total points
ID: 38290570
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)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question