Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Postmessage and dll

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

597 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