Solved

Handling Multiple Instances

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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 (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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