[Reprinted] Mutual acquisition of processes, windows, executable file names

Source: Internet
Author: User

[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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.