Article View: borland.public.cppbuilder.nativeapi
Article #3458Re: Message-Only Invisible Window
From: Mike Collins
Date: Mon, 12 Jun 2006 14:37
Date: Mon, 12 Jun 2006 14:37
136 lines
6487 bytes
6487 bytes
Thanks for the post - your code works pretty much as mine does. I've
upgraded it to use the Ex versions of RegisterClass and CreateWindow and it
works brilliantly. Obviously the while(GetMessage()) loop is used to
translate user input message and in my case this is not necessary AND blocks
the further flow of the dll's processing.
A point worth noting for you is that if you set the parent window handle to
HWND_MESSAGE - the message window does not appear when you enumerate windows
so it's a bit harder for others to find the handle and mess on with it. The
downside of this is that the window does not receive broadcast messages but
in my case that is now an issue.
"Dennis Jones" <nospam@nospam.com> wrote in message
news:448ce553$1@newsgroups.borland.com...
>
> "Mike Collins" <its@TheBottomOfThePost.com> wrote in message
> news:448c3299@newsgroups.borland.com...
>
>> After reading MSDN, it seems that I can do this with CreateProcess and
>> CreateProcessEx, passing HWND_MESSAGE as the hWndParent. However, I'm
>> not
>> 100% sure what calls I need to implement in this particular case.
> Normally
>> when using CreateWindow I would do the following;
>>
>> 1) define a WndProc callback procedure for the intended window.
>> 2) define and propagate a WNDCLASS or WNDCLASSEX class structure.
>> 3) Register the class with a call to RegisterClass or RegisterClassEx.
>> 4) If sucessfull, call CreateWindow or CreateWindowEx with the same
>> classname as used in WNDCLASS.
>> 5) If a valid HWND is created, call ShowWindow() and UpdateWindow().
>> 6) Enter a while(GetMessage()) loop.
>>
>> However, the situation that I have is slightly different and I need to
>> confirm the following:
>>
>> 1) Are there any specific properties, other than HWND_MESSAGE as the
>> hWndParent, that I need to set in either WNDCLASS / WNDCLASSEX or
>> RegisterClass / RegisterClassEx in my specific situation?
>> 2) Do I need to call ShowWindow() or UpdateWindow in this particular
> context
>> as I don't really have a window to show?
>> 3) Do I specifically have code the while(GetMessage()) loop or can I rely
> on
>> the WndProc to detect and capture my custom messages? I only ask this
>> because it would seem to me that if I have a while loop in the start-up
>> of
>> my DLL's main thread, it would then in effect pause the thread as it
>> would
>> be sitting in that loop.
>> 4) I want to ensure that no other processes can attempt to close or
>> terminate this hidden window - what is the best way to prevent this.
>
> I can't answer your questions directly, but I can tell you that I managed
> to
> create an invisible window to process messages in a service (a service
> does
> not normally receive/process Windows messages, and I needed to handle the
> WM_TIMECHANGE message, along with several custom messages). Basically, my
> service follows your steps 1-4 to create a window via CreateWindow(). I
> do
> not call ShowWindow/UpdateWindow nor do I have a GetMessage() loop, and
> yet,
> my service receives and processes Windows messages.
>
> Here is a code-snippet, just in case it proves to be helpful (keep in mind
> that this code was implemented in a TService descendant class):
>
> // Services cannot normally receive Windows messages because they do not
> have windows associated
> // with a desktop. This function registers a hidden window that is
> capable
> of receiving broadcast
> // Windows messages or self-posted messages.
> bool TMyService::CreateMessageHandlerWnd( void )
> {
> WNDCLASSEX w;
> memset(&w, 0, sizeof(w));
> w.cbSize = sizeof(w);
> w.hInstance = Forms::Application->Handle;
> w.lpfnWndProc = MyServiceWindowProc;
> w.lpszClassName = "MyServiceWindowClass";
> ATOM atom = ::RegisterClassEx(&w);
> if ( atom )
> {
> // TMyService::Handle is a static HWND data member
> TMyService::Handle = ::CreateWindow( w.lpszClassName,
> "",
> WS_DISABLED,
> 0, 0, 0, 0,
> NULL,
> NULL,
> w.hInstance,
> NULL);
> }
> return (atom && TMyService::Handle);
> }
>
> And the WndProc function is quite simple:
>
> // This is the message processing function. When the service receives
> broadcast messages, or
> // or messages posted by the service, they are handled and processed here.
> LRESULT CALLBACK TMyService::MyServiceWindowProc( HWND hWnd,
> UINT
> uMsg,
> WPARAM
> wParam,
> LPARAM
> lParam )
> {
> TMessage msg;
> msg.Msg = uMsg;
> msg.WParam = wParam;
> msg.LParam = lParam;
>
> // don't try to handle messages until the service has been instantiated
> if ( MyService )
> {
> switch( uMsg )
> {
> case WM_TIMECHANGE:
> MyService->WMTimeChangeHandler( msg );
> break;
>
> // etc.
> }
> }
> return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
> }
>
> - Dennis
>
>
Technical Details
Message-ID:
<448d7c27@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<448c3299@newsgroups.borland.com> <448ce553$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,80c772113b1e51c1 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Mike Collins" <its@TheBottomOfThePost.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <448c3299@newsgroups.borland.com> <448ce553$1@newsgroups.borland.com> Subject: Re: Message-Only Invisible Window Date: Mon, 12 Jun 2006 15:37:24 +0100 Lines: 138 Organization: SDA Ltd X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.2663 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.2663 NNTP-Posting-Host: 81.2.92.14 Message-ID: <448d7c27@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1150123047 81.2.92.14 (12 Jun 2006 07:37:27 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail