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.
LVL 6
Stuart_JohnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.