🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #3453

Re: Message-Only Invisible Window

#3453
From: Dennis Jones
Date: Mon, 12 Jun 2006 03:53
110 lines
5349 bytes
"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: <448ce553$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <448c3299@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: "Dennis Jones" <nospam@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <448c3299@newsgroups.borland.com>
Subject: Re: Message-Only Invisible Window
Date: Sun, 11 Jun 2006 20:53:58 -0700
Lines: 112
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 71.245.109.58
Message-ID: <448ce553$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1150084435 71.245.109.58 (11 Jun 2006 20:53:55 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail