Solved

Postmessage and dll

Posted on 2003-10-21
7
2,902 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

Industry Leaders: 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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