Solved

Windows message onActivateApp

Posted on 2004-09-16
10
364 Views
Last Modified: 2007-12-19
Hi all,
I have a problem that I can't work out.
I've created a DLL to run a program that was written in pascal.
What I want to do is send a message to another window when the pascal window becomes active. Basically its like this:
Win1 -> create pascal window through DLL
Win2 -> If activated send message to Win1

I know the handles of both Win1 and Win2. Win1 can only communicate with Win2 through the DLL (I know this isn't really the case, but for the sake of my problem it is).

I have added a message handler to the pascal window that looks like this.
[code]
procedure TMainForm.OnMessageHandler;
begin

  if (Msg.message = WM_ACTIVATEAPP) then
  begin
        if Msg.wParam > 0 then
        begin
         if its becoming activated send message to other window to come to front
           SetWindowPos(HWND_1,HWND_TOP,NULL,NULL,NULL,NULL,NULL);
            MessageBeep(0);
                        MessageBeep(0);
                                    MessageBeep(0);
                                                MessageBeep(0);
        end;
  end;
[/code]

The message beeps are in there because I can't get it to work.
Note that if I add a handler for WM_PAINT it beeps loads, so the function is definately working and hooking the messages.

As it stands though, the window never beeps hence the WM_ACTIVATEAPP is never being sent. Is this becuase its running through a DLL?

Any help would be gratefully received. I've been chugging away on this for ages.

Cheers
DRB2k2
0
Comment
Question by:drb2k2
  • 4
  • 3
10 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12073028
MSDN says:
---------------------------------------------------------------------------------------------------
The WM_ACTIVATEAPP message is sent when a window belonging to a different application than the active window is about to be activated. The message is sent to the application whose window is being activated and to the application whose window is being deactivated.
---------------------------------------------------------------------------------------------------

That means you only get it when a user switches between two different window applications or if you activate programmatically an already running Winddows application that wasn't activated before (didn't have the focus).

If your DLL starts the pascal application (e. g. using CreateProcess or ShellExecute) you should check for WM_CREATE or (first) WM_PAINT message or - may be best - for a user defined message that you have to register in both applications.

Regards, Alex
 

0
 

Author Comment

by:drb2k2
ID: 12073127
ok sounds good,
but how do I define a message that responds to the application being activated. I want this message to happen every time the user clicks on the DLL created window. i.e. they could click on another application, then click back on the DLL at any point.  

I don't think I can use WM_PAINT, as this is called when the window is not the one with the main focus. I really only want to call this when the Pascal program (run through the dll) is the window getting activated.

Thanks for the help so far.

Cheers
DRB2k2
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12073305
You have to do both, check for WM_ACTIVATEAPP to handle the situation where both programs are running and the user switches between applications *and* have an own message (or messages) that could be sent/received between both applications to handle all necessary cases. For user defined messages you should use RegisterWindowMessage on both apllications to get a unique message id.

Not, you should get WM_ACTIVATEAPP message both for activating and deactivating. So, if the C++ prog gets deactivated it could send a private message to the other application and tell it. If the pascal prog gets activated then it could send another message back, and so on. So both progs may 'know' their current status. Don't forget to send appropriate messages if an application get stopped (or at least ask if valid window handle before sending messages).

Regards, Alex
 
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12073442
"Not,"

   should be

"Note,"
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:drb2k2
ID: 12073702
ok,
two problems,
First I can't find anything about RegisterWindowsMessage it doesn't appear to be in MSDN and Borlan 6 doesn't seem to know what it is.

Secondly, I tried sending messages before from the pascal event handler when it was WM_ACTIVATEAPP'd the problem is that the pascal program never seems to get these messages. I would guess that instead they are being sent to the DLL. But even so you would expect a message to be sent  to the app. It doesn't even seem to get WM_SIZING or WM_MOVING (or WM_SIZE OR WM_MOVE) messages either.

If I can find out where these messages are going then I can do what I originally intended and itsme suggested of just monitoring the messages between the two apps.

phew

I think my main problem is understanding why when I have the handle to the pascal window it does not respond to sendmessage or postmessage.

Cheers
DRB2k2
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12073900
>>>> First I can't find anything about RegisterWindowsMessage

In MSDN coming with VC6 it is defined. Should be declared in winuser.h that is atomatically included with windows.h. You don't need registering message ids if you take a message number from WM_APP + 0x0100 til WM_APP + 0x0200 as it is very unlikely that other applications will use same id by accident.

>>>> that the pascal program never seems to get these messages

I don't think so. your pascal program maybe handles the messages elsewhere. that message is sent to the main (frame) window currently active (as far as i know). Check all your message target classes to find out which it is.

>>>> I would guess that instead they are being sent to the DLL

No, messages always go to windows (even in CWinThread of MFC there is a pseudo window for that). But sometimes these messages get filtered before by a baseclass handler, e. g. by accelarators.

>>>> WM_SIZE OR WM_MOVE

Same applies to these messages although they are only sent if the frame is sizeable resp. the window is moveable.

Unfortunately i know less to nothing of Borland 6 and Pascal. But you could check if there is something like function

     CWnd::PreTranslateMessage

of MFC, where *all* messages can be checked *before* they are handled or targeted elsewhere.

Regards, Alex




0
 

Author Comment

by:drb2k2
ID: 12074751
ok,
its making more sense now, but surely if I send a window a message directly through its handle it should get it.
I understand that it could pass it on to a main window, but it should still pass through the current message handler surely.
Cheers
DRB2k2
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12582228
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now