Solved

Handling Multiple Instances

Posted on 2001-06-26
3
346 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

12 Experts available now in Live!

Get 1:1 Help Now