?
Solved

Handling Multiple Instances

Posted on 2001-06-26
3
Medium Priority
?
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 800 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

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

762 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