[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Windows Messages in a Windows Service

I am trying to convert a C++ .NET application to a service.
The application uses Windows Messages (PostMessage etc) to send messages between threads and the GUI.

I know that if I mark the service as "Allow serive to interact with desktop" then I can get the GUI and the Windows Messages will probably work, but what happens to the app's windows before a user logs in to Windows?

Essentially I want the app to work before a user logs in and preferably without seeing the GUI, can I achieve this?

Thanks
0
gsimonds
Asked:
gsimonds
  • 2
1 Solution
 
itsmeandnobodyelseCommented:
>>>> uses Windows Messages (PostMessage etc) to send messages
>>>> between threads and the GUI.

Why do you want to send messages to the GUI while it is not available?

What kind of messages are that and why can't you write them to somewhere else, e. g. to a log file?

Regards, Alex



0
 
jkrCommented:
>>Essentially I want the app to work before a user logs in and preferably without seeing the GUI, can I achieve
>>this?

Yes. Please be more specific on what you are trying to accomplish, though.
0
 
gsimondsAuthor Commented:
I have a long running thread that receives a stream off the network.   I assume that there will be another thread to receive messages from the Windows service manager.  The second thread needs to be able to communicate with the first.

There will be no GUI and no windows.

The way to do this might be to use WaitForMultipleEvents and setevent.
0
 
itsmeandnobodyelseCommented:
>>>> The second thread needs to be able to communicate with the first

If you have threads - and not processes (services) - they share global (process) memory where you can base a communication on:

// global message queue
std::vector<std::string> msgqueue;
CRITICAL_SECTION cs;

int  service_main(...)
{
    InitializeCriticalSection(&cs);

    // create threads
    ...

    return 0;
}

void thread1Func(void* param1)
{
     ...
     while (true)
     {
            ...
            // send message
            EnterCriticalSection(&cs);
            msgqueue.push_back("any message");
            LeaveCriticalSection(&cs);
            ...
     }
};

void thread2Func(void* param2)
{
    ...
     while (true)
     {
            ...
            // receive messages
            EnterCriticalSection(&cs);
            for (int i = 0; i < msgqueue.size(); ++i)
            {
                 std::string msg = msgqueue[i];
                 // handle msg
                 ...
            }
            // make queue empty
            msgqueue.resize(0);
            LeaveCriticalSection(&cs);
            ...
     }
};

Regards, Alex

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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