title: Problems with PostMessage from C++ .dll to Delphi Forms application
body: <p>I have Windows Forms Application written on Delphi 7 and C++ .dll written using MFC.</p>
  
  <p>Currently I'm trying to implement basic message posting from .dll to main executable to show user calculation process on progressbar, but several problems were faced.</p>
  
  <p>Let me describe my approach first. I register simple message in my Delphi application like:</p>
  
  <pre><code>WM_MSG := RegisterWindowMessage('WM_MSG');
  </code></pre>
  
  <p>and do the same at the library part:</p>
  
  <pre><code>UINT nMsgID = RegisterWindowMessage(_T("WM_MSG"));
  </code></pre>
  
  <p>This is OK: I can see same values on both sides when debugging.</p>
  
  <p>My library function looks like this (just a dummy example to test progress bar):</p>
  
  <pre><code>extern "C" __declspec(dllexport) int MyFunction() {  
    UINT nMsgID = RegisterWindowMessage(_T("WM_MSG"));
    HWND hWnd = FindWindow(NULL, "Form1");
    if (hWnd &gt; 0)
      for (int i = 0; i &lt; 100000; i++) {
        int param = ceil(100 * (double) i / (double) 100000);
        PostMessage(hWnd, nMsgID, param, NULL);
      }
    return 1;
  }
  </code></pre>
  
  <p>Executable <strong>OnMessage</strong> event:</p>
  
  <pre><code>procedure TForm1.OnMessageEvent(var Msg: tagMSG; var Handled: Boolean);
  begin
    Handled := True;
    if Msg.message = WM_MSG then
      ProgressBar1.Position := Msg.wParam
    else Handled := False;
  end;
  </code></pre>
  
  <p>C++ function call from executable:</p>
  
  <pre><code>procedure TMyFunctionDLL.Execute;
  var
    i: Integer;
    tHWND: HWND;
  begin
    tHWND := FindWindow(nil, 'mainF');
    i := Func;
  end;
  </code></pre>
  
  <p>First problem is that <strong>tHWND</strong> and <strong>hWnd</strong> variables values are inexplicably different. After some research I've discovered 3 situations:
  1. Negative or positive huge <strong>hWnd</strong>
  2. Zero <strong>hWnd</strong>
  3. Undefined ('???')</p>
  
  <p>In all cases variable <strong>hWnd</strong> is marked as <em>unused</em> and I don't know what does that mean. The most interesting thing is that code DOES work if I test it in very simple Delphi form (with only one unit). That simple Delphi form works well with my real C++ .dll code where real data is calculated. But when I use my general Delphi application (many units but still one form) it seems main application OnMessage event doesn't catch any events from C++ dll.</p>
  
  <p>So, there are 2 questions:
  1. why are <strong>hWnd</strong> values are always different and why are they 'unused'?
  2. how can I force my main application to work correctly with progressbar?</p>
  
  <p>I've been using different approaches to resolve this. Such as passing <strong>Application.Handle</strong> or <strong>Form1.Handle</strong> as function parameter to C++ library. None of them worked not even saying about parameter value changed while passing (I guess that should be separate question). Also I've tried using <strong>::FindWindow()</strong> and <strong>::PostMessage()</strong> instead of <strong>FindWindow()</strong> and <strong>PostMessage()</strong> (what is difference between them?), that didn't helped either. I'm trying to improve situtuation for whole day already but have no idea how to solve it. Help me with any ideas please.</p>