Discussion:
Pointer to data in PostMessage(..., LPARAM)
(too old to reply)
Dan McCarty
2005-05-24 18:14:51 UTC
Permalink
Is sending a pointer to data in the LPARAM of PostMessage() an
acceptable way to pass data to a window?

If so, is there any value to checking the size of the passed pointer in
the message handler on the receiving end? e.g.,

ASSERT(sizeof((MonetJobTp &) pNewData) == sizeof(MonetJobTp));

I'm not sure whether this checks the size of pNewData against a
MonetJobTp struct or whether it just forces pNewData to align with a
MonetJobTp struct and always passes the test.

Is it better to just ASSERT(pNewData) to ensure it's a valid pointer
and leave it at that?
Dan McCarty
2005-05-24 20:37:49 UTC
Permalink
Is there any value to checking the size of the passed pointer in
the message handler on the receiving end? e.g.,
ASSERT(sizeof((MonetJobTp &) pNewData) == sizeof(MonetJobTp));
Okay, I just ran a test and I think what I'm doing is effectively
checking foo = foo.

int foo1 = sizeof((MonetJobTp &) newData);
int foo2 = sizeof((MonetJobTp &) foo1);

Variable watch:
foo1 = 844
foo2 = 844

So I guess it's a foolish sizeof and I should just stick to
ASSERT(pNewData).
Damian Driscoll
2005-05-24 21:38:09 UTC
Permalink
Post by Dan McCarty
Is sending a pointer to data in the LPARAM of PostMessage() an
acceptable way to pass data to a window?
Only if the scope is valid (don't pass a pointer to a variable on the stack
for example) and only if you own the window. If the window belongs to
another process then your pointer will be garbage as the receiving process
has its own virtual memory. In this case, use WM_COPYDATA (in which case
you must use SendMessage instead of PostMessage) or some other appropriate
interprocess communication mechanism instead (see interprocess
communication in MSDN).
Post by Dan McCarty
If so, is there any value to checking the size of the passed pointer in
the message handler on the receiving end? e.g.,
ASSERT(sizeof((MonetJobTp &) pNewData) == sizeof(MonetJobTp));
I'm not sure whether this checks the size of pNewData against a
MonetJobTp struct or whether it just forces pNewData to align with a
MonetJobTp struct and always passes the test.
you figured this one out yourself
Post by Dan McCarty
Is it better to just ASSERT(pNewData) to ensure it's a valid pointer
and leave it at that?
You might want to check that the whole of the memory you might refer to is
valid by calling IsBadReadPtr() or IsBadWritePtr().
You may also find that using SendMessage instead of PostMessage is
preferable.
--
Damian
James Brown
2005-05-24 22:05:39 UTC
Permalink
Try to avoid sending pointers using PostMessage unless you
are sure you can synchronise your code in such a way that the
source memory is still in scope when the window eventually
processes the message - i.e. it is possible to allocate some memory,
"send" it using PostMessage, and then free the memory. When the
message is eventually processes, the memory is no longer valid and
you have a problem. Even sending something "allocated" on the stack
(i.e. a local buffer) would be problematic if the calling function returned
before the message was processed.

SendMessage would work because it is a "synchronous/blocking" call and
will only return when the message has been processed.

just something to think about..

James
--
www.catch22.net
Free win32 software, sourcecode and tutorials
Post by Dan McCarty
Is sending a pointer to data in the LPARAM of PostMessage() an
acceptable way to pass data to a window?
If so, is there any value to checking the size of the passed pointer in
the message handler on the receiving end? e.g.,
ASSERT(sizeof((MonetJobTp &) pNewData) == sizeof(MonetJobTp));
I'm not sure whether this checks the size of pNewData against a
MonetJobTp struct or whether it just forces pNewData to align with a
MonetJobTp struct and always passes the test.
Is it better to just ASSERT(pNewData) to ensure it's a valid pointer
and leave it at that?
Loading...