Link to home
Start Free TrialLog in
Avatar of Nick_72
Nick_72

asked on

Why doesn't this work?

I get the message 'before GetMessage' printed, but nothing happens after that. I tried to print a line just after the call to GetMessage() too, but it never showed. What can be wrong?

Many thanks.

void CheckHotKey (void* dummy)
{
   MSG msg;

    if (!RegisterHotKey(NULL, 100, MOD_ALT, 'D'))
    {
        LogError("RegisterHotKey failed");
    }

   while (true)
   {
         LogError("before GetMessage");
         int res = GetMessage (&msg, NULL, 0, 0);

        if ( msg.message == WM_HOTKEY )
        {
            LogError("hotkey pressed!");
            WORD wModifier = LOWORD (msg.lParam);
            WORD wVKCode = HIWORD (msg.lParam);

            if ( (wVKCode == 'D')
             && (wModifier == MOD_ALT) )
            {
                    MessageBox(0, "worked", "", MB_OK);
            }
                  }
   }
}


// ***************************************************************

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
      _beginthread(CheckHotKey, 0, NULL);
      return 1;
}


btw, I do other stuff in the main thread after I have started the CheckHotKey thread. That is why it gets a thread of its own.
Avatar of Mercantilum
Mercantilum
Flag of Japan image

As the doc says

<< The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure. This function can retrieve both messages associated with a specified window and thread messages posted via the PostThreadMessage function. The function retrieves messages that lie within a specified range of message values. ***GetMessage does not retrieve messages for windows that belong to other threads or applications.  >>

Your main thread should GetMessage() and send them to your CheckHotKey thread.

Regards

Avatar of Nick_72
Nick_72

ASKER

Well that didn't make me any wiser, you'll have to excuse me, I'm a rookie when it comes to c++/mfc.

>>The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure.

I thought the calling thread was the CheckHotKey thread since it is that thread that calls RegisterHotKey..?

>>GetMessage does not retrieve messages for windows that belong to other threads or applications

What does that have to do with the CheckHotKey thread? That's the only thread I need to receive the messages in.

I can't (atleast I don't know how) call GetMessage in the main thread since it starts a service, and after the service is started, it calls

WaitForSingleObject(event, INFINITE);

which occupies that thread, so that is why I have to do the message handling in another thread.
Yes CheckHotKey is the thread calling GetMessage().

But unfortunately each thread has a message queue, and windows messages are sent to the main one (the one which returns immdiately...).

So your CheckHotKey thread receives   no  message - it waits forever...


Here is a sample of code where main thread communicates with its "child" thread.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&threadm=19980121001800.TAA03837%40ladder02.news.aol.com&rnum=1&prev=/groups%3Fq%3Dgetmessage%2B_beginthread%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26safe%3Doff%26selm%3D19980121001800.TAA03837%2540ladder02.news.aol.com%26rnum%3D1

Here some explanations about thread programming and make them waiting events

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&threadm=RUBh5.4%240W4.307%40newsread2.prod.itd.earthlink.net&rnum=3&prev=/groups%3Fq%3Dgetmessage%2B_beginthread%2Bservice%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26safe%3Doff%26selm%3DRUBh5.4%25240W4.307%2540newsread2.prod.itd.earthlink.net%26rnum%3D3

In this particular case (a service) have a look to

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&threadm=btkdem%248dj3l%241%40ID-103400.news.uni-berlin.de&rnum=6&prev=/groups%3Fq%3Dservice%2Bmessage%2Bloop%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26safe%3Doff%26selm%3Dbtkdem%25248dj3l%25241%2540ID-103400.news.uni-berlin.de%26rnum%3D6


To my mind (i'm not much familiar with services) you have to create a window in your thread and listen to the messages from that window which will have a message loop.


regards
I wonder how do you get 'before GetMessage' because main thread exits immidiately and program exits also.
Try the same in main thread.
Avatar of Nick_72

ASKER

AlexFM:

I think you might have missed my last comment in the first post:

>>btw, I do other stuff in the main thread after I have started the CheckHotKey thread. That is why it gets a thread of its own.

That means, I install a service, or run the service if it's already installed. However, the main thread gets stalled when it calls WaitForSingleObject() in the service main function. That is why I started another thread to do the message handling part.

I have tried to do it in the main thread but without success...

Mercantilum:

I tried the code in a 'non-service' exe, and it worked just fine. I guess the message is sent to the thread that calls RegisterHotKey() after all.

I really don't know how to continue with this, maybe I'll have to abandon the 'getting-messages-in-a-service' idea...

/Nick
Your service was created as "interactive" I suppose

When you tested your program as a non-service did you keep the same structure ?

main ()
{
   ...
    _beginthread(CheckHotKey, 0, NULL);
   ....

    return 1;
}

I.e. the CheckHotKey was running as a thread and not a simple function ?
Avatar of Nick_72

ASKER

basically yes, except that I added

Sleep(INFINITE);

after

_beginthread(CheckHotKey, 0, NULL);

to keep it from exit.
ASKER CERTIFIED SOLUTION
Avatar of Mercantilum
Mercantilum
Flag of Japan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nick_72

ASKER

Well, it was not what I was looking for, but you should have the points since you made a good effort :)