Application works as console but not as service

Hi All,

We are writing a service which will interact with a WDDM Display driver. It isn’t loading. Are there any special considerations we must take to load a service that interacts with the kernel?
It used to be an application with a window, and we are stopping the window to show up, and making it as a console app. It still works fine as a console application. But when I add the application’s functionality to a windows service application, the service fails to start giving Error 1053.
Are we allowed to have a HWND in a windows service, even if we never display it?

Thanks

Have you attached a debugger and determined what exactly is failing?

d

Bent by my phone


From: xxxxx@gmail.commailto:xxxxx
Sent: ?10/?18/?2013 7:49 AM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: [ntdev] Application works as console but not as service

Hi All,

We are writing a service which will interact with a WDDM Display driver. It isn’t loading. Are there any special considerations we must take to load a service that interacts with the kernel?
It used to be an application with a window, and we are stopping the window to show up, and making it as a console app. It still works fine as a console application. But when I add the application’s functionality to a windows service application, the service fails to start giving Error 1053.
Are we allowed to have a HWND in a windows service, even if we never display it?

Thanks


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</mailto:xxxxx></mailto:xxxxx>

xxxxx@gmail.com wrote:

We are writing a service which will interact with a WDDM Display driver. It isn’t loading. Are there any special considerations we must take to load a service that interacts with the kernel?
It used to be an application with a window, and we are stopping the window to show up, and making it as a console app. It still works fine as a console application. But when I add the application’s functionality to a windows service application, the service fails to start giving Error 1053.
Are we allowed to have a HWND in a windows service, even if we never display it?

You should be able to create a hidden window in a Windows Service,
although it’s not clear what good it will do. Every session has its own
namespace for handles, so a normal app in the interactive desktop
session can’t communicate with your service’s HWND.

HOWEVER, your service cannot display a window, and it cannot interact
with the graphics driver. That’s impossible.

My GUESS is that you are trying to run two message loops. The service
has its own message loop to handle service messages, and you probably
added a message loop for your hidden window when you merged the code.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Error 1053 is ERROR_SERVICE_REQUEST_TIMEOUT (The service did not respond to
the start or control request in a timely fashion.)

It is certainly possible to create windows and message loops in services,
but you cannot wait for user input as your windows will never ever be seen
by any user (with the exceptions of GDI being disabled by
SetProcessMitigationPolicy + ProcessSystemCallDisablePolicy or the use of
the window station APIs)

all services interact with the kernel to some degree as the SCM API and
nearly all others do. I assume that you meant to ask if there are special
requirements to open and call DeviceIOControl on an interface exposed by
your device. The answer is yes, make sure that the ACL on that interface
allows the user you are running the service as to access it

I suspect that you either have not coded your main to call
StartServiceCtrlDispatcher, or are executing a blocking wait of some kind in
your ServiceMain before updating your status to SERVICE_RUNNING. this
likely has nothing to do with the use of GDI or access to your device

wrote in message news:xxxxx@ntdev…

Hi All,

We are writing a service which will interact with a WDDM Display driver. It
isn’t loading. Are there any special considerations we must take to load a
service that interacts with the kernel?
It used to be an application with a window, and we are stopping the window
to show up, and making it as a console app. It still works fine as a console
application. But when I add the application’s functionality to a windows
service application, the service fails to start giving Error 1053.
Are we allowed to have a HWND in a windows service, even if we never display
it?

Thanks

Note that a Windows service is not just an app started as a service. It
has very precise requirements. The MSDN examples of programming a service
are notoriously bad examples, pretty much being exemplars of how a summer
intern who was totally clueless might write one. The best examples are in
Reeves and Brain, Windows System Programming. But you have not said how
your app works, and in any case, I have no idea how a service can interact
with a display driver in any meaningful way because services run in a
different desktop.

The error you descibe most commonly comes from an attempt to take a normal
console app and have the SCM start it; the second most common cause is
erroneous coding of the startup sequence of a more-or-less (in this case,
less) appropriately-coded service. If you do not have a “ServiceMain” or
“ServiceCtrkHandler(Ex)”, then you don’t even /have/ a service. If you
do, how does it interact with the display? (Services run in a separate
desktop, and the ability to interact with the user-visible screen is at
best complex beyond all reason and at worst has been deprecated so you
can’t do it at all.

Services have no special requirements for calling the kernel, other than
the fact that they are not generally running under a specific account and
their windows are not going to be visible. They have very precise
requirements that must be met about how they start, stop, and handle SCM
notifications. Any restrictions you encounter are usually caused by the
service account lacking necessary privileges, which is not a problem of
being a service but a problem of running under the wrong login ID.

What problem do you think running your task as a service is going to solve?
joe

xxxxx@gmail.com wrote:
> We are writing a service which will interact with a WDDM Display driver.
> It isn’t loading. Are there any special considerations we must take to
> load a service that interacts with the kernel?
> It used to be an application with a window, and we are stopping the
> window to show up, and making it as a console app. It still works fine
> as a console application. But when I add the application’s functionality
> to a windows service application, the service fails to start giving
> Error 1053.
> Are we allowed to have a HWND in a windows service, even if we never
> display it?

You should be able to create a hidden window in a Windows Service,
although it’s not clear what good it will do. Every session has its own
namespace for handles, so a normal app in the interactive desktop
session can’t communicate with your service’s HWND.

HOWEVER, your service cannot display a window, and it cannot interact
with the graphics driver. That’s impossible.

My GUESS is that you are trying to run two message loops. The service
has its own message loop to handle service messages, and you probably
added a message loop for your hidden window when you merged the code.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

In answer to the last part of the question, yes, you can have an HWND; I
have done this several times in services where I needed a message sink
(like for asynchronous sockets). If you expect it to be displayed, be
prepared for massive effort and quite ultimately, disappointment.

You have not indicated that you have built a proper service. Please
clarify that (a) you have done so and (b) you did not rely on the MSDN for
how to do this.
joe

Hi All,

We are writing a service which will interact with a WDDM Display driver.
It isn’t loading. Are there any special considerations we must take to
load a service that interacts with the kernel?
It used to be an application with a window, and we are stopping the window
to show up, and making it as a console app. It still works fine as a
console application. But when I add the application’s functionality to a
windows service application, the service fails to start giving Error 1053.
Are we allowed to have a HWND in a windows service, even if we never
display it?

Thanks


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

since it does’t come up, attaching a debugger can be challenging. Putting
in debug print or logging output into the sevice at critical points will
be more informative. Output indicating Main() has been called and exited,
output from “ServiceMain”, from worker threads, and from the various cases
in “ServiceCtrkHandler(Ex)” would give a better picture of what is
happening.
joe

Have you attached a debugger and determined what exactly is failing?

d

Bent by my phone


From: xxxxx@gmail.commailto:xxxxx
> Sent: ý10/ý18/ý2013 7:49 AM
> To: Windows System Software Devs Interest Listmailto:xxxxx
> Subject: [ntdev] Application works as console but not as service
>
> Hi All,
>
> We are writing a service which will interact with a WDDM Display driver.
> It isn’t loading. Are there any special considerations we must take to
> load a service that interacts with the kernel?
> It used to be an application with a window, and we are stopping the window
> to show up, and making it as a console app. It still works fine as a
> console application. But when I add the application’s functionality to a
> windows service application, the service fails to start giving Error 1053.
> Are we allowed to have a HWND in a windows service, even if we never
> display it?
>
> Thanks
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer</mailto:xxxxx></mailto:xxxxx>

On 10/18/2013 5:57 PM, Tim Roberts wrote:

xxxxx@gmail.com wrote:
> We are writing a service which will interact with a WDDM Display driver. It isn’t loading. Are there any special considerations we must take to load a service that interacts with the kernel?
> It used to be an application with a window, and we are stopping the window to show up, and making it as a console app. It still works fine as a console application. But when I add the application’s functionality to a windows service application, the service fails to start giving Error 1053.
> Are we allowed to have a HWND in a windows service, even if we never display it?
You should be able to create a hidden window in a Windows Service,
although it’s not clear what good it will do. Every session has its own
namespace for handles, so a normal app in the interactive desktop
session can’t communicate with your service’s HWND.

By the way If you use window messages to communicate between threads in
a service, you should use HWND_MESSAGE and not an ordinary (hidden)
window because it breaks in session 0 isolation on Vista/Server 2008
(e.g. http://sourceforge.net/p/wix/bugs/3245/).


Bruce Cran

Thanks. I was not aware of that issue. I think my clients still run
WinXP, and I will pass this on to them.
joe

On 10/18/2013 5:57 PM, Tim Roberts wrote:
> xxxxx@gmail.com wrote:
>> We are writing a service which will interact with a WDDM Display
>> driver. It isn’t loading. Are there any special considerations we
>> must take to load a service that interacts with the kernel?
>> It used to be an application with a window, and we are stopping the
>> window to show up, and making it as a console app. It still works fine
>> as a console application. But when I add the application’s
>> functionality to a windows service application, the service fails to
>> start giving Error 1053.
>> Are we allowed to have a HWND in a windows service, even if we never
>> display it?
> You should be able to create a hidden window in a Windows Service,
> although it’s not clear what good it will do. Every session has its own
> namespace for handles, so a normal app in the interactive desktop
> session can’t communicate with your service’s HWND.

By the way If you use window messages to communicate between threads in
a service, you should use HWND_MESSAGE and not an ordinary (hidden)
window because it breaks in session 0 isolation on Vista/Server 2008
(e.g. http://sourceforge.net/p/wix/bugs/3245/).


Bruce Cran


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Thank you all guys.
I’ll start answering you one by one:
Doron, I have not attached a debugger so far to find out the issue. I’ll try that.

Tim, I don’t want my service to display the window. But yes, we really want it to interact
with the graphics driver. That’s needed. Is it really Impossible for a service??
I am calling the desired function from the OnStart() routine of the service.

MM, we are not having any procedure for user input, it’s not an interactive application. I have StartServiceCtrlDispatcher called in the Run() routine, which registers the executable for a service with the Service Control Manager and eventually calls OnStart() method. It’s a WORKING sample service.

Joe, I have “ServiceMain” and “ServiceCtrkHandler” both in my service. I have a proper service app and it runs fine. In fact, i can see the prints I have inserted in the Event Viewer when I start or Stop the service. Apparently, it’s from msdn.

I am using CppWindowsService sample from msdn (http://code.msdn.microsoft.com/windowsdesktop/CppWindowsService-cacf4948).

The first and most important question is: Is it really impossible for a service to interact with a kernel-mode driver?
My code includes calls to D3DKMTEscape() as it’s a WDDM driver with which I’m trying to interact.

Thanks!

Think about your question ‘Is it really impossible for a service to interact
with a kernel-mode driver’: if it were impossible for a service to interact
with the kernel, then no services could ever be written because all
processes need at least some services from the OS kernel. Any program that
does anything besides while(true); interacts with the kernel in some way
shape or form.

re your question about interacting with a graphics driver, the answer
depends on what you want to do and how you plan to interact with the driver.
If you want your service to draw something on a monitor attached to a video
card in the machine, or create a window (form, dialog, control, or any other
visual element) via GDI, then you are out of luck. While it might be
possible in specific circumstances, this will do nothing but crash your
machine and or get you calls from angry users. If you want to open a
control device from your video card driver and read / write some out of band
status or control information, then this will work just fine.

The error you reported indicates that your service is NOT interacting
correctly with the OS SCM, so whatever else you are doing, you aren’t doing
that right. Debugging service startup routines is difficult, but there is
(or was) an article on MSDN describing how to do this. Note that Run and
OnStart are not standard terms, so they mean nothing to me. If your service
runs for a short while and then startup fails with this error, it is
probably because of where / how you are doing to work your service should do
and not the calls to StartServiceCtrlDispatcher etc. If you try to execute
long blocking calls on the handler thread, you will quickly get into
trouble.

wrote in message news:xxxxx@ntdev…

Thank you all guys.
I’ll start answering you one by one:
Doron, I have not attached a debugger so far to find out the issue. I’ll try
that.

Tim, I don’t want my service to display the window. But yes, we really want
it to interact
with the graphics driver. That’s needed. Is it really Impossible for a
service??
I am calling the desired function from the OnStart() routine of the service.

MM, we are not having any procedure for user input, it’s not an interactive
application. I have StartServiceCtrlDispatcher called in the Run() routine,
which registers the executable for a service with the Service Control
Manager and eventually calls OnStart() method. It’s a WORKING sample
service.

Joe, I have “ServiceMain” and “ServiceCtrkHandler” both in my service. I
have a proper service app and it runs fine. In fact, i can see the prints I
have inserted in the Event Viewer when I start or Stop the service.
Apparently, it’s from msdn.

I am using CppWindowsService sample from msdn
(http://code.msdn.microsoft.com/windowsdesktop/CppWindowsService-cacf4948).

The first and most important question is: Is it really impossible for a
service to interact with a kernel-mode driver?
My code includes calls to D3DKMTEscape() as it’s a WDDM driver with which
I’m trying to interact.

Thanks!

xxxxx@gmail.com wrote:

The first and most important question is: Is it really impossible for a service to interact with a kernel-mode driver?

No, that’s not what I said. I said it was impossible to interact with
the display driver.

My code includes calls to D3DKMTEscape() as it’s a WDDM driver with which I’m trying to interact.

How did you get the hAdapter and the hDevice?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

> Are we allowed to have a HWND in a windows service, even if we never display it?

Bad idea.

It is always good to separate the UI layer of the code from the other layers.

And, in a service, you will only need the other layers and not the UI.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

> By the way If you use window messages to communicate between threads in

a service

Or even better idea is to use COM for this.

The code is auto-generated by MSVC’s wizards and all this “hidden window” stuff is really hidden from you, you just deliver object method calls on another thread.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

> Thank you all guys.

I’ll start answering you one by one:
Doron, I have not attached a debugger so far to find out the issue. I’ll
try that.

Tim, I don’t want my service to display the window. But yes, we really
want it to interact
with the graphics driver. That’s needed. Is it really Impossible for a
service??
I am calling the desired function from the OnStart() routine of the
service.

MM, we are not having any procedure for user input, it’s not an
interactive application. I have StartServiceCtrlDispatcher called in the
Run() routine, which registers the executable for a service with the
Service Control Manager and eventually calls OnStart() method. It’s a
WORKING sample service.

Joe, I have “ServiceMain” and “ServiceCtrkHandler” both in my service. I
have a proper service app and it runs fine. In fact, i can see the prints
I have inserted in the Event Viewer when I start or Stop the service.
Apparently, it’s from msdn.

I am using CppWindowsService sample from msdn
(http://code.msdn.microsoft.com/windowsdesktop/CppWindowsService-cacf4948).

I don’t have time to download it, but it sounds like a C++ implementation
of a service framework. Note that the thread which is handling the events
from the ServiceCtrlHandler must not block. It must process the “Start”
request, send an acknowledgement to the SCM, and return. If it calls a
subroutine, then the SCM will time out because it did not get its response
in a timely fashion. The real work of the service should be done in a
separate thread. it is essential that you have this behavior. Otherwise,
you will get timeout notifications. Also, you need to send the proper
status reports, beware of blocking calls with potentially unbounded delays
during any start or continue sequence.

The concept that it is impossible for a service to interact with a driver
is nonsensical. It’s just another app. What is critical is whether or
not the device you are communicating with has ACLs that would block the
service if it is not started in the context of an appropriate user ID.
Note that the default login account for services is not necessarily
granted these privileges, for a variety of security reasons. So in
general, the answer is that, OF COURSE you can interact with a driver,
just like any app can interact with any driver: if you can open it, you
can interact with it, and all you need to open it is a suitable file name
(which you can derive if all you have is the interface GUID for the
device) and the privileges to access that. A service, in that sense, is
absolutely no different than any other app. If you log in with an account
that doesn’t have privileges for a device, you can’t get that device.
Services are the same in that respect.

[There’s another subgenre of this question: “Can I do a kernel call from a
DLL?” or the variant “Can I issue the XXXXXX() call from a DLL?”, which is
just as silly, because a DLL is just more code in the process space.
After many days of back-and-forthing in the newsgroup, it ALWAYS turned
out to be that in the refactoring of their code from the app to the DLL,
they got something wrong about the parameters to the call!]

Your problem is not with being a service; it is with being a service that
has service-related bugs in the code.

As long as you don’t try to draw anything, you will be safe. The issue is
really about “ownership of the screen”. The graphics subsystem owns the
screen, and you cannot bypass it.

I can’t speak as to whether or not the escape call you reference is safe
or not. Someone who knows D3D and graphics will have to answer that.
joe

The first and most important question is: Is it really impossible for a
service to interact with a kernel-mode driver?
My code includes calls to D3DKMTEscape() as it’s a WDDM driver with which
I’m trying to interact.

Thanks!


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

My favorite interthread communication mechanism is the I/O Completion
Port. And, unlike messages to windows, there is no artificial limit, and
with care, you can even use this technique in the main GUI thread and not
block the message pump. I describe this technique in one of the essays on
my MVP Tips site.

http://www.flounder.com/iocompletion.htm

joe

> By the way If you use window messages to communicate between threads in
> a service

Or even better idea is to use COM for this.

The code is auto-generated by MSVC’s wizards and all this “hidden window”
stuff is really hidden from you, you just deliver object method calls on
another thread.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Thank you all for thoughts.
I was debugging the service using debug prints and I found out that the execution stops after:

while( GetMessage(&msg, NULL, 0, 0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

Even this loop runs thrice and after that the execution stops as i don’t see any prints further. I am investigating that.
Can anyone give some insight into what is happening here?

Thanks!

xxxxx@gmail.com wrote:

I was debugging the service using debug prints and I found out that the execution stops after:

while( GetMessage(&msg, NULL, 0, 0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

Even this loop runs thrice and after that the execution stops as i don’t see any prints further. I am investigating that.
Can anyone give some insight into what is happening here?

Have you created a hidden window? If not, what messages are you getting
here? How does this interact with your registered service handler? Are
you calling this from your ServiceMain function after you call
RegisterServiceCtrlHandler? Do any of your service handler callbacks
depend on stuff happening in your wndproc?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

This is a standard message loop for a thread with windows. Find out what
thread this gets called from and make sure it is not called directly from
your ‘OnStart’ as this loop never returns until GetMessage returns false
(typically in response to a WM_QUIT message)

wrote in message news:xxxxx@ntdev…

Thank you all for thoughts.
I was debugging the service using debug prints and I found out that the
execution stops after:

while( GetMessage(&msg, NULL, 0, 0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

Even this loop runs thrice and after that the execution stops as i don’t see
any prints further. I am investigating that.
Can anyone give some insight into what is happening here?

Thanks!

If you put this loop in a separate thread, any windows it needs must be
created in the thread, not in so e other thread. So don’t create the
window(s) then create the thread; the number of diasters that will follow
will not be fun.

Note also the earlier note in this thread about the proper window style to
add to make sure it runs in Vista+
joe

This is a standard message loop for a thread with windows. Find out what
thread this gets called from and make sure it is not called directly from
your ‘OnStart’ as this loop never returns until GetMessage returns false
(typically in response to a WM_QUIT message)

wrote in message news:xxxxx@ntdev…

Thank you all for thoughts.
I was debugging the service using debug prints and I found out that the
execution stops after:

while( GetMessage(&msg, NULL, 0, 0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

Even this loop runs thrice and after that the execution stops as i don’t
see
any prints further. I am investigating that.
Can anyone give some insight into what is happening here?

Thanks!


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer