Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find Window's Owning App -- Win32 API

Posted on 1998-05-11
7
Medium Priority
?
276 Views
Last Modified: 2013-12-03
I need to connect windows with their "owning" application (i.e. for all high level windows, find which application opened them). I have tried GetModuleFileName(), but under Windows 95 it only works for the process my app is current running under, which seems pretty useless. (Apparently it works fine under Win 3.1, but that is not the environment I care about.) I need to connect other application's to their windows, not my own.
0
Comment
Question by:parkerea
[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
  • 5
  • 2
7 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1400907
GetWindowThreadProcessId()
0
 
LVL 11

Expert Comment

by:alexo
ID: 1400908
If that doesn't help, have you tried passing GetWindowLong(GWL_HINSTANCE) to GetModuleFileName()?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1400909
Also see KB article Q119163

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:parkerea
ID: 1400910
Yes, I have tried the proper combination of GetWindowLong() and GetModuleFileName(), but as I mentioned, under Win 95 it only works for the current app, not other apps. (Win NT has the same limitation because both are Win32.) Apparently this is because other apps (i.e. other processes) run in a different address space under Win32.

Per Alexo's note, I checked knowledge base article Q119163, but it appears this approach only works for Windows NT, because it looks through the registry for what appears to be an NT specific key, "HKEY_PERFORMANCE_DATA", if I am reading it properly. The article says it applies to NT 3.1 & 3.5; it does not mention Win 95. I need a solution for any Win32 environment.

0
 
LVL 11

Accepted Solution

by:
alexo earned 600 total points
ID: 1400911
You need to use the tool help library (Win95) or PSAPI (NT) to get the filename of a process.

For Win95: check the CreateToolhelp32Snapshot() and Process32First() / Process32Next(), Module32First() / Module32Next() functions.

The process ID is available via GetWindowThreadProcessId().

0
 

Author Comment

by:parkerea
ID: 1400912
Thanks, that should do it, but I do still have a problem: where the heck do I get ToolHelp32.dll? My Win 95 PC has the 16 bit version, ToolHelp.dll, but that is it. According to the documentation I have, the ToolHelp32 functions are actually in Kernel32.dll, but they are not exported. Do I need to get the Win 95 SDK or something?
0
 
LVL 11

Expert Comment

by:alexo
ID: 1400913
The tool help functions reside in the operating system kernel. The following example provides a platform-independent approach to accessing the tool help functions.

#include <tlhelp32.h> // needed for tool help declarations  
 
// Type definitions for pointers to call tool help functions.
typedef BOOL (WINAPI *MODULEWALK)(HANDLE hSnapshot,
    LPMODULEENTRY32 lpme);
typedef BOOL (WINAPI *THREADWALK)(HANDLE hSnapshot,
    LPTHREADENTRY32 lpte);
typedef BOOL (WINAPI *PROCESSWALK)(HANDLE hSnapshot,
    LPPROCESSENTRY32 lppe);
typedef HANDLE (WINAPI *CREATESNAPSHOT)(DWORD dwFlags,
    DWORD th32ProcessID);
 
// File scope globals. These pointers are declared because of the need
// to dynamically link to the functions.  They are exported only by
// the Windows 95 kernel. Explicitly linking to them will make this
// application unloadable in Microsoft(R) Windows NT(TM) and will
// produce an ugly system dialog box.
static CREATESNAPSHOT pCreateToolhelp32Snapshot = NULL;
static MODULEWALK  pModule32First  = NULL;
static MODULEWALK  pModule32Next   = NULL;
static PROCESSWALK pProcess32First = NULL;
static PROCESSWALK pProcess32Next  = NULL;
static THREADWALK  pThread32First  = NULL;
static THREADWALK  pThread32Next   = NULL;
 
// Function that initializes tool help functions.
BOOL InitToolhelp32 (void)
{
    BOOL   bRet  = FALSE;
    HANDLE hKernel = NULL;
 
    // Obtain the module handle of the kernel to retrieve addresses of
    // the tool helper functions.
    hKernel = GetModuleHandle("KERNEL32.DLL");
 
    if (hKernel){
        pCreateToolhelp32Snapshot =
            (CREATESNAPSHOT)GetProcAddress(hKernel,
            "CreateToolhelp32Snapshot");
 
        pModule32First  = (MODULEWALK)GetProcAddress(hKernel,
            "Module32First");
        pModule32Next   = (MODULEWALK)GetProcAddress(hKernel,
            "Module32Next");
 
        pProcess32First = (PROCESSWALK)GetProcAddress(hKernel,
            "Process32First");
        pProcess32Next  = (PROCESSWALK)GetProcAddress(hKernel,
            "Process32Next");
 
        pThread32First  = (THREADWALK)GetProcAddress(hKernel,
            "Thread32First");
        pThread32Next   = (THREADWALK)GetProcAddress(hKernel,
            "Thread32Next");
 
        // All addresses must be non-NULL to be successful.
        // If one of these addresses is NULL, one of
        // the needed lists cannot be walked.
        bRet =  pModule32First && pModule32Next  && pProcess32First && 
                pProcess32Next && pThread32First && pThread32Next && 
                pCreateToolhelp32Snapshot;
    }
    else
        bRet = FALSE; // could not even get the module handle of kernel
 
    return bRet;
}
 

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

722 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