Solved

Windows Messages in a Windows Service

Posted on 2006-10-25
6
146 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

809 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