FindWindow in a Service DLL

V

victor

Help ....

This is my situation:
I've made two apps, a Managed C# and an Unmanaged MFC. The C# app
communicates with the MFC-app via (Win32-API) PostMessage, done with
the P/Invoke method. One of the activities is "FindWindow". Everything
works fine.
Until ...: I needed the same procedure within another context: now I
need to communicate the C# with a web service (dll) running somewhere
on a Server machine. This doesnot work anymore!
My assumption is that the service-dll is *not* running in a
UserInteractive mode, and therefore cannot perform the "FindWindow"
function. (It keeps returning a zero handle ....)
Or, am I doing something wrong?
Somebody have an idea how to solve this?
Help is very much appreciated!
Thank you.

;-( victor
 
P

Pete Davis

A service really shouldn't interact with the desktop. I believe the SYSTEM
account can, but that's only meant for debugging purposes, as I understand.

If you really want to go down this dangerous road, there's a hack for
getting around it here:
http://www.codeproject.com/csharp/CsWindowsServiceDesktop.asp

I would really recommend you find a better communication mechanism, like
sockets.

Pete
 
V

victor

Pete, thank you for your response!
As you/ve probably guessed, I'm quite a novice on this 'service'
matters. Hence, I'm totally reliant on people like you. So, following
your link-tip (CodeProject) I've learned a lot more by now!
I did the coding section of Robert Davis - alas, still with no result;
meaning: FindWindow still reponds with a null and, what worries me
more: I can't find the key entry of my service in the Registry!
This brings me to the tought of: is Davis' code executed in my
service-DLL? I've put it right after the 'InitializeComponent();'
line. Is this the right place?

greetz, victor
------------------------------------------------------------------------------------------
 
P

Pete Davis

Victor,

This needs to be done in the service installer, not in the service itself.

What's happening is he's modifying the registry and setting a bit in the
flag that allows the service to interact with the desktop. The Service
Manager then uses this information when it starts the service. So it must be
set before the service actually runs.

Pete
 
W

Willy Denoyette [MVP]

No it's not, please follow the samples given in MSDN on how to implement a
Service using the framework.
But I'm affraid this will never work, because your service has no top level
window (and should not have on) and even if it has one (say you created and
instance of a Form and run a message loop), you won't be able to use it to
get messages from processes running on other machines using Postmessage.
Also I'm not clear why you want to use Postmessage to pass messages to a Web
service??


Willy.
 
R

Richard Grimes

victor said:
My assumption is that the service-dll is *not* running in a
UserInteractive mode, and therefore cannot perform the "FindWindow"
function. (It keeps returning a zero handle ....)
Or, am I doing something wrong?
Somebody have an idea how to solve this?
Help is very much appreciated!

Well, it does run on a desktop, but as you say, not the interactive
desktop. If the service has the appropriate (NT) security privileges it
can get access to all the WindowStations on the machine (a WindowStation
is an NT secure object). From the WindowStation you can get access to
the Desktop objects running there, and hence enumerate the windows.
You'll have to do all of that with platform invoke.

http://msdn.microsoft.com/library/en-us/dllproc/base/window_stations_and_desktops.asp

See EnumWindowStations, EnumDesktops and EnumDesktopWindows.

BTW the interactive WindowStation is called Winsta0.

Richard
 

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