How to handle a posted, registered windows message within a VCL application?
<p>I have some low level library code that I would like to be able to broadcast a few custom windows messages. </p>

<p>In the library code, the following is defined:</p>

<pre><code>static UINT WM_MOTOR_WARNING_MESSAGE = 0;

extern "C" int _libmain(unsigned long reason)
{
  WM_MOTOR_WARNING_MESSAGE = RegisterWindowMessage("MOTOR_WARNING_MESSAGE");
....
</code></pre>

<p>and the library is sending the message like this:</p>

<pre><code>        //Send windows message
        int ret = PostMessage(HWND_BROADCAST, WM_MOTOR_WARNING_MESSAGE, 0, 0);
        if(!ret)
        {
            Log(lError) &lt;&lt; "Post message failed..";
        }
</code></pre>

<p>The VCL Main form defines </p>

<pre><code>UINT WM_MOTOR_WARNING_MESSAGE = RegisterWindowMessage(L"MOTOR_WARNING_MESSAGE");
</code></pre>

<p>and an over ridden WndProc function:</p>

<pre><code>void __fastcall TMain::WndProc(TMessage&amp; Message)
{
    if (Message.Msg == WM_MOTOR_WARNING_MESSAGE)
    {
        MessageDlg("Turn off motor", mtInformation, TMsgDlgButtons() &lt;&lt; mbOK, 0);
    }
    else
    {
        TForm::WndProc(Message);
    }
}
</code></pre>

<p>The current problem seem to be that the library code, residing in a DLL, is loaded by the main application, causing the RegisterWindowMessage function to return 0 in the library. It seems you cannot have two RegisterWindowMessage calls for the same message within a single application.</p>

<p>So question is how to deal with this scenario? Although the Main application is using this DLL, there are other applications that could handle the library message.    </p>