I'll try this... It might take a while, since I'm sitting at a UNIX system right now :-)
Main Topics
Browse All TopicsFrom a newsgroup I got hold on this piece of sourcecode, which is supposed to get me the window handle from a processID. Well my question is in fact quite simple. It doesn't work, but whats wrong. I know that the if statement in the callback function EnumProc() never equals true, so no handle can be returned. Unfortunatly I can't seem to udnerstand exactly what is going on. Please help
Type
TEnumData = Record
hW: HWND;
pID: DWORD;
End;
Function EnumProc( hw: HWND; Var data: TEnumData ): Bool; stdcall;
Var
pID: DWORD;
Begin
Result := True;
If (GetWindowLong(hw, GWL_HWNDPARENT) = 0) and
(IsWindowVisible( hw ) or IsIconic(hw)) and
((GetWindowLong(hw, GWL_EXSTYLE) and WS_EX_APPWINDOW) <> 0)
Then Begin
GetWindowThreadProcessID( hw, @pID );
If pID = data.pID Then Begin
data.hW := hW;
Result := False;
End; { If }
End; { If }
End; { EnumProc }
Function WindowFromProcessID( pID: DWORD ): HWND;
Var
data: TEnumData;
Begin
data.pID := pID;
data.hW := 0;
EnumWindows( @EnumProc, longint(@data) );
Result := data.hW;
End; { WindowFromProcessID }
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I think there's a difference between threadid and processid. When I find the process ID using WinSight32 and insert in instead of GetCurrentThreadId, the returned handle is 0, but if I leave GetCUrrentThreadId, then I get a handle (of cource not the one I want)
One other thing... I am having problems using a callback function. When I call EnumWindows(@EnumProc, 0) where EnumProc is:
function EmunProc(app_hnd : HWND; lparam : DWORD) : boolean;
begin
// some stuff here
Form1.Memo1.Lines.Add(IntT
end;
The memo component gets filled with the same handle a lot of times. Isn't EnumWIndows suppored to pass the handle of all top level windows in turn to the CallBack function. What am I doing wrong ??
No you need to specify which windows..... Here is an example...
function GetWinProc(Handle : HWND; lParam : LPARAM):Boolean;stdcall;
var
Name : array[0..MAX_PATH] of char;
begin
if IsWindowVisible(Handle) then begin
GetWindowText(Handle, Name, SizeOf(Name));
if not(Name = '') then
Form1.Listbox1.Items.Add(N
Result := true;
end;
end;
procedure TForm1.Button1Click(Sender
begin
ListBox1.Items.Clear; //Just to clean the listbox so it clears it before filling it again..
EnumWindows(@GetWinProc,0)
end;
This will get all visible windows, not the one that don't have names like popup menus and similar....
Hoe this helps a bit!
btw- I don't know why the processID isn't working... Gotta try it on my Delphi, and will get back to you soon.///
Regards,
Viktor Ivanov
Oh, ok.... Cool... You said you were going to a High School so I though were still there... But what I missed was that I didn't see you registered in 97 :-| Sorry... Anyway, I'm 16 and really like programming in Delphi. Actually it's my first programming language and I started programming in it a bit more than 3 months...maybe 4 months.... ago. I've learned a little C/C++ and I'm learning assmbler right now :-)
btw- What kind of stuff do you study in Computer Science....????
Regards,
Viktor Ivanov
Wow, it's been 2 years since I posted to this question, and there's a comment waiting for me now. Anyways, I don't really have time to write any code for you, but here is the principle.
You can use ShellExecute() to execute your application, and then you can use FindWindow() to track down the handle of your window. There are other more complex methods to do it, but I don't have the time, as I previously mentioned.
Regards,
Viktor
Business Accounts
Answer for Membership
by: viktornetPosted on 1998-10-17 at 12:49:21ID: 1343243
How about this code???
: TObject); ntThreadId , @GetHwnd, 0) then begin
var
DaHandle : HWND;
function GetHwnd(Handle : HWND; lParam : LPARAM) : Boolean;stdcall;
begin
DaHandle := Handle;
Result := True;
end;
procedure TForm1.Button1Click(Sender
var
Name : array [0..MAX_PATH] of char;
begin
if EnumThreadWindows(GetCurre
GetWindowText(DaHandle, Name, SizeOf(Name));//YOu can comment out this
Button1.Caption := StrPas(Name);//and this
//and use the DaHandle that has been returned by the EnumThreadWindows()
end;
end;