Solved

Windows Messages in a Windows Service

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 281
Problem with SqlConnection 4 184
C++ mouse_event mouse look 7 112
Precision Problem in C++ 7 52
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 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