Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:42:01+00:00 2026-06-05T10:42:01+00:00

I have Windows Forms Application written on Delphi 7 and C++ .dll written using

  • 0

I have Windows Forms Application written on Delphi 7 and C++ .dll written using MFC.

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.

Let me describe my approach first. I register simple message in my Delphi application like:

WM_MSG := RegisterWindowMessage('WM_MSG');

and do the same at the library part:

UINT nMsgID = RegisterWindowMessage(_T("WM_MSG"));

This is OK: I can see same values on both sides when debugging.

My library function looks like this (just a dummy example to test progress bar):

extern "C" __declspec(dllexport) int MyFunction() {  
  UINT nMsgID = RegisterWindowMessage(_T("WM_MSG"));
  HWND hWnd = FindWindow(NULL, "Form1");
  if (hWnd > 0)
    for (int i = 0; i < 100000; i++) {
      int param = ceil(100 * (double) i / (double) 100000);
      PostMessage(hWnd, nMsgID, param, NULL);
    }
  return 1;
}

Executable OnMessage event:

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;

C++ function call from executable:

procedure TMyFunctionDLL.Execute;
var
  i: Integer;
  tHWND: HWND;
begin
  tHWND := FindWindow(nil, 'mainF');
  i := Func;
end;

First problem is that tHWND and hWnd variables values are inexplicably different. After some research I’ve discovered 3 situations:
1. Negative or positive huge hWnd
2. Zero hWnd
3. Undefined (‘???’)

In all cases variable hWnd is marked as unused 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.

So, there are 2 questions:
1. why are hWnd values are always different and why are they ‘unused’?
2. how can I force my main application to work correctly with progressbar?

I’ve been using different approaches to resolve this. Such as passing Application.Handle or Form1.Handle 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 ::FindWindow() and ::PostMessage() instead of FindWindow() and PostMessage() (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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:42:03+00:00Added an answer on June 5, 2026 at 10:42 am

    In addition to what others have stated, a better design would be to have the EXE pass its HWND into the DLL directly, then the DLL does not have to go hunting for it. This has the added benefit that the EXE can then decide which HWND the DLL should post its messages to. I would use AllocateHWnd() to create a dedicated window for that.

    Try this:

    UINT nMsgID = RegisterWindowMessage(_T("WM_MSG")); 
    
    extern "C" __declspec(dllexport) int __stdcall MyFunction(HWND hWnd) {   
        if ((nMsgID != 0) && (hWnd != NULL)) {
            for (int i = 0; i < 100000; i++) { 
                int param = ceil(100 * (double) i / (double) 100000); 
                PostMessage(hWnd, nMsgID, param, 0); 
            } 
        }
        return 1; 
    } 
    

    .

    unit Unit1;
    
    interface
    
    ...
    
    var
      DllWnd: HWND = 0;
    
    implementation
    
    var
      WM_MSG: UINT = 0;
    
    procedure TForm1.FormCreate(Sender: TObject); 
    begin 
      DllWnd := AllocateHWnd(DllWndProc);
    end; 
    
    procedure TForm1.FormDestroy(Sender: TObject); 
    begin 
      if DllWnd <> 0 then
      begin
        DeallocateHWnd(DllWnd);
        DllWnd := 0;
      end;
    end; 
    
    procedure TForm1.DllWndProc(var Message: TMessage); 
    begin 
      if (Message.Msg = WM_MSG) and (WM_MSG <> 0) then 
        ProgressBar1.Position := Message.WParam
      else
        Message.Result := DefWindowProc(DllWnd, Message.Msg, Message.WParam, Message.LParam); 
    end; 
    
    ...
    
    initialization
      WM_MSG := RegisterWindowMessage('WM_MSG');     
    
    end.
    

    .

    uses
      Unit1;
    
    function DllFunc(Wnd: HWND): Integer; stdcall; external 'My.dll' name 'MyFunction'; 
    
    procedure TMyFunctionDLL.Execute;   
    var   
      i: Integer;   
    begin   
      i := DllFunc(DllWnd);   
    end;   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF 4.0 application written in C# and am currently using System.Windows.Forms.Help.ShowHelp()
I have an application that is written in VB.NET, using the System.Windows.Forms.Form as the
I have written a C# Windows Forms application, not a service (it is only
I have written a small .net Windows Forms application. And now I decided to
I have a Windows forms application written in VB.NET where I host multiple WebBrowser
This is about a .NET Windows Forms application written in C#. I have a
I have written a Windows Forms application and now I want to write some
I have written a Chat application using C#. The project is a separate Windows
I have an application written in Delphi 6 and compiled on Windows XP. Usually
I have a Windows Forms application written in C#. I have to make my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.