Solved

Windows Messages in a Windows Service

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 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

20 Experts available now in Live!

Get 1:1 Help Now