How to manually start a message pump in C ++

ORIGINAL QUESTION

Our application uses CSocket, which requires the message pump to work, for it to work. It is currently not practical to switch to another socket implementation, although at some point we want to finish.

The application is in Visual C ++ (NOT managed).

We are currently starting the C ++ DLL using the C # .NET launcher, which starts the thread using Application.Run to start the message pump, and then uses DllImport to start the start method in our DLL.

There are many problems associated with this, the most relevant of which is that if the DLL crashes for any reason, we do not get the dump file!

As a result of this, we move on to the C ++ launcher, and while we are fine with the service aspect, we are a bit unhappy with how to get the message pump to go.

I had a google review and some questions here, but part of my problem is the lack of basic C ++ knowledge, so apologize if this is a cheating question, if someone can point me in the right direction, which would be highly appreciated.

Thanks so much in advance Matt Pedldsden

ADDITIONAL INFORMATION

The current C # service we are trying to replace is essentially this:

public void PumpThread() { DLLStart(); Application.Run(); } protected override void OnStart(string[] args) { try { Thread pumpThread = new Thread(new ThreadStart(PumpThread)); pumpThread.IsBackground = true; pumpThread.Start(); } catch (DllNotFoundException dnfe) { } catch (Exception e) { } } protected override void OnStop() { try { DLLStop(); } catch (DllNotFoundException dnfe) { } catch (Exception e) { } } 

In fact, we are just trying to replace the above C # .NET Windows Service with the C ++ equivalent, so that our code works completely in an unmanaged world, and does not unnecessarily confuse this with 5 lines of managed code.

DLLStart () and DLLStop () are two functions that are imported from our C ++ unmanaged DLL, which actually starts and stops the system.

I'm not quite sure what Visual C ++ project it should be in order to be able to do anything with a pump, to be honest.

We hope that this additional data will be useful.

+4
source share
3 answers

My translated C ++ code is below (not checked)

PS: Calling DLLStart, DLLStop in the same thread will be more reliable.

Global variable contains threadID
DWORD threadID = 0;

In the equivalent of OnStart:
CreateThread(0, 0, PumpThread, 0, 0, 0);

In the equivalent of OnStop:
PostThreadMessage(threadID, WM_QUIT, 0, 0);

Flow procedure:

 DWORD WINAPI PumpThread(void* param) { threadID = GetCurrentThreadId(); DLLStart(); // create thread message queue PeekMessage(&msg, 0, WM_USER, WM_USER, PM_NOREMOVE); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } DLLStop(); } 
+2
source

The message loop in its simplest is as follows:

 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } 

but I'm not sure what really answers your question - you cannot just start a message loop wherever you go. Therefore, we need more details.

+3
source

You need to create a user interface thread. Workflows have no messages.

Take a look at CWinThread

+1
source

All Articles