Solved

Handling Multiple Instances

Posted on 2001-06-26
3
351 Views
Last Modified: 2007-12-19
Reference:
MSDN Library Visual Studio 6.0
HardCore Visual Basic
Handling Multiple Instances

Text From Example:
The GetFirstInstWnd function does the actual work. It works by looping through all the top windows until it finds one that has a different process ID, but the same module name. That?s the duplicate. The code (in MODTOOL.BAS) looks like this:

Function GetFirstInstWnd(hWndMe As Long) As Long    
    Dim hWndYou As Long, idMe As Long, sExeMe As String    
    ' Get my own process ID and executable name    
    idMe = MWinTool.ProcIDFromWnd(hWndMe)    
    sExeMe = ExeNameFromWnd(hWndMe)    
    ' Get first sibling to start iterating top-level windows    
    hWndYou = GetWindow(hWndMe, GW_HWNDFIRST)    
    Do While hWndYou <> hNull        
        ' Ignore if process ID of target is same        
        If idMe <> MWinTool.ProcIDFromWnd(hWndYou) Then            
            ' Ignore if module name is different            
            If sExeMe = ExeNameFromWnd(hWndYou) Then                
                ' Return first with same module, different process                
                GetFirstInstWnd = hWndYou                
                Exit Function            
            End If        
       End If        
       ' Get next sibling        
       hWndYou = GetWindow(hWndYou, GW_HWNDNEXT)    
   Loop
End Function
>>>>>
The above discussion was found in Visual Studio 6.0 help. If you inspect the above wording a reference is made to MODTOOL.BAS which I suspet is in a CD that comes with the HardCore Visual Basic Book.
I do not have this CD or book but suspect that the ExeNameFromWnd function referenced in the above code is comprised of several Windows API CALLs that somehow  produces the name of a process (in string format) given the Window's hWnd.

How was this done? A code sample is what I am looking for here.

0
Comment
Question by:jdwarren
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 6227852
I don't know if this is the original code but it should perform the same function. Paste it into a module and then call Msgbox ExeNameFromHwnd(hWnd)

Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer
Const GWW_HINSTANCE = (-6)

Public Function ExeNameFromHwnd(hwnd As Long) As String
    Dim ModuleName As String, FileName As String, hInst As Long
    ModuleName = String$(128, Chr$(0))
    hInst = GetWindowWord(hwnd, GWW_HINSTANCE)
    ModuleName = Left$(ModuleName, GetModuleFileName(hInst, ModuleName, Len(ModuleName)))
    ExeFromHwnd = ModuleName
End Function

0
 
LVL 15

Expert Comment

by:ameba
ID: 6227880
That file is in HardCore3.zip (2.5 MB), you can download it from:
http://www.vb-zone.com/upload/free/features/vbpj/1999/mckinney/mckinney2a.asp
0
 

Author Comment

by:jdwarren
ID: 6235210
TimCottee,
Thanks for the code example. I will accept your answer.

ameba,
your answer is also correct in that it actually provides the source code that was referenced in my question. I gave the points to TimCottee only because I ended up using his approach. Thanks very much for this link as other may find it useful.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 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

13 Experts available now in Live!

Get 1:1 Help Now