Article View: borland.public.cppbuilder.nativeapi
Article #13301Re: ShowWindow question
From: Remy Lebeau (TeamB)
Date: Sun, 19 Oct 2003 22:30
Date: Sun, 19 Oct 2003 22:30
79 lines
3516 bytes
3516 bytes
"Martin C" <martinc@aol.com> wrote in message
news:3f91c244@newsgroups.borland.com...
> Thank you for your reply, actually all I was doing was trying
> to get the exe file window handle based on the exe name
The code you current have will not do that. You are locating and returning
a process handle, not a window handle. The two are not the same thing. You
only have half of the equation. Once you have the process ID (given by
Proc.th32ProcessID), you then need to enumerate the available windows
separately, using GetWindowThreadProcessId() to find HWND handles that
belong to the found process ID.
BTW, your process searching code will not work under WinNT, the ToolHelp
functions are not available. Have a look at the following article for
information on how to enumerate processes under all Windows OSs:
HOWTO: Enumerate Applications Using Win32 APIs
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q175030
With that article's enumeration code, you can then do something like the
following (may need further tweaking to support applications with multiple
windows):
struct TProcessInfo
{
char *pName;
DWORD dwProcessId;
HWND hWnd;
};
BOOL CALLBACK FindExeWindow(HWND hwnd, LPARAM lParam)
{
TProcessInfo *info = reinterpret_cast<TProcessInfo*>(lParam);
DWORD dwProcessId;
GetWindowThreadProcessId(hwnd, &dwProcessId);
if( dwProcessId == info->dwProcessId )
{
info->hWnd = hwnd;
return FALSE; // stop enumerating
}
return TRUE; // keep enumerating
}
BOOL CALLBACK FindExeProcessInfo(DWORD dw, WORD w16, LPCSTR lpstr,
LPARAM lParam)
{
TProcessInfo *info = reinterpret_cast<TProcessInfo*>(lParam);
if( AnsiSameText(lpstr, info->pName) )
{
info->dwProcessId = dw;
EnumWindows(reinterpret_cast<WNDENUMPROC>(FindExeWindow),
lParam);
return FALSE; // stop enumerating
}
return TRUE; // keep enumerating
}
With that in place, you can then do the following:
TProcessInfo info = {0};
info.pName = "notepad.exe";
EnumProcs(reinterpret_cast<PROCENUMPROC>(FindExeProcessInfo),
reinterpret_cast<LPARAM>(&info));
if( info.dwProcessId != 0 )
{
// use info.hWnd as needed
}
Gambit
Technical Details
Message-ID:
<3f93100d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3f90a515@newsgroups.borland.com> <srq1pvg3emp51jq97kjt2tkaqv8uoa5cpm@4ax.com> <3f91b0b3@newsgroups.borland.com> <pee3pv8pp2m367s11uq91tmls2fl4alf8c@4ax.com> <3f91c244@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,644d4ac8e2f18824 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-10-19 15:39:08 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f90a515@newsgroups.borland.com> <srq1pvg3emp51jq97kjt2tkaqv8uoa5cpm@4ax.com> <3f91b0b3@newsgroups.borland.com> <pee3pv8pp2m367s11uq91tmls2fl4alf8c@4ax.com> <3f91c244@newsgroups.borland.com> Subject: Re: ShowWindow question Date: Sun, 19 Oct 2003 15:30:17 -0700 Lines: 82 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 67.22.137.75 Message-ID: <3f93100d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1066602509 67.22.137.75 (19 Oct 2003 15:28:29 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail