Solved

Windows Messages in a Windows Service

Posted on 2006-10-25
6
149 Views
Last Modified: 2010-04-01
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
Comment
Question by:gsimonds
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17809743
>>>> 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
 
LVL 86

Expert Comment

by:jkr
ID: 17813147
>>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
 

Author Comment

by:gsimonds
ID: 17886494
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 17887292
>>>> 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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

689 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