473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

valid HWND in a Service

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
Jul 23 '08 #1
11 4593
"Kolja Märtens" <ha*@haznet.dewrote in message
news:g6**********@online.de...
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

--
Mark Salsbery
Microsoft MVP - Visual C++
>
Hope anyone can provide some information on how to deal with this problem.

Kind regards,

Kolja
Jul 23 '08 #2
Mark Salsbery [MVP] wrote:
>
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.
Jul 23 '08 #3
"Kolja Märtens" <ha*@haznet.dewrote in message
news:g6**********@online.de...
Mark Salsbery [MVP] wrote:
>>
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.

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

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++

Jul 23 '08 #4
Kolja Märtens wrote:
Mark Salsbery [MVP] wrote:
>>
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).
If you are using .NET, use the NativeWindow class, read the Handle property
to get the HWND, override WndProc, etc.
>
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.

Jul 23 '08 #5
Ben Voigt [C++ MVP] wrote:
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...
Jul 24 '08 #6
Kolja Märtens schrieb:
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..."
Jul 24 '08 #7
Kolja Märtens schrieb:
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.
Jul 24 '08 #8
Kolja Märtens wrote:
Kolja Märtens schrieb:
>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.
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.
>
>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.
Bad idea. The function is provided by Microsoft inside USER32.dll.
Jul 24 '08 #9
Ben Voigt [C++ MVP] schrieb:
>
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...
>>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.

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 :)
Jul 25 '08 #10
Kolja Märtens wrote:
Ben Voigt [C++ MVP] schrieb:
>>
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.
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".
>
>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...
>>>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.

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 :)
You mean like http://msdn.microsoft.com/en-us/libr...30(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).
Jul 28 '08 #11
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.
Jul 29 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Moore | last post by:
Hi All, I have a managed C++ class that makes calls into the Win32 API. I specifically am trying to take a Handle from a .NET form and convert it to a HWND to pass to a Win32 functions. The...
1
by: Zeya | last post by:
I have this code, which uses WMI to operate on Windows service from C# code. When Service.InvokeMethod is called, the method throws an exception: System.Management Operation is not valid due...
17
by: Bonj | last post by:
Is PostQuitMessage(?) different to PostMessage(hWnd, WM_QUIT, ... ) ? I've got a window in a DLL (the same one experiencing the issue below, "return value of WM_QUIT") in which PostMessage(hWnd,...
5
by: MrBewsher | last post by:
Hi, I'm using VB.Net and writing an NT service that runs in the system account in the background with no interaction with the desktop. I'm running it on XP Pro. I'd like to get the URL of...
4
by: --== Alain ==-- | last post by:
Hi, When i debugged my code (see below) from my overrider WndProc method, i discovered that update has alway left, right, botton and top set to 0. therefore i wonder if my hwnd point to the...
15
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
How can I pass a C++ HWND to and from C# and Managed C++?
3
by: c2 | last post by:
hi, i face a problem when click the icon , my login will prompt this message String was not recognized as a valid Boolean See the end of this message for details on invoking just-in-time (JIT)...
0
by: BarryM | last post by:
I was told that this piece coding to find the es_password state in c# would work but an error message saying hWnd does not exist in the current context internal static class UnsafeNativeMethods...
3
by: =?Utf-8?B?YW5kcmV3?= | last post by:
I have a web application demo page and a web service. On my machine everything works great. In our test environment the web service is working fine... when I point the demo page on my machine...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.