[From] http://blog.donews.com/zwell/articles/37069.aspx
The following is an example of how to obtain the title text of a window from a process:
Typedef struct tagwndinfo
{
DWORD dwprocid;
Hwnd;
} Wndinfo, * lpwndinfo;
Bool callback myenumproc (hwnd, lparam)
{
DWORD dwprocid;
Getwindowthreadprocessid (hwnd, & dwprocid );
Lpwndinfo pinfo = (lpwndinfo) lparam;
If (dwprocid = pinfo-> dwprocid)
{
Pinfo-> hwnd = hwnd;
Return false;
}
Return true;
}
Hwnd getprocessmainwnd (DWORD dwprocid)
{
Wndinfo wi;
WI. dwprocid = dwprocid;
WI. hwnd = NULL;
Enumwindows (myenumproc, (lparam) & WI );
Return wi. hwnd;
}
After obtaining the window handle through the getprocessmainwnd function, you can
Call getwindowtext (hwnd) to obtain the window title.
Note: For details about function call parameters, refer to the msdn library!
How to get the executable file name from the window:
The following is an example of how to get executable file names in a window:
Void _ fastcall getexebywnd (hwnd)
{
Handle hproc;
Char szprocessname [80];
Hmodule hmod;
DWORD dwprocid;
Hmod = getclasslong (hwnd, gcl_hmodule); </P> <p> getwindowthreadprocessid (hwnd, & dwprocid); </P> <p> hproc = OpenProcess (process_query_information | process_vm_read,
False,
Dwprocid );
If (hproc)
{
If (getmodulebasename (hproc,
Hmod,
Szprocessname,
Sizeof (szprocessname )))
{
// Add your own ProcessingProgram
}
Else
{
// Add your own handler
}
Closehandle (hproc );
}
} </P> <p> you can use the getmodulefilenameex function to replace getmodulebasename,
It can check the file name of any module in the specified process and carry a path.
However, this function sometimes does not work in Win9x. In this case, only the enumeration process can obtain the file name and path information.
Note: For details about function call parameters, refer to the msdn library!
How to get the executable file name by the process:
The following is a simple example of how a process can get executable file names:
Void _ fastcall getexebyprocid (DWORD dprocid)
{
Handle hproc;
Char szprocessname [80];
Hmodule ahmod [10];
DWORD dwneeded;
Hproc = OpenProcess (process_query_information | process_vm_read,
False,
Dprocid );
If (hproc)
{
If (enumprocessmodules (hproc,
Ahmod,
Sizeof (ahmod ),
& Dwneeded ))
{
If (getmodulebasename (hproc,
Ahmod [0],
Szprocessname,
Sizeof (szprocessname )))
{
// Add your own handler
}
Else
{
// Add your own handler
}
}
Closehandle (hproc );
}
} </P> <p>
API functions related to process ID acquisition:
: Getcurrentprocessid () to get the process ID of the currently running program;
The following three API functions can obtain all the process IDs in the system:
: Createconlhelp32snapshot (th32cs_snapprocess, 0 ),
: Process32first (hsnapshot, & lpproc ),
: Process32next (hsnapshot, & lpproc)
Note: For details about function call parameters, refer to the msdn library!