Category: NETCF

  • Windows CE Services and Compact Framework

    Neither .NETCF v1.0 or v2.0 will support runtime hosting, this means it’s not possible to write a service in managed code which will run within the Services.exe process. However it is often useful to be able to query and manipulate other system services from managed code.


    An example of this is where a system service needs to be stopped so that you can access some hardware – such as the OBEX listener service on Windows Mobile which opens the Infrared port. With this scenario in mind I set about wrapping some of the CE functionality for managing services, using the desktop framework as a model.


    The resultant library – InTheHand.ServiceProcess.dll is designed as a subset of System.ServiceProcess.dll – this is the assembly which is added as a reference to your project when you create a windows service project with the desktop framework. The library includes a single class ServiceController which is created with the identifier of the service you which to attach to. Under Windows CE these services have an identifier in the form of a device name e.g. AAAN: where AAA is the prefix, often providing a clue as to the purpose of the service, and N is a numerical index. Returning to our example of OBEX the service is called OBX0:. If you don’t know the name of a particular service you can either trawl through the registry under HKLMServices, or better still call the ServiceController.GetServices static method. The final consideration is to ensure you close/dispose the ServiceController when you have finished working with it. Our OBEX shutdown code looks like this:-


    ServiceController scObex = new ServiceController(“OBX0:”);
    scObex.Stop();

    while(scObex.Status!=ServiceControllerStatus.Stopped)
    {
        System.Threading.Thread.Sleep(1000);
    }

    scObex.Close();


    Notice that the WaitForStatus method isn’t supported so in this case we periodically check the status while sleeping. If we weren’t bothered when the service shutdown you could leave out this section, even when you call Close() to close our handle to the service it will continue with it’s action and shutdown in it’s  own time.


    In terms of platform support, this services.exe model is supported on CE.NET 4.x and above so this includes Windows Mobile 2003 and above.


    You can download the library here:-


    http://www.inthehand.com/Services.aspx

  • Restricting Pim Item Collections

    This applies equally to WindowsMobile In The Hand and the Windows Mobile 5.0 APIs. On the collection classes there exists a Restrict method which returns a subset of the collection matching your search query. You can use the resulting collection to databind or otherwise enumerate over.


    The query language used is similar, but not the same, as SQL: if you imagine that what you are entering here is just the WHERE clause of a query you will not be far off. The key rules to follow are:-



    • Field names must match exactly – refer to the Properties of the Appointment, Contact and Task class for valid field names
    • Field names must be enclosed in square brackets e.g. [MobileTelephoneNumber]
    • You can combine multiple expressions with AND and OR
    • Supported operators are < (Less Than), <= (Less Than or Equal To), > (Greater Than), >= Greater Than or Equal To), = (Equals), <> (Not Equals)
    • Items without the property set will not be returned in a query. So if you set a query of [BusinessAddressCity] = “London” it would not return items without the business address city present even though these are not London.
    • You can use this knowledge to form a query to return all records where the property is set with [PropertyName] <> “” as all items with the property set will match this pattern.

    You can return a full contact list for all items with a mobile number using this code:-


    os = new OutlookSession();

    PimItemCollection pic = os.Contacts.Items.Restrict(“[MobileTelephoneNumber] <> “””);

    listBox1.DataSource = pic;


    listBox1.DisplayMember = “FileAs”


    This example bind the collection to a control, the list will show the contact name only but you’ll be able to retrieve the Contact object from the SelectedItem property of the list box to use it’s other properties (e.g. the mobile number).


    For Windows Mobile 5.0 you can use the new ChooseContactDialog which features a RequiredProperties property so you can easily setup the dialog to show only relevant items.


    Update: Here is the VB equivalent:-


    os = New OutlookSession
    Dim pic As PimItemCollection = os.Contacts.Items.Restrict(“[MobileTelephoneNumber] <> “ & Chr(34) & Chr(34))
    ListBox1.DataSource = pic
    ListBox1.DisplayMember = “FileAs”


     

  • NDoc helper for Microsoft.WindowsCE.Forms

    If you’ve tried documenting .NETCF libraries which make use of Compact Framework specific types you’ll know that you have to do all sorts of workarounds and special builds to get an output which can successfully run through NDoc. This is because although the device specific assemblies such as Microsoft.WindowsCE.Forms are marked retargetable there isn’t an equivalent in the desktop framework to retarget to.


    One solution is aggressive conditional compilation to hide all hints of of these device classes. Another way which is cleaner and easier to deal with when you are regularly updating code and documentation is to build your own dummy classes into your NDoc configuration of your project. I’ve done this for documenting my own code as I often find myself using MessageWindows etc in my projects. Ratherthan everyone re-invent the wheel you can use the file (see link below) as-is. You will still be producing a separate dll for documentation purposes but the amount of code you have to hack around with is greatly reduced.


    Simply add the attached cs or vb file to your project. Then when you build the project in Debug or Release configuration absolutely nothing is added into your dll which will reference Microsoft.WindowsCE.Forms normally. But when you do an NDoc build and define the NDOC conditional constant it will magically spring into life and create a local dummy copy of the whole Microsoft.WindowsCE.Forms namespace. Because of the way the compiler chooses which class to reference your local version will take preference over the official library, thus removing all dependency on the Microsoft.WindowsCE.Forms library.


    Microsoft.WindowsCE.Forms.zip (1.27 KB)


    For this to work in VB there are a couple of additional points, firstly make sure you don’t have a Root Namespace configured (Project > Properties). You will also need VBCommenter to build the XML output used to feed Ndoc (Visual Studio 2005 will introduce built in XML comments generation for VB.NET projects).


    Of course the same technique can be used for other device specific assemblies such as System.Data.SqlServerCe, although it contains a lot more types and so is less practical to do in this way. You can instead use this technique just with the individual types/methods you use…

  • Bluetooth Socket Options on Windows XP

    Windows XP supports a much smaller number of socket options for Bluetooth than Windows CE, although it generally provides alternative ways to get/set those properties. The table below shows those that are supported on XP and how they relate to their Windows CE equivalent. Notice that the only one which behaves the same on both platforms is SO_BTH_ENCRYPT




























    Windows XP


    Windows CE


    #define SO_BTH_AUTHENTICATE 0x80000001  // optlen=sizeof(ULONG), optval = &(ULONG)TRUE/FALSE


    #define SO_BTH_AUTHENTICATE                                    0x00000001    // optlen=0, optval ignored


    #define SO_BTH_ENCRYPT      0x00000002  // optlen=sizeof(ULONG), optval = &(ULONG)TRUE/FALSE


    #define SO_BTH_ENCRYPT                                               0x00000002    // optlen=sizeof(unsigned int), optval = &(unsigned int)TRUE/FALSE


    #define SO_BTH_MTU          0x80000007  // optlen=sizeof(ULONG), optval = &mtu


     


    #define SO_BTH_SET_MTU                                                0x00000006    // unconnected only! optlen=sizeof(unsigned int), optval = &mtu


    #define SO_BTH_GET_MTU                                               0x00000007    // optlen=sizeof(unsigned int), optval = &mtu


    #define SO_BTH_MTU_MAX      0x80000008  // optlen=sizeof(ULONG), optval = &max. mtu


     


    #define SO_BTH_SET_MTU_MAX                                    0x00000008    // unconnected only! optlen=sizeof(unsigned int), optval = &max. mtu


    #define SO_BTH_GET_MTU_MAX                                    0x00000009    // bound only! optlen=sizeof(unsigned int), optval = &max. mtu


    #define SO_BTH_MTU_MIN      0x8000000a  // optlen=sizeof(ULONG), optval = &min. mtu


    #define SO_BTH_SET_MTU_MIN                          0x0000000a    // unconnected only! optlen=sizeof(unsigned int), optval = &min. mtu


    #define SO_BTH_GET_MTU_MIN                         0x0000000b    // bound only! optlen=sizeof(unsigned int), optval = &min. mtu


    The current version of the Bluetooth.NET library doesn’t include the XP versions in the BluetoothSocketOptionName enumeration, but I have added these for the next release.

  • Comparing Windows Mobile APIs

    To illustrate how the WindowsMobile In The Hand is a subset of the Windows Mobile 5.0 APIs I’ve modified this class diagram for the Windows Mobile 5.0 APIs to ghost out those which are not supported in WindowsMobile In The Hand on earlier devices. The key differences are in support for system state events and in the number of properties supported – there is no easy way to do events for these system properties without a lot of polling which is bad. Also it lacks many new properties on the PIM items (such as Photos for contacts) including the ability to add custom properties – this is based on a lot of work on the underlying POOM APIs which would not be feasable to replicate on older devices.

    wm5vwmith1.jpg (679.87 KB)

    In a future post we will look at what is added beyond the standard Windows Mobile 5.0 APIs.

  • Retrieve IMEI Through TAPI

    Earlier I posted to the newsgroup a description of using Alex Feinman‘s Tapi Wrapper to retrieve the IMEI of the phone device. When I checked again, the specific required method is not included in the wrapper, however as Alex has made the line handles accessible it’s easy to tag functionality onto the library without re-inventing the wheel. I put together a VB example to P/Invoke the lineGetGeneralInfo method to retrieve information about the phone device. If you use this on a Smartphone device you may find it doesn’t work – this is because many Smartphone devices require signing to access many of the TAPI methods. I tested this on a HTC Phone Edition device


    TapiVBExample.zip (52.74 KB)


     

  • No KnownColors In NETCF

    A requested feature for NETCF is to retrieve a list of named colors. Fortunately this is easily done using reflection to read all the public statis properties of the Color class:-


    System.Reflection.PropertyInfo[] colors = typeof(Color).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
    Color[] allcolors = new Color[colors.Length];


    for(int i = 0; i < colors.Length; i++)
    {
        allcolors[i] = (Color)colors[i].GetValue(null,null);
    }

    comboBox1.DataSource = allcolors;


     


    In this example the full list is assigned to a ComboBox for display purposes.

  • Compiled Documentation for OpenNETCF Libraries

    With the release of the Smart Device Framework v1.3 the compiled help documentation was no longer included in the installer. I’ve built a standalone installer to install the full collection of documentation into your Visual Studio help. This release includes:-


  • OpenNETCF Smart Device Framework v1.3
  • OpenNETCF Application Blocks v1.0
  • OpenNETCF Desktop Communication
  • Microsoft.WindowsCE.Forms

  • The full package is approximately 5mb but once installed makes it much quicker to search than using the online version. It installs with either Visual Studio 2003 or Visual Studio 2005 Beta, Download it here.

  • Using the Clipboard

    This is a very basic sample app to assist a thread in the newsgroup. Basically it shows a couple of approaches to working with the Clipboard, both using SDF v1.3.


    The first is the TextBoxEx class onto which I’ve added a ContextMenu with edit commands. The second is a plain TextBox and I’ve added buttons to call methods on the OpenNETCF.Windows.Forms.Clipboard class.


    In .NETCF v2.0 there will be basic functionality on the Clipboard class but it wont have the GetText and SetText shortcut methods. The OpenNETCF.Windows.Forms.Clipboard class follows the desktop v2.0 equivalent so should be fairly intuitive to use.


    The sample project was written and tested with a Pocket PC 2003. On brief testing with the 2002 emulator the context menu causes problems due to the way a tap and hold is handled and the control loses focus, thus the selection is removed, therefore no text is cut to the clipboard. I have yet to try on Pocket PC 2000/2002 device so I’d be interested in hearing if there are any problems encountered. It requires SDF v1.3 and I also recommend you are running the latest .NET Service Pack (SP3).

    ClipboardTest.zip (9.32 KB)

  • SDF 1.3 Known Issues

    Firstly an apology to recent visitors to our website, we’ve just moved to a new server and are experiencing teething problems, so currently OpenNETCF forums and blogs are not working.


    This post is designed to highlight a few issues found with v1.3 and workarounds (if possible), I expect we will produce a refresh release shortly to resolve these:-



    • OpenNETCF.Configuration – in order to use these classes you’ll need to replace the windowsconfigmachine.config file with an amended version which correctly references the v1.3 runtimes (http://www.peterfoot.net/OpenNETCFConfigurationV13Workaround.aspx)
    • OpenNETCF.VisualBasic – This assembly is unusable in the compact framework as it references the desktop version of mscorlib.dll. No current workaround (other than using the functionality from elsewhere in the SDF since all of the methods here are wrappers to other parts of the SDF
    • OpenNETCF.WindowsCE.Forms – It appears this assembly is not correctly registered in the GAC on devices, this will cause a TypeLoadException when you reference a class from this library. To workaround this you can manually copy the assembly to your program folder on the device e.g. over activesync or add to your project as “Content” file
    • All assemblies appear as “Smart Device Framework” in the add reference dialog. If you scroll across (you may need to extend the columns) to the path you’ll be able to determine the name of each assembly.
    • IDE Plugins for v1.3 may not correctly install on your machine. You may find the custom menu items don’t do anything when clicked. You can still access all of the SDF and Application Blocks functionality by manually adding references to your project through the Add Reference dialog
    • (Update) Integrated help is not included in the v1.3 installer. However I’ve built a standalone installer with compiled help for all our libraries.

    Please use our bug submission tool if you encounter other errors, and try to give as much detail as possible. We will try and make arrangements for a refresh release shortly to overcome these issues, thanks for your patience.