سؤال

I have a datamodule with a HWND handle that receives PostMessage calls.

The handle is allocated in the DataModuleCreate like this (TTNONVISUAL is not defined):

{$IFNDEF TTNONVISUAL}
FHWND := AllocateHWND(WindProc);
{$ELSE}
FHWND := 0;
{$ENDIF}

The data module is also used in a non-visual program where TTNONVISUAL is defined.

The WindProc in the datamodule is enclosed in {$IFNDEF TTNONVISUAL}, so in the non-visual program there is no WindProc and FHWND=0

In that program, will it be safe to do PostMessage calls to this 0 handle?

Note: The PostMessage does not send any additional data:
PostMessage(FHWND,WM_SYNC_PROGRESS,0,0)
and WM_SYNC_PROGRESS is WM_USER + 111 and there are no other WM_SYNC_PROGRESS handlers.

هل كانت مفيدة؟

المحلول

The documentation says:

[When passing NULL for a handle, which doesn't refer to variant Null, but actually the value 0]:

The function behaves like a call to PostThreadMessage with the dwThreadId parameter set to the identifier of the current thread.

So, it has a distinct meaning to post messages to 0, and you should not just allow that if you actually want to fire messages into oblivion. If that's the case, you'd rather check if the handle is 0 and not post the message at all.

Note, though, that is it perfectly legit to create a window, even in a non-visual application. Handles like this are for sending messages to. They don't necessarily point to a visible window. Actually, that is just what AllocateHWND is for. It is used for non visual controls (like TTimer) that need to be able to recieve messages after all.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top