valid HWND in a Service

K

Kolja Märtens

Hello!

I've been professionally working on java projects for several years, but
have done extremely little C/C++ coding and just a few little things in
VB.Net.

Right now I'm trying to write a Windows Service in VC++ .Net thats
supposed to use a 3rd party SDK do receive Image data and send it out
through a webservice.

In the 3rd party sdk there is a function that requires a HWND to be
passed to it. It then uses the component behind that handle to paint the
images straight to it (Actually what i think it does is getting the
position and dimension of the component and drawing via directX
overlay). However, it also supports registering a custom callback
methode where the actual raw picture data is passed to.

By writing a custom callback methode i can receive all the data I need,
but only if i provide a valid HWND in the first place (NULL and 0 both
result in an exception stating the Windowhandle was invalid).
Since I need the whole thing to run as a windows service I dont have a
handle to a real Window or Form component, so Im looking for a way to
create a dummy window component, that is going to "look and feel" like a
real component to the 3rd party method.

Hope anyone can provide some information on how to deal with this problem.

Kind regards,

Kolja
 
M

Mark Salsbery [MVP]

Kolja Märtens said:
Hello!

I've been professionally working on java projects for several years, but
have done extremely little C/C++ coding and just a few little things in
VB.Net.

Right now I'm trying to write a Windows Service in VC++ .Net thats
supposed to use a 3rd party SDK do receive Image data and send it out
through a webservice.

In the 3rd party sdk there is a function that requires a HWND to be passed
to it. It then uses the component behind that handle to paint the images
straight to it (Actually what i think it does is getting the position and
dimension of the component and drawing via directX overlay). However, it
also supports registering a custom callback methode where the actual raw
picture data is passed to.

By writing a custom callback methode i can receive all the data I need,
but only if i provide a valid HWND in the first place (NULL and 0 both
result in an exception stating the Windowhandle was invalid).
Since I need the whole thing to run as a windows service I dont have a
handle to a real Window or Form component, so Im looking for a way to
create a dummy window component, that is going to "look and feel" like a
real component to the 3rd party method.


You should have no problem creating a hidden window in your service (not
that it would show if it wasn't hidden anyway).

The RegisterClass and CreateWindow APIs should work fine. Just create the
window without the WS_VISIBLE style.
You'll maybe want to do this on a thread with a message loop :)

Mark
 
K

Kolja Märtens

Mark said:
You should have no problem creating a hidden window in your service (not
that it would show if it wasn't hidden anyway).

The RegisterClass and CreateWindow APIs should work fine. Just create
the window without the WS_VISIBLE style.
You'll maybe want to do this on a thread with a message loop :)

Thanx for your reply, Im not sure if I understand exactly what you mean.
Allready before I read your post I tried to open a window by manually
calling CreateWindow (this time form a .Net console application for
testing).

Heres what I tried (after googeling for hours):
HWND hWnd = CreateWindow("STATIC", "", 0, 0, 0, 0, 0, NULL, NULL, NULL,
NULL);

It compiles, but hWnd is not propperly initialized, Ill try research on
RegisterClass as you mentioned.
 
M

Mark Salsbery [MVP]

Kolja Märtens said:
Thanx for your reply, Im not sure if I understand exactly what you mean.
Allready before I read your post I tried to open a window by manually
calling CreateWindow (this time form a .Net console application for
testing).

Heres what I tried (after googeling for hours):
HWND hWnd = CreateWindow("STATIC", "", 0, 0, 0, 0, 0, NULL, NULL, NULL,
NULL);

It compiles, but hWnd is not propperly initialized, Ill try research on
RegisterClass as you mentioned.


See
Using Window Classes
http://msdn.microsoft.com/en-us/library/ms633575(VS.85).aspx

Mark
 
B

Ben Voigt [C++ MVP]

Kolja said:
Thanx for your reply, Im not sure if I understand exactly what you
mean. Allready before I read your post I tried to open a window by
manually calling CreateWindow (this time form a .Net console
application for testing).

If you are using .NET, use the NativeWindow class, read the Handle property
to get the HWND, override WndProc, etc.
 
K

Kolja Märtens

Ben said:
If you are using .NET, use the NativeWindow class, read the Handle property
to get the HWND, override WndProc, etc.

That seemed like an easy approach so I tried.

NativeWindow * w = new NativeWindow();
CreateParams * params = new CreateParams();
params->X=100;
params->Y=100;
params->Height=100;
params->Width=100;

w->CreateHandle(params);

The Handle Parameter of the Window becomes !=0 wich seems good.
But when I try to get the HWND by
HWND hWnd = (HWND)w->get_Handle().ToPointer();
the HWND is still uninitialized.

I was really hoping to find a quick solution that works without the need
to understand a whole lot about windows programming so I can get back to
the Java Part of the Solution (The server side of the webservice).
Unfortunately the more I tried the less I think it will be possible
without digging deeper and at least starting to understand how this works...
 
K

Kolja Märtens

Kolja said:
The Handle Parameter of the Window becomes !=0 wich seems good.
But when I try to get the HWND by
HWND hWnd = (HWND)w->get_Handle().ToPointer();
the HWND is still uninitialized.

I wrote a class that inherits from NativeWindow and with that I managed
to get a working HWND now. Not sure what went wrong earlier, cause it
seems like it should have worked before.
Unfortunately the Callback method mentioned earlier is never invoked.
It seems a lot like the 3rd party Thread that does the painting was
checking if the window at the provided HWND is visible before it even
starts doing something.
In the .net forms application i can simulate this behavior by calling
Hide() on the Window.

Is there a way to make the 3rd party thread think the window was visible
and all good?
I suppose I should overwrite the function that is used for detecting if
the window is visible and sinply return true, but i have no clue what
funtion that is.
I wish i had the eclipse IDE that Im familiar with and could just click
on "overwrite/implement Methods..."
 
K

Kolja Märtens

Kolja said:
Is there a way to make the 3rd party thread think the window was visible
and all good?

Ive done some more testing and found that the callback is invoked fine
when i allow the Service to interact with the desktop and call
ShowWindow on the Windowclass I created.
I suppose I should overwrite the function that is used for detecting if
the window is visible and sinply return true, but i have no clue what
funtion that is.

still looking for that.
 
B

Ben Voigt [C++ MVP]

Kolja said:
Ive done some more testing and found that the callback is invoked fine
when i allow the Service to interact with the desktop and call
ShowWindow on the Windowclass I created.

ShowWindow should have worked fine and set the WS_VISIBLE flag even when the
application runs on a service desktop.

However, if the callback is triggered by WM_PAINT it will only happen when
the window really is on the screen. However, you can InvalidateWindow and
SendMessage WM_PAINT to the window yourself.
still looking for that.

Bad idea. The function is provided by Microsoft inside USER32.dll.
 
K

Kolja Märtens

Ben said:
ShowWindow should have worked fine and set the WS_VISIBLE flag even when the
application runs on a service desktop.

What do you mean by "service desktop"?
The Callback is indeed invoked even when the user is logged of as long
as the Service has the right to interact with the desktop.

Big drawback is, once a user logs in, the Window is displayed and thats
something I definetly dont want.
However, if the callback is triggered by WM_PAINT it will only happen when
the window really is on the screen. However, you can InvalidateWindow and
SendMessage WM_PAINT to the window yourself.

I tracked all window messages in my dummy window class and passed them
on to the NativeWindow WndProc. Seems like the window was only used
during initialization because when the process starts drawing the
Pictures (in an overlay i suppose) there are no more window messages
coming in.

Heres what WndProc is getting:
36 WM_GETMINMAXINFO
129 WM_NCCREATE
131 WM_NCCALCSIZE
1 WM_CREATE
24 WM_SHOWWINDOW
70 WM_WINDOWPOSCHANGING
70 WM_WINDOWPOSCHANGING
28 WM_ACTIVATEAPP
13 WM_GETTEXT
134 WM_NCACTIVATE
642 WM_IME_NOTIFY
641 WM_IME_SETCONTEXT
7 WM_SETFOCUS
6 WM_ACTIVATE
13 WM_GETTEXT
133 WM_NCPAINT
20 WM_ERASEBKGND
71 WM_WINDOWPOSCHANGED
5 WM_SIZE
3 WM_MOVE

I just realized that there is indeed one WM_PAINT.
From what I know about video overlay I think the Background of the
Window is changed to magenta to mark the aerea to overlay.

Ill have to read about what InvalidateWindow does...
Bad idea. The function is provided by Microsoft inside USER32.dll.

I was hoping for something like "bool isVisible()" in the Windowclass...
I know by now this is not the way things work around here :)
 
B

Ben Voigt [C++ MVP]

Kolja said:
What do you mean by "service desktop"?
The Callback is indeed invoked even when the user is logged of as long
as the Service has the right to interact with the desktop.

Big drawback is, once a user logs in, the Window is displayed and
thats something I definetly dont want.

If you don't check that "Interact with user's desktop" permission (which is
not even available in newer versions of Windows), your service runs in a
separate desktop just for services. This way it can create windows, pass
messages to other services' windows, etc, but there's a security barrier
between windows of trusted services and windows in the user's logon session.
This prevents the "shatter attack".
I tracked all window messages in my dummy window class and passed them
on to the NativeWindow WndProc. Seems like the window was only used
during initialization because when the process starts drawing the
Pictures (in an overlay i suppose) there are no more window messages
coming in.

Heres what WndProc is getting:
36 WM_GETMINMAXINFO
129 WM_NCCREATE
131 WM_NCCALCSIZE
1 WM_CREATE
24 WM_SHOWWINDOW
70 WM_WINDOWPOSCHANGING
70 WM_WINDOWPOSCHANGING
28 WM_ACTIVATEAPP
13 WM_GETTEXT
134 WM_NCACTIVATE
642 WM_IME_NOTIFY
641 WM_IME_SETCONTEXT
7 WM_SETFOCUS
6 WM_ACTIVATE
13 WM_GETTEXT
133 WM_NCPAINT
20 WM_ERASEBKGND
71 WM_WINDOWPOSCHANGED
5 WM_SIZE
3 WM_MOVE

I just realized that there is indeed one WM_PAINT.
From what I know about video overlay I think the Background of the
Window is changed to magenta to mark the aerea to overlay.

Ill have to read about what InvalidateWindow does...


I was hoping for something like "bool isVisible()" in the
Windowclass... I know by now this is not the way things work around
here :)

You mean like http://msdn.microsoft.com/en-us/library/ms633530(VS.85).aspx ?

Though if you were hoping for "virtual bool isVisible()" that you could
override, you're out of luck. There's one implementation inside USER32.DLL
which is used for all windows, and there's nothing like a v-table that would
allow you to change it. Only the window procedure can be replaced on a
per-window basis (this is called subclassing).
 
K

Kolja Märtens

Just wanted to take a chance and say thank you to everyone who supported
me on this.
I talked to the company that developed the SDK and they told me that the
next SDK version is going to support a way to have the callback invoked
without the need of a HWND in the first place.

Seems I had luck this time since now I have the pre release version and
things should be a lot easier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top