Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Handling Multiple Instances

Posted on 2001-06-26
3
Medium Priority
?
392 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Suggested Courses

604 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