# Sunday, May 21, 2006

Take a Shortcut

There are a number of ways you can make use of Shortcuts within your project. You may create a shortcut to your application during your CAB file installation - either on the Start Menu or perhaps in the \Windows\Startup folder. VS2005 makes this easy because it has a nice file system graphical editor for CAB projects. Another way you can use shortcuts is within your code itself. There are two API functions to support this - SHCreateShortcut and SHGetShortcutTarget. By P/Invoking these from your .NETCF application you can create your own launcher applications which reuse the shortcuts setup for your Start Menu for example.

There are some caveats when using these functions though which you need to be aware of, the documentation has been revised since the Pocket PC 2003 SDK to warn you of this. To understand why let's have a quick look at the contents of an LNK file:-

34#"\My Documents\Templates\Memo.psw"

 

As you can see it's a very simple text format. The number indicates the length of the path, this is followed by the # symbol then the full path to the target. When the path contains a space it must be wrapped in quotes. So what were those caveats? Well whatever path you pass to SHCreateShortcut it doesn't add these quotes how you would expect - you end up with:-

34#"\My" Documents\Templates\Memo.psw

 

This path is invalid and the shortcut won't work. The solution is to wrap any path you pass into SHCreateShortcut with quotes to begin with. Even if the path doesn't contain spaces this will give you a valid shortcut.

The second caveat comes with the SHGetShortcutTarget API. When this reads the shortcut file it doesn't strip out the quotes if present, this means your returned path cannot be used as-is with any of the System.IO functionality. The solution to this one is very simple - perform a Trim() on the returned string passing in the " character - this will remove the leading and trailing quotes and leave you with a valid path. If your shortcuts are to executables and also have arguments specified you will need to split the quoted path from any following arguments.

To demonstrate both of these techniques I've attached a sample VB.NET project. It can create and list shortcuts and when you tap on an item it will launch the target document. For a true launcher application there are some additional steps, for example retrieving the Icon for the file, these are not included in this sample.

Shortcuts.zip (7.27 KB)
#    Comments [0] |
# Tuesday, May 09, 2006

32feet.NET v2.0 Beta 1 Available

I've posted a Beta build of 32feet.NET to the Files section of the site. Please read the file description for a high-level list of changes. This release is the first to be built against .NET v2.0 and will resolve issues encountered with using the v1.x library from a .NET v2.0 VB project. It also consolidates the previously separate dlls into a single dll (InTheHand.Net.Personal.dll) which reduces the overall footprint to 83kb for Infrared, Bluetooth, Object Exchange and Forms functionality. If you are at MEDC and would like to see this version in action, Nick Landry will be using it as part of his session on Thursday at 11:15 "APP335 Tablet PCs and Smartphones: Working Hand-in-Hand to Enable Your Mobile Workforce".

#    Comments [0] |
# Monday, May 08, 2006

Here in Las Vegas for MEDC2006

I'm partly blogging to show I haven't fallen off the face of the earth (yesterday was a near miss though), and partly to plug MEDC2006 which is starting today in Las Vegas.

Being somewhat of a cheapskate I opted for an indirect flight here, the first leg, a mere 8 hours or so from London to Minneapolis went almost without a hitch (in fact we were early), it was all downhill from there. The second flight loaded up and taxied out towards the runway. We then had a message from the captain to tell us that his pre-flight checks indicated that the plane had no hydraulic fluid at all, which could be a bit of a bugger for steering the plane. Thankfully he decided that it would be a bad idea to try. So we then taxied back to the terminal and piled off the plane. Luckily there was another plane available, although because it was a subtly different flavour of 757 they had to reassign many of the seats. So that makes it my 16th plane in the last 2 months, I think after Vegas I'll have a bit of a break from air travel for a while!

So now although still tired I'm ready and looking forward to the cool stuff this week has to offer. I'll be helping out on the MVP Cabana on Wednesday and Thursday, and hanging out with fellow MVPs and OpenNETCF folk.

#    Comments [0] |
# Monday, February 27, 2006

Bluetooth Profiles

This is a post I've been meaning to do for a couple of years and just never got around to it. The table below is an attempt to document the Bluetooth profiles supported on various flavours of Windows. Because Windows CE is modular don't expect every CE device to support all the possible profiles, this table gives an indication of what the platform creators had available to them. In order to conserve space I've made copious use of acronyms (and initialisations), the current release versions of all of these profiles are documented on the public Bluetooth website.

XP SP2 Vista CE.NET 4.2 WM 2003 WM 2003 SE CE 5.0 WM 5.0 WM 5.0 AKU1 WM 5.0 AKU2
A2DP                 Y
AVRCP                 Y
HFP   ?  o o o/Y Y Y Yp Y
HSP   ?  Y o o/Y Y Y Yp Y
HID Y Y Y     Y Y Y Y
FTP     Y     Y      
PAN Y Y     Y      
LAP     Y     Y      
BPP                  
HCRP Y Y              
DUN Yc Yc Y Ys Ys Y Ys Ys Ys
OPP Y Y Y Y Y Y Y Y Y
SPP Y Y Y Y Y Y Y Y Y
c = Client support, s = Server support, o = OEM implemented, p = adds phonebook support, ? Vista recognises these profiles but they don't seem to be used.

What I haven't attempted to do is compare the profiles available on 3rd Party stacks, that would be a little more complex :-)

#    Comments [3] |
# Thursday, February 23, 2006

Writing Provisioning XML for Windows Mobile

This recent article discussed one of the ways you can use XML provisioning data - through a cab file installation. You can also pass the XML to DMProcessConfigXML or ConfigurationManager.ProcessConfiguration in the managed world. But how about creating the XML?

Well you could use notepad and code it all by hand as a worst-case-scenario. The next best thing is to use the XML support in Visual Studio which will at least help to ensure you write valid XML. Mobile In The Hand 2.0 takes this one step further by adding a schema into VS2005 to ensure your document conforms to the msprov schema:-

1. Create a new XML document in your project

2. With the document selected locate "Schemas" in the properties pane.

3. Click "..." to bring up a list of installed schemas

4. Select msprov from the list

5. Start writing your document and watch the intellisense kick in

#    Comments [0] |
# Friday, February 17, 2006

Contact.WebPage

The Managed APIs in Windows Mobile 5.0 expose the Contact.WebPage as a Uri. The problem with this approach is that the user forms are free text and you can enter anything into this field. Probably 9 times out of 10 you'll enter an address such as

www.peterfoot.net

And because the managed API passes this to the Uri constructor and it has no http:// prefix it fails and returns null. However there is a solution to workaround the issue. You can access the Properties collection and access the property and it will be returned as a string. So for example you would use:-

[C#]
string webPage = session.Contacts.Items[0].Properties[Microsoft.WindowsMobile.PocketOutlook.ContactProperty.WebPage];

[VB]

Dim webPage As String = session.Contacts.Items(0).Properties(Microsoft.WindowsMobile.PocketOutlook.ContactProperty.WebPage)

If you then want to use a Uri with this value, check to see if it includes the http:// prefix and if not add your own e.g.

Uri contactsWebpage = new Uri("http://" + webPage);

 

#    Comments [0] |
# Monday, February 13, 2006

OpenNETCF.Windows.Forms.Control2 in SDF 2.0

In SDF v1 I wrote a class called ControlEx which would allow you to host a native windows control within a managed Control. This was the subject of an MSDN article (of which much of the concept applies here too). The implementation was rather convoluted to work around the limits of .NETCF v1.0, and the control was constructed with a Control, which hosted a MessageWindow with some of it's style bits altered which in turn hosted the native control. Luckily hosting controls is easier in NETCF v2.0 but unlike the desktop this functionality is still not built into the base Control class. Which is where Control2 enters the stage.

Like several other classes throughout the SDF the name has been changed to use the 2 suffix, but there are a few other changes I want to draw your attention to. First the inevitable disclaimer, what I'm going to describe won't work with the current Beta release, as I was building the sample code for this post I realised there were a few "nice" features lacking to add a bit more reusable functionality into the Control2 class itself. So lets proceed with how the architecture has changed, and then work through specifics with a sample.

The fundamental feature in .NETCF v2.0 which enables the new architecture is the ability to marshal a managed function as a function pointer to native code. Using this we are able to handle our wndproc in a managed method, this removes the need for the MessageWindow class. Control2 is derived from Control and when added to a form it hooks up a method to process its own windows messages. These are passed to a virtual WndProc method which can be overridden in a derived control to handle specific messages. After this is hooked up the CreateControl method is called, this takes the window class details returned from CreateParams and creates the window as a child of our Control2. This means than in it's simplest form you only need to override CreateParams with a valid class name to host your control.

However Control2 provides only basic functionality to interact with this class - for example automatically resizing when you resize the managed control, and getting/setting the control text. Beyond this you have two ways to interact with the control - add your own properties and methods to your Control2 derived class and from these send windows messages to the native control, and secondly add your own handlers for incoming messages from the control to create events.

For a basic example I've wrapped the CAPEDIT control which is part of WindowsMobile - this is simply a textbox which can Automatically Capitalize The First Letter Of Every Word. Because CAPEDIT is a common control it requires a call to InitCommonControlsEx - there is a static helper method in Control2 to help with this:-

Shared Sub New()

   Control2.InitCommonControls(&H2000)

End Sub

By the way as you've probably noticed, this and the rest of the example are in VB.NET, I would hope that it is self-explanitory enough for you to apply to C# as required.

Interestingly we don't need to worry about the constructor for the control at all, as we don't do anything special. Where the magic happens is in our override for the CreateParams property:-

Protected Overrides ReadOnly Property CreateParams() As OpenNETCF.Windows.Forms.CreateParams

   Get

      Dim cp As CreateParams = MyBase.CreateParams

      cp.ClassName = "CAPEDIT"

      Return cp

   End Get

End Property

And that's it, all we did was change the ClassName in this case. The other parameters such as size are already set for you based on the current size of your managed control.

So as I alluded to earlier, the example is a very simple class so there aren't any events to show you, just a couple of properties which are implemented like so:-

Private allcaps As Boolean = False

Public Property UpCaseAllWords() As Boolean

   Get

      Return allcaps

   End Get

   Set(ByVal value As Boolean)

      Dim val As IntPtr

      If (value) Then

         val = New IntPtr(-1)

      End If

      Dim m As Microsoft.WindowsCE.Forms.Message = Microsoft.WindowsCE.Forms.Message.Create(Me.childHandle, 1025, val, IntPtr.Zero)

      Microsoft.WindowsCE.Forms.MessageWindow.SendMessage(m)

      allcaps = value

   End Set

End Property

The magic value of 1025 was determined from the commctrl.h header file. I should proobably have created it as a const with the same name as the native implementation. One of the great things about VS2005 is that when you build a project with such a control in it, you'll get an automatic addition to your toolbar for it. We haven't added any designer support (outside the scope of this article) but you can set descriptions, categories and the toolbox icon for the control in the usual way. By default you'll get a rectangle with the name of the control which you can position on your form, in many cases this might be good enough. The sample project shows the control dropped on a form with a couple of checkboxes to toggle the properties. Also as a sanity check there is a button labelled Copy which copies the text value from the native control to a regular TextBox - remember we didn't write any code to do that, it's built into Control2 (well in a post-Beta release at least!). The standard behaviour of the CAPEDIT control is to have no border, but you can change this by setting the BorderStyle to BorderStyle.FixedSingle for a plain 1-pixel border.

Control2Example.zip (9.07 KB)

Wait what have I missed, oh yes there are two other useful properties - DesignMode allows you to check if your control is currently in a designer so you can for example not call device specific code, and ModifierKeys is a static property which gives you the current state of the Shift/Ctrl/Alt modifier keys, if your device has a "proper" keyboard.

Stop Press:-

Exhibit A - A CAPEDIT control in a managed app:-

#    Comments [0] |
# Friday, February 03, 2006

MEDC 2006 Websites are Live

The conference website is now live at www.medc2006.com. Registration is now open along with details of the pre-conference session. Full session details for the conference tracks are not up yet, but you can subscribe to the RSS feeds for Application Development, Embedded and Device Management tracks. See you in Vegas in May!

And for those of us in Europe the European MEDC conference which will take place in June in Nice also have a website and registration is now open - http://www.microsoft.com/europe/medc/

#    Comments [0] |
# Tuesday, January 31, 2006

Local Newspapers

Reading a local newspaper is always a joy, I stumbled on this wonderful (and presumably accidental) headline which I thought I'd share:-

(No school children were hurt during the making of this news article)

#    Comments [1] |
# Saturday, January 28, 2006

OpenNETCF.IO in SDF 2.0

As well as the revolutionary changes in the SDF v2.0, some of which are simply only made possible by improvements in .NETCF v2.0, we have used this opportunity to make a number of evolutionary changes too. For example I'm going to take a quick trip through the file functionality in the OpenNETCF.IO namespace:-

DriveInfo - This is a new class which mimics functionality new in the desktop framework v2.0 and replaces previous separate classes for StorageCard and ObjectStore information. You can use it in two ways, use the constructor which takes a path and pass in the path of your storage device e.g. "\" or "\Storage Card" or you can use the static GetDrives() method which will return an array of all devices. A pleasant side-effect of this is that you can data-bind to this array of DriveInfo objects.

OpenNETCF.IO.DriveInfo[] di = OpenNETCF.IO.DriveInfo.GetDrives();
dataGrid1.DataSource = di;

File2 - To follow accepted naming behaviour we have stopped using Ex as a name suffix and so instead our classes which overlap existing classes in .NETCF have the 2 suffix. I have added some additional helper methods which are found on the desktop to quickly read/write whole files - ReadAllText / ReadAllLines and WriteAllText / WriteAllLines.

File2.WriteAllText(filename, textBox1.Text);

FileSystemWatcher - We've made some improvements here so that all the various event combinations are correctly raised. Also we have built in designer support for VS2005 so you can drop a FileSystemWatcher onto your form and hook up the events in the designer.

#    Comments [0] |
# Thursday, January 19, 2006

GetDeviceUniqueID For VB

To complement the C# version posted Here,  here is a working VB translation:-

<System.Runtime.InteropServices.DllImport("coredll.dll")> _
Private Shared Function GetDeviceUniqueID(ByVal appdata As Byte(), ByVal cbApplictionData As Integer, ByVal dwDeviceIDVersion As Integer, ByVal deviceIDOuput As Byte(), ByRef pcbDeviceIDOutput As Integer) As Integer
End Function

Private Function GetDeviceId(ByVal appData As String) As Byte()

Dim appDataBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(appData)
Dim outputSize As Integer = 20
Dim output(19) As Byte

Dim result As Integer = GetDeviceUniqueID(appDataBytes, appDataBytes.Length, 1, output, outputSize)

Return output

End Function

#    Comments [0] |

Indigo Reaches Beta 2 And Gets Go-Live License

Indigo (Yeah I know I'm supposed to call it Windows Communication Foundation), has reached the Beta 2 milestone. With this release you can apply for a Go-Live license to deploy the technology in live environments*

Read all about it here:-

http://msdn.microsoft.com/winfx/getthebeta/golive/

*Terms and conditions apply. Your small print may go down as well as up.

#    Comments [0] |
# Sunday, January 15, 2006

Any Port in a Storm

A user posted an important observation on the Bluetooth COM support in Windows Mobile 5 here on the microsoft.public.pocketpc.developer newsgroup. The problem is that while the system allows you to create virtual COM ports for all your paired devices which support Serial Port Profile, most of the time this will fail because the COM port you pick is already in use, on many devices you can have just 2 virtual COM ports. I've posted a few of my thoughts in the thread linked above, but while there are things that Microsoft can work to improve there are also a few things that developers can do to avoid the problem:-

  • Don't rely on the control panel for setting up ports, use the RegisterDevice route (or BluetoothSerialPort in 32Feet.net) to just register a port for the duration you need it (and possibly reuse the same port name). This also allows you to use a different port prefix since few COMx: ports are available out of the box.
  • Don't use serial ports. Unless you need your device to be accessible by a legacy app then you can avoid using virtual COM ports altogether and work directly with Sockets (again 32Feet.net will allow you to setup these connections easily). The downside with this approach is it applies only to the Microsoft Bluetooth Stack, as the programming model for the main alternative from Broadcom is essentially based around ports. However based on the first batch of devices released with Windows Mobile 5.0 the Broadcom stack is a lot less common.
#    Comments [0] |
# Tuesday, January 10, 2006

MEDC 2006 Europe

[via Jason Langridge]

MEDC will be returning to a dedicated conference for Europe this year, last year saw a number of one day events and in 2004 MDC was a thread within Tech Ed Europe.

 

Details are fairly basic at this early stage but if you are interested I suggest signing the "guestbook" so that you will be notified as more information becomes available.

#    Comments [0] |
# Sunday, January 08, 2006

Determine WM5.0 AKU Version Programmatically

The full build and AKU version of a device is shown on the Start > Settings > System > About screen, however what if you want to determine the version from your code. Well starting with Windows Mobile 5.0 there is now a registry key which holds the AKU version e.g.

RegistryKey kAku = Registry.LocalMachine.OpenSubKey("System\\Versions");

string akuVersion = kAku.GetValue("Aku", "");

kAku.Close();

This version will include a leading period so for example if the string is ".0.1.1" as on the current JasJar ROM the device has AKU 0.1, if it reads ".1.1.0" as on the current K-Jam ROM you have AKU 1.1. As far as I'm aware all shipping devices will have at least 0.1 as some fixes were implemented between RTM in May 2005 and the first shipping devices released later in the year. If your version is ".2.x.x" then you are a very lucky bunny indeed!

Whilst Windows Mobile 2003 had a similar concept of periodic AKU updates throughout it's lifetime this registry key is not available, nor is the AKU version displayed on the About dialog, only the Build number is displayed.

#    Comments [0] |