Unity and WM_QUIT


I’m using XYNTService to run one of my application as a windows service.

It works just fine when I launch the service, as my application starts running in background.

When I stop the service, XYNTService send a message WM_QUIT. However, Unity doesn’t react to it.

I found this workaround :

private const UInt32 WM_QUIT           = 0x0012;
const Int32 PM_REMOVE = 1;

struct POINTAPI {
  public Int32 x;
  public Int32 y;

struct MSG {
  public Int32 hwnd;
  public Int32 message;
  public Int32 wParam;
  public Int32 lParam;
  public Int32 time;
  public POINTAPI pt;

[DllImport( "user32.dll", SetLastError = true )]
private static extern bool  PeekMessage(ref MSG lpMsg,
                                        Int32 hwnd,
                                        Int32 wMsgFilterMin,
                                        Int32 wMsgFilterMax,
                                        Int32 wRemoveMsg);

void Update() {
	MSG msg = new MSG();
    if ( PeekMessage( ref msg, 0, 0, 0, PM_REMOVE ) ) {
      	if ( msg.message == WM_QUIT ) {
			Debug.Log("Message : " + msg.message);