Solved

Postmessage and dll

Posted on 2003-10-21
7
2,895 Views
Last Modified: 2012-06-21
Hi

I have a weird problem I think, but first a littel about the setup:

I use Delphi 5, uses packages VCL50;Vclx50;te_d5 (the last is theme engine, but that shouldn't matter)
I have a exe and a dll.

I the dll I place this code. The code is about sending or posting a message to a window created by AllocatehWnd which then will call a procedure. It works fine when I use SendMessage, but when I use PostMessage the message never arrives, why?

If I use the code in the exe, everything is working fine, what is the difference, I thought it would be the same when I use packages.


type
    TProcedure = procedure;
    TTest = class(TObject)
    private
        SyncLock: TRTLCriticalSection;
        hMsgWnd: THandle;
        SyncP: TProcedure;
        procedure WndMessages(var Message: TMessage);
    public
        constructor Create;
        destructor Destroy; override;
        procedure Call1(P: TProcedure);        //Works
        procedure Call2(P: TProcedure);        //Don't work ??!
    end;

implementation

const
    WM_SYNCHRONIZE = WM_USER;

constructor TTest.Create;
begin
    InitializeCriticalSection(SyncLock);
    hMsgWnd := AllocatehWnd(WndMessages);
end;

destructor TTest.Destroy;
begin
    DeleteCriticalSection(SyncLock);
    DeallocatehWnd(hMsgWnd);
    inherited;
end;

procedure TTest.Call1(P: TProcedure);
begin
    EnterCriticalSection(SyncLock);
    SyncP := P;
    Windows.SendMessage(hMsgWnd, WM_SYNCHRONIZE, 0, 0);
end;

procedure TTest.Call2(P: TProcedure);
begin
    EnterCriticalSection(SyncLock);
    SyncP := P;
    Windows.PostMessage(hMsgWnd, WM_SYNCHRONIZE, 0, 0);
end;

procedure TTest.WndMessages(var Message: TMessage);
begin
    if Message.Msg = WM_SYNCHRONIZE then
    try
        SyncP;
    finally
        LeaveCriticalSection(SyncLock);
    end
    else
        Message.Result := DefWindowProc(hMsgWnd, Message.Msg, Message.wParam, Message.lParam);
end;



procedure CallProc;
begin
    messagebox(0,'ok','ok',0);
end;

var
    T: TTest;
procedure TestIt;
begin
    T := TTest.Create;
    T.Call1(CallProc);  //Works, uses sendmessage
    T.Call2(CallProc);  //Don't works, uses postmessage
end;
0
Comment
Question by:koger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Accepted Solution

by:
gmayo earned 30 total points
ID: 9591479
Maybe it has something to do with the critical sections and order of processing. If you use sendmessage, the following happens:
1. Enter CS
2. Send message
2a. app receives message and handles it
3. Leave CS

But when you do PostMessage the order may be different:
1. Enter CS
2. Post message
3. Leave CS
and then
4. App receives message (or doesn't in your case)

I don't know what you are trying to achieve, but have a look at SendMessageTimeout too.

Geoff M.
0
 

Assisted Solution

by:OpenSourceDeveloper
OpenSourceDeveloper earned 30 total points
ID: 9591910
the difference is that SendMessage waits for the target to receive the message and PostMessage adds it the message queue and returns immediately. you should only be using SendMessage for intra/interprocess communication.
0
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 30 total points
ID: 9592858
hello koger, there may or may not be a reason for this difference between the postMessage and SendMessage behavior, there are several descriptions given in the API documetation (Help) about the differences between Post and Send Message, And it mostly gives a "General" notice about when a PostMessage may not work (If the message queue is full). . . . But I have used both many times, and SendMessage, usually Always works, but PostMessage will work on some things and Not Work on other things, but I could not find any "Logic" or Connection of when it will not work. . . for me, I test it and if it don't go, then I change to SendMessage. .  However, I have not heard of a way to make it work if it does not. . . .
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 30 total points
ID: 9598404
It might be related to the message being send to the Application object of the executable but not to the application object of the DLL. Or something related to this.
Maybe look at the SendMessageCallback() which is almost similar to the PostMessage function.
0
 
LVL 6

Assisted Solution

by:swift99
swift99 earned 30 total points
ID: 9625041
SendMessage is defined by M$oft to wait for the method handling the message to retrn before returning control to the application.

Postmessage places the message in the target's message queue and returns immediately.   The target thread will handle the message in its own time.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to save the image in the .cds File ClientDataSet? 1 41
shape, triangle, dbctrlgrid 3 44
IP without any Dots 1 95
Connect to SQL 2008 r2 server over the Internet 4 75
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question