• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Get Window Handle

Hi all,

I need to get the handle of the application which started my program.  Is there any way of doing this?

Cheers,

Stu.
0
Stuart_Johnson
Asked:
Stuart_Johnson
1 Solution
 
ahalyaCommented:
If you know the "Caption" and/or classname of the application then yes, you can use  hWND := FindWindow('Classname', 'Caption');

otherwise I don't think you can find out which program executed your application.
0
 
Stuart_JohnsonAuthor Commented:
Hi there.

But that won't tell me what called my application.  What I am trying to find out is, was the executing program Explorer, another one of my programs, or a program which I know nothing about.

Cheers,

Stu.
0
 
X_KaliburCommented:
hiyas,

i suppose you'd need a window hook?

that way the hook could detect when the program has been called, and which program called it.

-x
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Stuart_JohnsonAuthor Commented:
Sounds good!  How do I do it???

Stu.
0
 
MadshiCommented:
Hi Stu,

for which OS system do you need that?
BTW, all this stuff is in winObj, I asked you several times, if you're interested in newer versions, but you didn't even respond...

Regards, Madshi.
0
 
MadshiCommented:
P.S: I remember your question about something like PCAnywhere. What's happended to this question?
0
 
dotanCommented:
Here is an idea...
When your app initiates use the GetForegroundWindow to get the program that called youre app.
On your app.Free use the SetForegroundWindow and ShowWindow to restore the original app....
I tsted it and it workes.
Have fun
0
 
MadshiCommented:
Perhaps that works in most cases, but definitely not always.
0
 
Stuart_JohnsonAuthor Commented:
dotan,

I have only rejected your answer because I want the question available for other people to comment on.  I'll grade your comment later if it does what I want.

The only problem I can see with your proposal is what happens if the calling application is not the top most window?  Example:  If it is run from Scheduler.

I'll give it a go and see.

Thanks for the input.

Stu.
0
 
Stuart_JohnsonAuthor Commented:
Hi Madshi,

I'm sorry.  I didn't see anything from you about WinObj.  I have changed email addresses (which may have something to do with it).  I'll send you an email just to let you know my new address.  Sorry about that.

I'll have a look now at WinOBJ.  I have been so busy, I have only had a chance to briefly look at it.

The RA program was unfortunately shelved.  I actually got it working, but I couldn't work out how to do the XORing of the image (like you where telling me about) so it wasn't really fast, but it worked well for what we originally wanted it too do.

Anyway, on with the question at hand.  I am hoping that this will work on all version of Win32 - 9x, NT, 2000.  Can you see that being a problem?

Thanks for your help,


Stu.
0
 
Stuart_JohnsonAuthor Commented:
Madshi,

Have you got a PAQ at all for that RA question of mine?  I can't find it anywhere.

Stu.
0
 
MadshiCommented:
Hi Stu,

with winObj (at least with the current version, I don't know which version you have) you can do e.g. this:

  with GetCurrentProcess.ParentProcess do
    ShowMessage(FileName + ', id: $' + IntToHex(ID));

This shows you the filename of the executable file and the process ID of the process that has created you. If you want to know the window handle of the main window of this process, you can do this:

var wnd : dword;
begin
  with GetCurrentProcess.ParentProcess do
    if TaskbarWindows.ItemCount > 0 then
      wnd := TaskbarWindows[0].Handle
    else if Windows_.ItemCount > 0 then
      wnd := Windows_[0].Handle
    else
      wnd := 0;  // this app has no window...

Of course this works for all systems including win2000 - like everything in winObj...   :-)

You can do all this without winObj, too. In win9x you will have to enumerate all processes with the toolhelp functions. In the entry of your own process you find the id of the parent process. In NT, you will need to download Microsoft's psApi.dll and use some functions of it. (winObj doesn't need it).

P.S: No, there's no paq for that RA question. I've only written comments, so it was most probably automatically deleted because it was idle.

Regards, Madshi.
0
 
Stuart_JohnsonAuthor Commented:
Madshi,

Can you give me a download link so I can get the new version?  I thought I had your email address, but I don't.  I have it in my organiser, but that's at work.  My new email address is stuart@eep.com.au (if you want to add me back into your mailing list for WinOBJ).

Cheers,

Stu.
0
 
MadshiCommented:
Hi Stu, it's not downloadable yet (cause I still don't know whether I'll sell it or not). I've mailed it to you.

Regards, Madshi.
0
 
MadshiCommented:
Now I got only a C grade because of "auto grading"...   :-((
0
 
Stuart_JohnsonAuthor Commented:
Oh ****!  Sorry Madshi!  I'll reopen the question again (I'll do it again) because I still haven't had a chance to have a look at it.

This autograding is crap.  It's OK when it happens to someone else ;)

Sorry mate.  Keen an eye open for the new question.


Stu.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now