?
Solved

Postmessage and dll

Posted on 2003-10-21
7
Medium Priority
?
2,924 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 120 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 120 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 120 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 120 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 120 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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