Determine Activex dll already running

Posted on 2012-08-12
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
    LVL 35

    Expert Comment

    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
    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


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

    LVL 96

    Expert Comment

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

    Accepted Solution

    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now