Home All Groups Group Topic Archive Search About

SendMessage vs. PostMessage

Author
19 Mar 2006 3:27 PM
DanS
Hello all,

I'm closing applications from my app.

These are the declarations.

Private Declare Function SendMessage Lib "user32" _
                Alias "SendMessageA" (ByVal hWnd As Long, _
                ByVal wMsg As Long, ByVal wParam As Long, _
                lParam As Any) AsLong
Private Declare Function PostMessage Lib "user32" _
                (ByVal hWnd As Long, ByVal wMsg As Long, _
                ByVal wParam As Long, lParam As Any) As Long

Is there any reason that using PostMessage, the applications will not close
but if I use SendMessage it will ?

This works:   SendMessage iHwnd, WM_CLOSE, 0&, 0&

This doesn't: PostMessage iHwnd, WM_CLOSE, 0&, 0&


TIA,

DanS

Author
19 Mar 2006 5:03 PM
DanS
DanS <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in
news:Xns978B6A7FDC136idispcom@216.196.97.142:

Dammit. I didn't see it until I read this post.

Bad Declare.



Show quoteHide quote
> Hello all,
>
> I'm closing applications from my app.
>
> These are the declarations.
>
> Private Declare Function SendMessage Lib "user32" _
>                    Alias "SendMessageA" (ByVal hWnd As Long, _
>                    ByVal wMsg As Long, ByVal wParam As Long, _
>                    lParam As Any) AsLong
> Private Declare Function PostMessage Lib "user32" _
>                    (ByVal hWnd As Long, ByVal wMsg As Long, _
>                    ByVal wParam As Long, lParam As Any) As Long
>
> Is there any reason that using PostMessage, the applications will not
> close but if I use SendMessage it will ?
>
> This works:   SendMessage iHwnd, WM_CLOSE, 0&, 0&
>
> This doesn't: PostMessage iHwnd, WM_CLOSE, 0&, 0&
>
>
> TIA,
>
> DanS
>
Author
20 Mar 2006 8:15 AM
J French
On Sun, 19 Mar 2006 11:03:53 -0600, DanS
<t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote:

>DanS <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in
>news:Xns978B6A7FDC136idispcom@216.196.97.142:
>
>Dammit. I didn't see it until I read this post.
>
>Bad Declare.

I don't see that.

You are invoking both incorrectly :-

SendMessage iHwnd, WM_CLOSE, 0&,  ByVal 0&   <---

The ByVal is essential as otherwise you are sending the address of a
Long containing 0&

PostMessage and SendMessage are really the same thing (well for Apps
in different Processes)

PostMessage expects no return result so it returns immediately
SendMessage expects a return result, so your App waits until the other
App has processed the Message.

Within the same App SendMessage behaves just like a Call, I don't
think it ever really gets put on the App's Message Queue

PostMessage definitely gets shoved on the Message Queue and is not
processes until you do an explicit or implicit DoEvents.
Author
20 Mar 2006 12:26 PM
DanS
erew***@nowhere.uk (J French) wrote in news:441e620d.508798062
@news.btopenworld.com:

> On Sun, 19 Mar 2006 11:03:53 -0600, DanS
><t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote:
>
>>DanS <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in
>>news:Xns978B6A7FDC136idispcom@216.196.97.142:
>>
>>Dammit. I didn't see it until I read this post.
>>
>>Bad Declare.
>
> I don't see that.

I don't know where the PostMessage declare came from originally, I would
have thought it was from the API viewer, but there was no Alias in it.
Author
20 Mar 2006 1:30 PM
J French
On Mon, 20 Mar 2006 06:26:26 -0600, DanS
<t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote:

<snip>

>I don't know where the PostMessage declare came from originally, I would
>have thought it was from the API viewer, but there was no Alias in it.

I did not spot that  -  it should have crunched

There are bad declares in at least one of the AllAPI examples, but
mostly they are solid
Author
21 Mar 2006 12:23 PM
DanS
erew***@nowhere.uk (J French) wrote in news:441eac6b.527838706
@news.btopenworld.com:

> On Mon, 20 Mar 2006 06:26:26 -0600, DanS
><t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote:
>
><snip>
>
>>I don't know where the PostMessage declare came from originally, I would
>>have thought it was from the API viewer, but there was no Alias in it.
>
> I did not spot that  -  it should have crunched
>
> There are bad declares in at least one of the AllAPI examples, but
> mostly they are solid
>

I did find another, I can't remeber what it was, but it was wrong in both
the VB API AND API Viewer 2004.
Author
21 Mar 2006 1:17 PM
J French
On Tue, 21 Mar 2006 06:23:31 -0600, DanS
<t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote:

<snip>

>I did find another, I can't remeber what it was, but it was wrong in both
>the VB API AND API Viewer 2004.

The All API example contains this :-

Private Declare Function SendMessage _
           Lib "user32" _
           Alias "SendMessageA" _
           (ByVal hwnd As Long, _
            ByVal wMsg As Long, _
            ByVal wParam As Integer, _   <----- EH ? ----
            ByVal lParam As Any) As Long