Click here to Skip to main content
14,970,700 members
Articles / Mobile Apps / Windows Mobile
Posted 11 Feb 2008


41 bookmarked

Service Browser Application for Windows Mobile

Rate me:
Please Sign up or sign in to vote.
4.42/5 (11 votes)
11 Feb 2008CPOL4 min read
Helper application for managing services running under Services.exe process on Windows Mobile platform.



One of the projects I have been working on recently required a background process running on a Smartphone device. Windows Mobile SDK provides the programming model for writing services that run in Services.exe process. I have decided to use that model for my project. During development/debugging of the service, I needed a way to start/stop my service while I have a Visual Studio attached to the Services.exe process on the target device. This article shows how to create an application that allows managing services running on a Smartphone device.


Service Browser was developed using Visual Studio 2005, Windows Mobile 6 SDK Standard and Windows Template Library (WTL) 8.0. The application was tested on the MOTO Q 9h Smartphone and Visual Studio 2005 emulators.

Using the Code

Download CAB and install it on your device or use the provided solution to build the application. Make sure you have SDK and WTL installed.


Services.exe reference from the SDK documentation explains all the steps that are required to create and register a service. We need to know how a service is registered, loaded and unloaded from Services.exe process.

Registration is done by writing an arbitrary key to the Registry under HKEY_LOCAL_MACHINE\Services. That key must have certain values that are described below:

OrderREG_DWORDOrder in which Services.exe will load each service. The service with the lowest order is loaded first.
DllREG_SZDynamic-link library (DLL) file to be loaded. File name with the extension only. This DLL should be located in \Windows folder.
KeepREG_DWORDMust be 1 for services that should run in background, if Keep = 0, the DLL will be unloaded immediately after initialization.
PrefixREG_SZPrefix for service functions exported from the service DLL. Must be at least 3 symbols.
IndexREG_DWORDService index
DisplayNameREG_SZDisplay service name
DescriptionREG_SZDescription of display service
ContextREG_DWORDValue passed into the initialization routine

NOTE: There is no information on what values are optional and what values are required. From personal experience Order, Dll, Keep Prefix, Index and Context must be provided everything else is optional. I have also seen that some of the services had other different values in the Registry that are service specific.

In order to load a service to the Services.exe process, the application must call ActivateService function:

HANDLE ActivateService(
  LPCWSTR lpszDevKey,
  DWORD dwClientInfo

The first parameter of this function is the key under which the service is registered in the Registry. The second parameter should be set to 0. This function loads the service and returns a handle to the service.

To unload a service application can use the handle that was returned by ActivateService and call DeregisterService function:

BOOL DeregisterService(
  HANDLE hDevice

In case when an application doesn't have a valid handle to the service, the handle can be obtained by using GetServiceHandle:

HANDLE GetServiceHandle(
  LPWSTR szPrefix,
  LPWSTR szDllName,
  DWORD pdwDllBuf

The first parameter is the most important parameter. It specifies the prefix key for the service in the form XXXN:, where XXX is the Prefix that was specified in the Registry for that service and N is the Index from the Registry as well.

NOTE: I have seen that some of the services use prefixes that are more than 3 characters in length, but only the 3 first characters are used by the system. The documentation states that GetServiceHandle is a depreciated function and CreateFile should be used instead with the exception when handle is obtained for the purpose of deregistering the service.

Another way of obtaining a service handle is to enumerate all loaded services using EnumServices function:

BOOL EnumServices(
  PBYTE pBuffer,
  DWORD * pdwServiceEntries,
  DWORD * pdwBufferLen

In order to numerate all running services the EnumServices has to be called twice. First with pBuffer set to NULL and pdwBufferLen set to 0. The function will return the required size for pBuffer and number of running services. Second call to EnumServices with the valid pBufffer of the right size will return information about running services. The returned buffer consists of two parts according to the documentation. We will be using the first part which is a total of pdwServiceEntries ServiceEnumInfo structures:

Typedef struct_ServiceEnumInfo {
  WCHAR szPrefix[6];
  WCHAR szDllName;
  HANDLE hServiceHandle;
  DWORD dwServiceState;
} ServiceEnumInfo;

Now we have enough information to build the application.


Please use WTL Mobile Application Wizard under "Other Languages/Visual C++/Smart Device". In this application, I have used "Dialog base" application type with the "Orientation aware" checked. For a detailed step by step description of using the wizard, please refer to my other article: GPS and Web Service using C++ ATL/WTL (Windows Mobile 6, Standard).

The core of the application is the list of available services which is generated by reading all the keys from the Registry under HKEY_LOCAL_MACHINE\Services. EnumServices is used to mark which services are "running" (loaded). Loading and unloading of the selected service from the list is done by using "Start" and "Stop" menu items.

VOID CServiceBrowserDialog::RefreshServices()

    //Get services information from the registry
    CRegKey rkServices;
    LONG lRes = rkServices.Open(HKEY_LOCAL_MACHINE, _T("Services"));

    //Registry Key/Value name
    DWORD dwIndex = 0;
    DWORD dwNameLength = 64;
    TCHAR szBuffer[64] = {0};

    //Registry Values we are interested in
    CString strPrefx(_T("Prefix"));
    CString strDll(_T("Dll"));
    CString strDisplayName(_T("DisplayName"));
    CString strDescription(_T("Description"));
    CString strIndex(_T("Index"));
    CString strOrder(_T("Order"));
    CString strKeep(_T("Keep"));
    CString strContext(_T("Context"));

    //Registry Value data and type
    DWORD dwType = 0;
    DWORD dwValueIndex = 0;
    BYTE pData[256] = {0};
    DWORD dwDataLength = 256;

    //Enumerate through services subkeys
    while( rkServices.EnumKey(dwIndex++,szBuffer,&dwNameLength) == ERROR_SUCCESS )

        //Open each Service key
        CRegKey rkService;
        if( rkService.Open(rkServices,szBuffer) == ERROR_SUCCESS )
            CServiceInfo * pServiceInfo = new CServiceInfo();
            if( !pServiceInfo )

            pServiceInfo->m_strName = szBuffer;

            //We will reuse buffer for name
            szBuffer[0] = 0;
            dwNameLength = 64;

            dwType = 0;
            dwValueIndex = 0;
            pData[0] = 0;
            dwDataLength = 256;

            //Get all values for each registered service
            while( RegEnumValue(rkService,dwValueIndex++,szBuffer,
	     &dwNameLength,NULL,&dwType, pData, &dwDataLength) == ERROR_SUCCESS )
                switch( dwType )
                case REG_SZ:
                    ATLTRACE(_T("\t%s - %s\n"),szBuffer, pData);
                    if( strPrefx.CompareNoCase(szBuffer) == 0 )
                        pServiceInfo->m_strPrefix = (TCHAR*)pData;
                    else if( strDll.CompareNoCase(szBuffer) == 0 )
                        pServiceInfo->m_strDll = (TCHAR*)pData;
                    else if( strDescription.CompareNoCase(szBuffer) == 0 )
                        pServiceInfo->m_strDescription = (TCHAR*)pData;
                    else if( strDisplayName.CompareNoCase( szBuffer ) == 0 )
                        pServiceInfo->m_strDisplayName = (TCHAR*)pData;
                case REG_DWORD:
                        DWORD dwValue = *((LPDWORD)pData);
                        ATLTRACE(_T("\t%s - %d\n"),szBuffer, dwValue);
                        if( strIndex.CompareNoCase(szBuffer) == 0 )
                            pServiceInfo->m_dwIndex = dwValue;
                        else if( strKeep.CompareNoCase(szBuffer) == 0 )
                            pServiceInfo->m_dwKeep = dwValue;
                        else if( strOrder.CompareNoCase(szBuffer) == 0 )
                            pServiceInfo->m_dwOrder = dwValue;
                        else if( strContext.CompareNoCase(szBuffer) == 0 )
                            pServiceInfo->m_dwContext = dwValue;

                //Reset everything for next iteration
                szBuffer[0] = 0;
                dwNameLength = 64;

                pData[0] = 0;
                dwDataLength = 256;

            //If there is no "Prefix" it is not a valid service key
            if( pServiceInfo->m_strPrefix.IsEmpty() )
                delete pServiceInfo;
                pServiceInfo = NULL;
                                pServiceInfo->m_dwIndex == -1 ? 0: 
                m_services.insert(  InfoPair(pServiceInfo->m_strKey, pServiceInfo) );

        szBuffer[0] = 0;
        dwNameLength = 64;

    //Let's get information about running services
    DWORD nServices = 0;
    DWORD nBufferSize = 0;
    PBYTE pBuffer = NULL;
    ServiceEnumInfo * pInfo = NULL;

    EnumServices(NULL, &nServices, &nBufferSize);

    if( nBufferSize != NULL && nServices != 0 )
        pBuffer = (PBYTE)malloc(nBufferSize);

        nServices = 0;

        if( EnumServices(pBuffer,&nServices, &nBufferSize) && nServices != 0 )
            pInfo = (ServiceEnumInfo *)pBuffer;    

    if( pInfo )
        InfoMap::iterator serviceIter;

        for( int i = 0; i < nServices; i++ )
            serviceIter = m_services.find( pInfo[i].szPrefix );

            if( serviceIter != m_services.end() )
                //Save service handle
                serviceIter->second->m_hHandle = pInfo[i].hServiceHandle;
                ATLASSERT( serviceIter != m_services.end() );

    if( pBuffer )

VOID CServiceBrowserDialog::CleanUp()

    InfoMap::iterator serviceIter = m_services.begin();

    while( serviceIter != m_services.end() )
        CServiceInfo * pServiceInfo = serviceIter->second;
        delete pServiceInfo;


LRESULT CServiceBrowserDialog::OnStart(WORD /*wNotifyCode*/, 
	WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    INT nSelected = m_list.GetSelectedIndex();
    if( nSelected != -1 )
        CServiceInfo * pInfo = (CServiceInfo*)m_list.GetItemData(nSelected);
        HANDLE h = ActivateService(pInfo->m_strName, 0);

    return 0;

LRESULT CServiceBrowserDialog::OnStop(WORD /*wNotifyCode*/, 
	WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    INT nSelected = m_list.GetSelectedIndex();
    if( nSelected != -1 )
        CServiceInfo * pInfo = (CServiceInfo*)m_list.GetItemData(nSelected);
        HANDLE h = GetServiceHandle(CT2W(pInfo->m_strKey), NULL,0);

        BOOL bRet = DeregisterService(h);

    return 0;



  • February 11, 2008 - Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Valentin Ivanov
Chief Technology Officer CDYNE Corporation
United States United States
Developer, Maker and Tinkerer

Comments and Discussions

QuestionHow to build a service program Pin
Willian Hall31-Oct-08 19:24
MemberWillian Hall31-Oct-08 19:24 
AnswerRe: How to build a service program Pin
Amvrel21-Sep-09 9:55
MemberAmvrel21-Sep-09 9:55 
GeneralRMS Pin
Edyb25-Aug-08 2:40
MemberEdyb25-Aug-08 2:40 
AnswerRe: RMS Pin
Valentin Ivanov2-Sep-08 10:52
professionalValentin Ivanov2-Sep-08 10:52 
GeneralStart browser from app Pin
Edyb16-Jul-08 4:14
MemberEdyb16-Jul-08 4:14 
AnswerRe: Start browser from app Pin
Valentin Ivanov16-Jul-08 4:19
professionalValentin Ivanov16-Jul-08 4:19 
GeneralRe: Start browser from app Pin
Edyb16-Jul-08 20:45
MemberEdyb16-Jul-08 20:45 
GeneralStatus Pin
khass518519-Mar-08 10:31
Memberkhass518519-Mar-08 10:31 
AnswerRe: Status Pin
Valentin Ivanov21-Mar-08 5:19
professionalValentin Ivanov21-Mar-08 5:19 
GeneralRe: Status Pin
khass518521-Mar-08 8:14
Memberkhass518521-Mar-08 8:14 
AnswerRe: Status Pin
Valentin Ivanov21-Mar-08 8:59
professionalValentin Ivanov21-Mar-08 8:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.