When should I write to device.exe instead of services.exe?


By default, if you have some long running system service, you should target it to run in services.exe.  To quote the services.exe white paper  when it described why putting say a web server in device.exe was a bad idea:


However, using device.exe also posed some problems. A lot of other components run in device.exe, including the network stacks and real device drivers, which could make tracking resource leaks very difficult. If device.exe is leaking memory and there are 50 other device drivers running, how does a person find the guilty party? Even worse, a poorly written DLL could potentially corrupt memory in the TCP/IP stack, PCMCIA driver or other device driver, causing inexplicable crashes.


Services.exe was created to deal with these issues. Services.exe is designed to have a programming model and application programming interface (API) set that are similar to device.exe to facilitate moving device drivers that weren't really device drivers (think about a Web server) from device.exe to services.exe. If you're not familiar with device.exe, don't worry. This article starts from the basics. If you're familiar with device drivers on Windows CE, much of this article will cover familiar ground.


As the author of services.exe, it pains me to say that sometimes device.exe has its uses.  In particular, you really need to run in device.exe if:


1) You need to touch physical hardware.  In current versions of WinCE you probably can get away doing this in services.exe.  In the next version of CE this will no longer be an option, so do the right thing and get out of services.exe now.  In any event it's not exactly clean to be touching hardware in a process named services.exe when there's a device.exe designed for this.


2) Related to (1), you need your driver/"service" to take xxx_PowerUp and xxx_PowerDown notifications.  Unfortunately some sample services implement xxx_PowerUp/xxx_PowerDown and may make you think that a service can receive this.  Services.exe itself is notified when power notifications come in but it will not pass these on to your service for various reasons.  Device.exe will pass these on.


3) You need direct access to something else that lives in device.exe.  A firewall component that examines every incoming IP packet as it arrives is a good example.  While this isn't touching hardware, it does need to talk to the TCP/IP stack.  Having each packet go across process to be examined would kill performance, which is why the firewall is loaded by device.exe.


4) Something in device.exe needs your component to be up and running before it can proceed.  At boottime, all device drivers are started up before the first service is.


A good (or not so good) example of where Microsoft has really screwed this one up is with the notification API service.  Notification API basically lets apps register to be notified when certain system events happen or when some time event occurs.  Since there's no hardware interaction, seems this should live in services.exe, right?  Wrong.  It turns out a ton of stuff in device.exe needs this API too.  These drivers have to go into a lame polling mode to make sure that the notification service has started already.


As a semi-random side note, in the next CE version we are going to do the right thing here in notification API.  In the interim, if you run into this problem yourself and you're an OEM you can set BSP_NOTIFY_IN_DEVICE=1 and run makeimg again to have notifications put back into device.  This isn't an officially tested scenario mind you, but I've had customers on the newsgroup do this and have never heard them complain.


[Author: John Spaith]

Comments (9)
  1. Andrew says:

    Hi John, first of all I just wanted to say that these posts by you and the rest of the team are, for me, pure gold, and I really wanted to say thanks for taking the time to post them!

    I would like to ask is there any good references around to implement a Services.exe process?

    I have found the following from a few years ago:

    http://www.pocketpcdn.com/articles/services.html

    Do you know of any other samples, (with downloadable source) that are around in SDK’s or the internet somewhere?

  2. cenet says:

    Andrew – happy the blog is helping you out.

    Let me understand your request – you want to implement a service DLL of some sort, and not implement services.exe yourself, right? For implementing the DLL, I briefly skimmed your link and (without making legally binding promises about its accuracy :)) it looked quite good.

    There’s also my white paper at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnce50/html/implement_network_service.asp?frame=true

    If you have access to Platform Builder, we have a bunch of sample service source (like ftp, telnet, …) in %_WINCEROOT%publicserverssdksamples.

    John Spaith

  3. Andrew says:

    Yes, I should have been more clear! I want to look into implementing a service DLL.

    I will try and get access to the platform builder too, although between the article I mentioned and your whitepaper, I should be able to work it out. 🙂

    Can I ask how you will ensure that the next version of services.exe will not access physical hardware? (just out of curiousity really)

  4. Hi John,

    I found an evaluation CD with the Platform Builder for CE 5.0, and the source you pointed to is great! Between that, the article and your whitepaper, I certainly have all I need for the moment.

    Yes, I am looking into building a service DLL to run in the background.

    If I could ask one more question, would you be able to tell me if there are any future plans to add provision for the Compact Framework for services.exe. Would it even be possible to write Services.exe in managed code? From the content of the general articles in this blog, I would strongly guess ‘no’, but no harm in asking right? 🙂

  5. cenet says:

    A managed version of services.exe is something we sometimes kick around, but isn’t something we’re planning in the next release. Most likely if we did implement this, there would be a services.managed.exe which would be distinct from the services.exe running native services.

    For how we’ll prevent touching physical hardware, in CE 5.01 (which is what WM 2005 is built on) and before services.exe could pretty much do anything that any DLL in device.exe or the kernel itself could do. It may have had to call SetKMode() first, but then it could party hard. That’s not going to be the case anymore – services will have lower privelege in the future.

    I’d recommend posting tech questions on the newsgroup. I’m happy to help on blog, but the blog interface is clunkier and I don’t check blog feedback as frequently as newsgroups.

  6. Nidhi says:

    I m frequently getting device.exe exception in my Smart phone application.Please help me if somebody knows about that.

  7. cenet says:

    Nidhi – I’d recommend posting this to a newsgroup listed on http://blogs.msdn.com/cenet/archive/2005/12/05/500181.aspx, the CENet blog isn’t really a good forum for questions like this but the newsgroups are.

    When you post, I’d recommend getting as much information as you possibly can – what does your app do prior to getting exception, call stack if possible or anything else of that nature.

    John

  8. Jeremy says:

    I am not a computer whizz. I own a Qtek 9000 which runs on WM5 (which seems also to be called WinCE 5.01). I have the classic memory problem, which is that storage memory keeps going to critical.

    I have tried:

    Rewriting the registry so IE caches on the storage card. This made IE crash constantly, so I reversed the change and now regularly purge the cache myself.

    Moving ALL large files to the storage card. In fact moving most small files too.

    I have got Resco installed (for the registry edits), and Agenda One (for the calendar), but not much else. These failings are driving me CRAZY when the Qtek ought to be a perfectly practical piece of equipment.

    Like I say, I’m an amateur. When my memory dropped from 2.5 to 1MB for no apparent reason, I realised I needed an expert on the case.

    Thanks for reading this. Jeremy

  9. cenet says:

    Jeremy – sounds like you know what you’re doing pretty well for an amateur!  Anyway I’d recommend you check out microsoft.public.pocketpc, it’s probably a better forum for a discussion like this.  I’m afraid I don’t know much about the details of your problem here.

Comments are closed.

Skip to main content