Tuesday, March 27, 2007

Pocket PC services: Creation, deployment and debugging

Often Pocket PC programs need a background process and most of existing Pocket PC programs implement it by creating an executable file which is started during system start-up. But Pocket PC has 32 processes limit and having many different programs running in background can be a serious problem. Pocket PC 2003 provides special mechanism implemented in Services.exe that allows Pocket PC developers to shape their background applications into services that run in one process as different threads.

Many Pocket PC applications need a background process. For example:

  • A program that shows a window when SD card is inserted

  • A permanent backup program

  • A program that control GPRS traffic

  • A Web server

  • A program that adds a special icons to the taskbar

Now almost all Pocket PC developers create an executable file and put shortcut to this file to \Windows\StartUp so this program is started just after reset and runs in background. This approach has a serious problem because number of processes in Windows CE is limited by 32. For example in XDA II Pocket PC devices there are 28 processes running just after soft-reset. If you install 2-3 third-party programs that need background process you will have only 1-2 processes for running programs!
Microsoft has introduced a solution for this problem in Windows Mobile 2003. Windows Mobile 2003 supports services which are DLLs run in one process (services.exe) as different threads. It solves the problem but:
1. Most Pocket PC developers do not know about this technology.
2. Services are not properly documented. For example you will not find necessary headers in Pocket PC 2003 SDK.
3. This technology is not supported on Pocket PC 2000 and Pocket PC 2002. So you cannot use services if you want to create programs that will work on both Pocket PC 2002 and Windows Mobile 2003 for Pocket PC.
This article describes how to solve these problems and how to create and distribute services for Pocket PC that will work on all Pocket PC versions.
Pocket PC service interface is similar to Pocket PC driver interface. Pocket PC service is a DLL that should export a set of functions and services.exe process loads these DLLs and initializes them by calling one of the functions.


Creating Service DLL

  1. In Microsoft eMbedded Visual C++ create new WCE Dynamic-Link Library project
  2. Add exported functions definitions (you should use your own prefix instead of MYS)
DWORD MYS_Close(DWORD dwData)
{
return 0;
}
DWORD MYS_Deinit(DWORD dwData)
{
return 0;
}
DWORD MYS_Init(DWORD dwData)
{
return 1;
}
DWORD MYS_IOControl(
DWORD dwData,
DWORD dwCode,
PBYTE pBufIn,
DWORD dwLenIn,
PBYTE pBufOut,
DWORD dwLenOut,
PDWORD pdwActualOut)
{
return 1;
}
DWORD MYS_Open(
DWORD dwData,
DWORD dwAccess,
DWORD dwShareMode)
{
return 0;
}
DWORD MYS_Read(
DWORD dwData,
LPVOID pBuf,
DWORD dwLen)
{
return 0;
}
DWORD MYS_Seek(
DWORD dwData,
long pos,
DWORD type)
{
return 0;
}
DWORD MYS_Write(
DWORD dwData,
LPCVOID pInBuf,
DWORD dwInLen)
{
return 0;
}
MYS here is a prefix that is to be specified further by application calls to Services.exe (here MYS stands for MY Service).
  1. Add these functions names to your project .def file export table (you should use your own prefix instead of MYS):
Exports;
Explicit exports can go here
MYS_Close
MYS_Deinit
MYS_Init
MYS_IOControl
MYS_Open
MYS_Read
MYS_Seek
MYS_Writeb
  1. This is now time to add some specific initialization code to MYS_Init function.
Most likely you need to create a new thread here that will encapsulate all service logic. To do this defines thread controlling function. That function is the right place to create additional windows, timers or any other application-specific needs. In the sample given we create a timer and run thread message loop.
unsigned long __cdecl MyControllingFunction( LPVOID pParam )
{
g_nTimer = SetTimer(0, 0, 10 * 1000, MyTimerProc);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
DWORD MYS_Init(DWORD dwData)
{
HANDLE hThread = CreateThread( 0, 0, MyControllingFunction, 0, 0, 0);
return 1;
}
Be sure to return non-zero value from MYS_Init function as zero return value indicates service initialization failure and causes service DLL to be unloaded imediately.
Depending on application needs user can put some logic to the other MYS_... methods in the same manner.


Service Registration in Registry

In order to have service automatically started at system startup you should add an arbitrary named subkey to the registry HKEY_LOCAL_MACHINE\Services\MyService key (Service key is the name of the service, user can use there other names also) and specify the following values:
Order: REG_DWORD
Order in which Services.exe will load each service. The service with the lowest order is loaded first.
Dll : REG_SZ
Dynamic-link library (DLL) files to be loaded. Only file name without path. This DLL should be located in \Windows folder.
Keep : REG_DWORD
Must be 1 for services that should run in background, if Keep = 0, the DLL will be unloaded immediately after initialization.
Prefix : REG_SZ
Prefix of your functions exported from the service DLL (instead of xxx in xxx_Init, etc). Must be 3 symbols.
Index : REG_SZ
Service index (set to 0).
DisplayName : REG_SZ
Display service name.
Description : REG_SZ
Description of display service.
Context : REG_DWORD
Initial value passed into the initialization routine (must by 0).
For example, for the Sample MFC-based service to start as a service at boot time, the following registry key should be used.
 
[HKEY_LOCAL_MACHINE\Services\MyService]
"Dll"="MYS.dll"
"Order"=dword:8
"Keep"=dword:1
"Prefix"="MYS"
"Index"=dword:0
"Context"=dword:0
"DisplayName"="Sample Service"
"Description"="Sample Service"
User can create a C # application to register the service DLL.


Installing Pocket PC Service

To install a service you should copy your service DLL into the \Windows folder. From eVC IDE select the Pocket PC 2003 device as target platform and build the project, it will deploy the DLL into \Windows folder. For some devices while deploying the IDE may show “CE Platform PocketPC 2003 does not match remote OS version 421. Continue?” error message, neglect the warning and press “Yes”.


Start Service

Once the service is installed and registered, the PDA need to be restarted to start the service DLL.


C# code to start and stop the service DLL

Sometimes user may need to start or stop the service from code, such as backup service where before creating the backup or restoring it, we may pause the service.
[DllImport("coredll")]
private extern static int ActivateService(string lpszDevKey, int dwClientInfo);
[DllImport("coredll")]
private extern static int GetServiceHandle(string szPrefix, string szDllName, int pdwDllBuf);
[DllImport("coredll")]
private extern static bool DeregisterService(int hDevice);
Above are the declarations for the API functions that would require activating and deactivating the service.
If you want to deploy modified version of already deployed and running DLL, before deploying the new DLL, services.exe should be terminated because it won’t allow replacing existing DLL in running mode. To kill services.exe the steps are.
  1. From eVC Tools menu select Remote Process Viewer.
  2. Select your device from Select the Device popup.
  3. Select the Services.exe from Services section.
  4. Click Kill Process (X) button on the toolbar.

Activate the Service

int handle = ActivateService(“MyService”, 0);
The above code starts the service DLL.
First parameter is registry entry of the service; here it is “MyService”.
Second parameter is reserved and it should be Zero.

Stop the Service

int handle = GetServiceHandle ("MYS" + "0:", null, 0);
DeregisterService (handle);
GetServiceHandle function returns the handle of the service DLL.
The first parameter is the prefix of the service with index number; here prefix is “MYS” and index number is “0”
Second parameter is optional that contains the name of the service DLL.
Third parameter is also optional specifies the number of bytes written or the number of bytes required if the buffer is not large enough.
DeregisterService stops the instance of the service provided as handle.

Debugging Pocket PC Service

To debug your service you can attach to the services.exe process. You have to use the eVC++ 4 debugger for that because the eVC++ 3 debugger does not support attaching to remote processes. To attach the debugger to the services.exe process you need a local copy of the services.exe file. The user can copy the services.exe from the /Windows folder of PDA.
You can attach to the services.exe process using the following step-by-step instructions:
  1. Open your service project in Microsoft eMbedded C++ 4.0.
  2. Add Debugbreak() calls in proper place of your code. (Setting breakpoints in your code will not help.)
  3. Build debug configuration and upload your DLL to the device.
  4. Add registry values desired as described above.
  5. Start your service on the device (e.g. soft-reset).
  6. Attach to the services process (Build / Start Debug / Attach to WCE process...).
  7. Connect to your device.
  8. Select "services.exe" as the process to debug. Browse for your local copy of services.exe binary image.
  9. As soon as your service stops at the breakpoint debugger loop for each service DLL in services.exe as soon as it ask for local copy of your DLL, provide the local copy of DLL.
  10. It will take some time (can be from few seconds to few minutes) to reach the code still breakpoint in the service DLL.
  11. Start debugging.

Monday, March 26, 2007

Memory Profiler: Generate stat file for .NETCF 1.0 application

Remote performance monitor is available with .NETCF 2.0 to monitor the performance of application developed on the .NETCF 2.0, we can also monitor the performance of application developed on .NETCF 1.0, but it won't be real time, here a stat file will be generated for the application.

Steps to generate stat file

  • Download attached zip file.
  • Open the PMonTest solution in .Net 2005
  • Run the project, Select Pocket PC 2003 Second Edition Emulator to deploy the PMon Test application.
  • Next task is to edit the Registry. To generate the .stat file for the applications,
  • Key named as PerfMonitor need to add under
  • HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\.NETCompactFramework\\
  • Under this key one sub key named as Counters need to be added & set its value = 1
  • Set Registry button will do the step no 3 & 4.
  • First task is to read the above subkey, to do so press Read Registry button.If the subkey is missing it will show “NullReferenceException” otherwise it will show “Performance Counter On/Off” as per the value of the Counters subkey.
  • If Counters subkey is missing or its value is “0”, Press Set Regstry button , it will set the Counters value equal to “1”.
  • Press Exit Button to close the application.* don’t press the x button of the form; it will not close the application. To generate the .stat file application needs to be closed.
  • Now open any application for which you want to generate the stat file or you can open the same PMonTest application. Its path is \PMonTest .Open the PMonTest.exe. & press either Read Registry or Show Date button.
  • Close the application by pressing the Exit Button
  • Now go to root of your device, it will have stat file for your application, so if your application name is TestApp, the stat files name would be TestApp.stat.
  • Import the stat file on your computer using .Windows CE Remote file viewer.
  • Open this file in NetCFRPM.exe, the file is located in “C:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\bin” folder.

Friday, March 16, 2007

OpenNETCF's Smart Client Offline application block

Now I am back from Virtual earth to mobile application development & task was to design a component to handle connection and data upload and download, it should able to work in online and offline modes.

While goggling for the possible solutions I came across application blocks by OpenNETCF, OpenNETCF is not new to me , I have already tried the Exception management application block provided by them & I found that they have also provides the application block for Offline, actually all these application blocks are inspired by application blocks provided by Microsoft but OpenNETCF provides these for .NET compact framework. I must say a great work by OpenNETCF poeples but felt that it lacks a small thing & i.e. how to use these complex & enormous application blocks.

After try & error methods I able to compile the error free version of the application block ( warnings were neglected ;) as usual)

I tried to use it on sample application & it stared screwing me up. Hell the call goes from one assembly to another & functions receives interfaces as input parameter (I was really horrified by this ), the designed might have included all these complexities to server the actual need of components but my god its really not mentioned any where that how to use it AS IT IS or from where you can start. Apart from these difficulties I got error in configuration section (God knows why these peoples needs config file for an pocket pc application).



so Still searching for a good alternative.

How to get source image from site created using mapcruncher?

Last week I were told by one of my colleagues to include the image of London 2012 in our POC project for Virtual Earth API's. The site was showing the Olympic stadiums to be build in London.

So first task was for me to extract the source image from that site, first feeling said it is not possible but the xml structure that microsoft research provides for map cruncher projects shows me the path to do so!

  • First of all consider that the site name is www.xyzCrunch.com where the crunched map is hosted.
  • Next step is to get the MapLayers.Crunched xml from this site, so it would be www.xyzCrunch.com/MapLayers.crunched.xml This file contains the name of crunch project in its header against attribute "SourceMashupFilename"; assume it is SourceData/xyzCruch.yum.xml.
  • Now we got the project file name, so open the project xml file , it would be www.xyzCrunch.com/SourceData/xyzCruch.yum.xml.
  • The name of the image file gets stored with each layer and the name of the image file is stored as value for SourceMapFilename attribute under SourceMap element node.The name of the file is xyz.png.
  • Now last step, we have file name & we will get that file from www.xyzCrunch.com/SourceData/xyz.png.
Remember that the Sourcedata folder contains the Map Cruncher project(.YUM) in the xml format & it also contains the source images for each layer.

So, Enjoy Crunching!

Friday, March 2, 2007

"'VEMap' is undefined "error

Today morning I faced a strange problem in my Crunched VE application, it was working fine till yesterday, but today when I tried to load it, it shown a html error 'VEMap' is undefined .
Quite strange that the other VE application that uses js from virtual earth site was working fine but this application which uses js from research.microsoft.com was giving error.

I also noticed that mapcruncher site was not operational, after googling this problem on the code project site I got a post with same problem, which tells about Fiddler. A too which logs all http traffic between computer and internet, so using it I found out that the following files not getting download on my computer

  • http://research.microsoft.com/mapcruncher/scripts/v4/MapControl-CVE-obfuscated.js
  • http://research.microsoft.com/mapcruncher/scripts/v4/CrunchControl.js
  • http://research.microsoft.com/mapcruncher/scripts/v4/LegendStyle.css
The site is still down :(.