# Thursday, October 04, 2007

How To: Use the FtpWebRequest

Networking In The Hand includes a full desktop-compatible implementation of the FtpWebRequest class. This plugs into the WebRequest class so that calling WebRequest.Create() with an FTP Uri will create an object of type FtpWebRequest. Because FTP support isn't built into the Compact Framework you have to register the class with this prefix using the the following code (only required once in your code):-

InTheHand.Net.FtpWebRequest.RegisterPrefix();

We have already looked at performing simple operations with the WebClient class. for more complex FTP operations you can use the FtpWebRequest directly. For example retrieving the modification date of a specific file:-

FtpWebRequest requestDate = (FtpWebRequest)FtpWebRequest.Create(requestUri);
requestDate.Method = WebRequestMethods.Ftp.GetDateTimestamp;
if (wc.Credentials != null)
{
   requestDate.Credentials = wc.Credentials;
}
FtpWebResponse responseDate = null;

responseDate = (FtpWebResponse)requestDate.GetResponse();

if (responseDate != null)
{
   MessageBox.Show(responseDate.LastModified.ToString());
   responseDate.Close();
}

#    |
# Tuesday, October 02, 2007

How To: Use the WebClient

Networking In The Hand includes the WebClient class which is a helper class which makes it easier to do uploading and downloading of data using HTTP and FTP transports. For example rather than creating an HttpWebRequest, setting a number of properties, getting the response and reading the response stream and copying the data into a file, why not use DownloadFile to perform a single operation to write the data from a specific Uri to a local file:-

WebClient wc = new WebClient();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
   wc.DownloadFile(uriTarget, saveFileDialog1.FileName);
}

Additional methods provide the ability to download a string, and download a byte array containing the data. There are a similar set of operations for uploading:-

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
   wc.UploadFile(uriDestination, openFileDialog1.FileName);
}

Not only does this work with the HTTP transport, but also with the FTP support which is also part of the library - the only difference is the Uri that you pass in. If your site requires a username/password you can set the Credentials property of the WebClient once and they will be used for all subsequent operations.

#    |

Fix: SMS Interception on T-Mobile Dash

A number of customers have been reporting issues with SMS interception on the T-Mobile Dash handset. On these devices the SMS interception does not work through either the Microsoft or In The Hand class libraries. This is specific to the T-Mobile ROM as other branded versions of the same device, such as the HTC S620, do not have the problem. The culprit is a badly behaved IMessageFilter implementation which stops the standard interceptor from working. You can fix this issue by removing the entry from the registry. Take a look at this registry key:-

HKEY_LOCAL_MACHINE\Software\Microsoft/Software/Inbox/Svc/Sms/Rules

In here you'll find 2 entries by default on Windows Mobile 5.0:-

{1000BC1C-F4A3-4210-B197-4AEBF2CEE6F5}

{77990A0E-60B8-4103-B9AF-17157E4274FD}

If you find additional entries here try deleting them (make a backup first so that you can restore if necessary) then reset the phone. This should allow the normal SMS interception mechanism to work again.

On Windows Mobile 6 the above registry key is not present by default, therefore the standard SMS interception will work without any additional rules registered.

#    |
# Monday, October 01, 2007

How To: Use the Ping class

Networking In The Hand includes the Ping class (In the InTheHand.Net.NetworkInformation namespace). The component allows you to determine if a network path to a particular host is available and whether the host is responding. It doesn't guarantee that a particular service is running on the server (HTTP, FTP etc). You can perform a Ping in a couple of lines of code, the class has been designed to be an exact subset of the equivalent class in the full .NET framework. The following is an example of the simplest ping request using a default payload and timeout settings.

InTheHand.Net.NetworkInformation.Ping p = new InTheHand.Net.NetworkInformation.Ping();
InTheHand.Net.NetworkInformation.PingReply reply = p.Send("www.google.com");

if (reply.Status == IPStatus.Success)
{
   string message = string.Format("Address: {0}\r\nRoundTrip time: {1}\r\nTime to live: {2}\r\nDon't fragment: {3}\r\nBuffer size: {0}",
      reply.Address.ToString(),
      reply.RoundtripTime,
      reply.Options.Ttl,
      reply.Options.DontFragment,
      reply.Buffer.Length);

   MessageBox.Show(message, "Ping");
}
else
{
   MessageBox.Show(reply.Status.ToString(), "Ping");
}

The class allows you to further customise the request. You can specify your own data payload for example, or set a different timeout value (the default is 5 seconds).

#    |
# Friday, September 28, 2007

How To: Use the AuthenticationDialog

Networking In The Hand 2.0 offers a new dialog which allows you to prompt the user for networking credentials. The dialog is designed for the best appearance depending on your platform, for example the screenshots below show both Windows Mobile 5.0 Pocket PC and Windows Mobile 6 Standard Landscape:-

 

   

If your application stores authentication details you can pre-populate the dialog. Optionally you can display a Save Settings checkbox on the form. The following code snippet shows how to display the dialog and return the credentials as an ICredentials object:-

private ICredentials GetCredentials(string resourceName)
{
   InTheHand.Windows.Forms.AuthenticationDialog ad = new InTheHand.Windows.Forms.AuthenticationDialog();

   ad.ResourceName = resourceName;
   if(ad.ShowDialog() == DialogResult.OK)
   {
        return new NetworkCredential(ad.Username, ad.Password, ad.Domain);
   }
   else
   {
      return null;
   }

}

#    |
# Friday, September 21, 2007

New Networking Component

In The Hand Ltd today released Networking In The Hand, a developer library for the .NET Compact Framework 2.0 and 3.5 Beta. This library adds additional networking functionality not found in in the Compact Framework while matching the object model used in the full .NET framework to make it easy to share your code between platforms. Functionality includes:-

  • InTheHand.Net.WebClient - Provides helper methods for working with information upload and download over HTTP and FTP transports, and any other WebRequest based implementations.
  • InTheHand.Net.FtpWebRequest - Provides a complete desktop compatible implementation of the WebRequest pattern for FTP.
  • InTheHand.Net.WebRequestMethods - Provides a reference of all the HTTP and FTP methods (GET/POST etc)
  • InTheHand.Net.NetworkInformation.Ping - Perform a Ping and determine network availability and performance.
  • InTheHand.Net.NetworkInformation.IPGlobalProperties - Retrieve a wide range of networking statistics to measure traffic and help identify faults.

The library is licensed on a per-developer basis with no additional royalties required to distribute the runtime with your applications. Full details on the product can be found on the product details page - http://inthehand.com/content/Networking.aspx. A fully functionality Evaluation version is available to download - http://inthehand.com/files/folders/evals/entry4014.aspx.

#    |

Stephen Fry is Blogging

Stephen Fry, intellectual and comic hero has not only started blogging, but started talking about mobile devices. Definitely worth a read.

(from Dave Sussman)

#    |

Friday Fun: Untamed Workforce

There are videos and photos doing the rounds from a new spoof information site for Windows Mobile which takes a light-hearted look at mobile working. Why not take a look?

 

#    |
# Monday, September 03, 2007

HTC S620 Windows Mobile 6 Upgrade Experience

Late last week HTC quietly released the Windows Mobile 6 upgrade for the S620. A few eagle eyed bloggers posted about it. The first thing I found was that the upgrade process doesn't work with Windows Vista, when it reboots the device into bootloader mode the computer fails to connect. I had to run the upgrade from a Windows XP machine (where it worked flawlessy). Upon rebooting I had a fresh clean Windows Mobile 6 device. I noticed that HTC had left the default "Mobile Operator" screen during boot up, only after running its initial setup was this removed. Everything was working great, I was able to setup my exchange server in ActiveSync and get all my PIM data back onto the device. The lock dialog is slightly altered in this release, rather than tapping the right softkey to cancel you have to tap the right soft-key to open the menu and select the second option (option 1 is "Reset Password" but is disabled). The reason I often cancel this screen is because I just want to check the home screen for the time / next appointment. The time can optionally be displayed on the unlock screen but only for the key lock, I no longer use that because my exchange provider enforces a password.

There are a few fancy UI features, the improvements to the calendar day screen are welcome along with the ability to edit task items. Essentially there are few differences to Windows Mobile 5.0 in the look and feel and layout of start menu and applications. One interesting change is that the alarm can now be set to operate only on weekdays, or every day. It's still not as flexible as other approaches, and the default sound applied to the alarm is rather alarming to say the least!

With my initial success I then tried docking the device with my main Vista machine. On this machine I'm running the latest Windows Mobile Device Center 6.1. When I connected the S620 popped up a message telling me to upgrade to ActiveSync 4.5 or later on my PC. Fellow MVP Jaap van Ekris was able to point out a simple workaround. Just go to Start > Settings > Connections > USB to PC and uncheck "Enable advanced network functionality". It now partners correctly with my Vista machine.

#    |
# Friday, August 31, 2007

Tech Ed Developers 2007 EMEA for Mobile and Embedded Developers

In November Microsoft will be hosting the twin conferences of Tech Ed Developers and Tech Ed IT Forum in Barcelona. Tech Ed Developers runs the week of 5th to 9th and covers 14 different tracks, one of which is Mobile & Embedded. This will cover the latest versions of all of Microsoft's Mobile & Embedded products from the .NET Micro Framework through to Windows Mobile, Windows Embedded CE and Robotics studio, and of course the .NET Compact Framework.

This year I'll be delivering a session on networking technologies available to Windows Mobile developers. This will cover a broad spectrum from personal area networking (Bluetooth and IrDA) through to cellular network specific (SMS, GPRS etc) and internet standards (FTP, HTTP etc). We will look at which technologies are appropriate for which scenarios, the relative costs and how to implement them. It's going to be an interactive session and will include plenty of code snippets and an interactive demo or two.

My co-authors Andy Wigley and Daniel Moth will also be delivering sessions in the Mobile & Embedded track and we plan to get together for a book signing during the event. Registration for the conference is available with a €300 off the full price until 28th September.

#    |
# Friday, August 24, 2007

MOTODEV Summit

Following on from the developer contest I mentioned a while ago, Motorola are running a series of developer summits later this year. They will be covering development across their range of platforms and devices, including Windows Mobile. They will have a UK event in London on the 9th of November, which conflicts with Tech Ed Developers 2007 in Barcelona which I'm already committed to - more details on that to follow...

Full details - MOTODEV Summit

#    |
# Tuesday, August 21, 2007

System.Media.SoundPlayer versus the PlaySound API

In .NETCF v3.5 there is a new namespace - System.Media which brings audio support to the Compact Framework. Previously the common way to add sounds to your application was to use the PlaySound API (either P/Invoking yourself or using one of many wrappers). Now that the framework itself has built-in support, which matches the desktop .NET v2.0 framework it makes sense to standardise on the SoundPlayer component.

There are some differences in behaviour which you'll need to be aware of. When you specify a filename this doesn't simply call through to the native APIs passing the filename, the file is first loaded into memory and then the WaveOut APIs are used to play it. This means that if you simply create a new SoundPlayer and call Play the file will not be loaded, the Play method will have to first load the file contents, and then play the sound. This will create a noticable delay before the sound is heard. The class allows you to load the file at any time prior to calling play - you can use either Load or LoadAsync to do this. Once the file is loaded the Play method will be able to immediately begin playing the file. Exactly where you call the Load/LoadAsync method will depend on your application design. Keeping a large audio file cached will tie up valuable memory, you should dispose of the SoundPlayer instance once you have finished with it.

#    |
# Tuesday, August 14, 2007

Using the UnInstall Configuration Service Provider

Among the Configuration providers in Windows Mobile is one which allows you to programmatically uninstall package from the device. The UnInstall provider is documented here:-

http://msdn2.microsoft.com/en-us/library/aa455977.aspx

To see if your package can be uninstalled you send the following XML - e.g. using DMProcessConfig.XML or ProcessConfiguration in managed code.

<wap-provisioningdoc>
   <characteristic type="UnInstall">
      <characteristic-query type="YourAppName"/>
   </characteristic>
</wap-provisioningdoc>

Replacing YourAppName with the name of your installation package usually "CompanyName Product" as defined in your device CAB project. The response will look something like this if the package name is found:-

<wap-provisioningdoc>
   <characteristic type="UnInstall">
      <characteristic type="YourAppName">
         <parm name="uninstall" value="0"/>
      </characteristic>
   </characteristic>
</wap-provisioningdoc>

Then you can uninstall the package using:-

<wap-provisioningdoc>
   <characteristic type="UnInstall">
      <characteristic type="YourAppName">
         <parm name="uninstall" value="1"/>   
      </characteristic>
   </characteristic>
</wap-provisioningdoc>

#    |

Desktop ActiveSync Registry Settings

In March I showed how to get the version of a connected device from the desktop. This post documents the rest of the registry settings used to store device information. There are two registry locations, the first at HKEY_CURRENT_USER\Software\Microsoft\Windows CE Services contains information about the currently connected device. The second, HKEY_CURRENT_USER\Software\Microsoft\Windows CE Services\Partners contains information for each of the partnerships established on the desktop PC.

When the device is docked, regardless of whether a partnership is established, the following keys are populated in HKEY_CURRENT_USER\Software\Microsoft\Windows CE Services:-

  • DeviceHardwareId -String containing a unique Guid for the device
  • DeviceOemInfo - OEM specific string (as returned by SystemParametersInfo using SPI_GETOEMINFO) e.g. WIZA200 (HTC Wizard)
  • DeviceProcessorType - DWORD value - 2577 (ARM) for all Windows Mobile devices
  • DeviceType - string containing platform type (as returned by SystemParamtersInfo using SPI_GETPLATFORMTYPE) e.g. PocketPC or SmartPhone
  • DeviceVersion - DWORD value - see my previous post.

The partnerships are stored with a unique DWORD partnership id e.g.

HKEY_CURRENT_USER\Software\Microsoft\Windows CE Services\Partners\12345678\

There are additional values to describe sync behaviour and some Windows Mobile Device Center specific values e.g. to specify the display icon for a device. Otherwise all of the above values are duplicated here for the partner device, except with some naming differences:-

  • DeviceHardwareId
  • OemInfo
  • ProcessorType
  • DeviceType
  • Version

Each partnership has a directory containing any resources used, this includes the icon etc. The path is retrieved from the DataFolder value. This is a path beneath the users roaming application data folder which you would get using System.Environment.GetFolder(System.Environment.SpecialFolder.ApplicationData)

The icon is specified in DeviceIconFile and if you append this to the path you'll have the full filename of the icon.

#    |
# Friday, August 10, 2007

Skydriving

Windows Live Folders is now Windows Live Skydrive and the beta has been extended to locales outside the US. The service provides a free 500mb of online storage for you to use however you want. Beyond the new look and feel and new name there is one great addition - a drag and drop upload tool which makes it much easier to upload multiple files. Previously you had to browse for files individually and upload in batches of 5. This is a great addition to the other Live services and gives you somewhere handy to store your own files online, as well as share public files with others.

#    |
# Monday, July 30, 2007
# Friday, July 27, 2007

Multiline Button

On the newsgroup somebody asked how to have multiple lines of text on a .NETCF Button control. The native control supports this but the appropriate style is not set by default. The following code snippet shows how to enable this:-

private const int BS_MULTILINE = 0x00002000;
private const int GWL_STYLE = -16;

[System.Runtime.InteropServices.DllImport("coredll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[System.Runtime.InteropServices.DllImport("coredll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

public static void MakeButtonMultiline(Button b)
{
    IntPtr hwnd = b.Handle;
    int currentStyle = GetWindowLong(hwnd, GWL_STYLE);
    int newStyle = SetWindowLong(hwnd, GWL_STYLE, currentStyle | BS_MULTILINE);
}

The usage is simple, just pass the specific Button control to MakeButtonMultiline:-

MakeButtonMultiline(button1);

Here is what the amended button looks like on the left (default appearance on the right)

The designer fully supports entering multiple lines for the Button Text property, just click the dropdown at the end of the field and it will pop up a window into which you can add linebreaks with your text.

#    |
# Wednesday, July 25, 2007

More POOM Anomalies

Every version of POOM (Pocket Outlook Object Model) brings great improvements, however there are always a few things which just have you screaming "Why!". One of those examples is the implementation of IItem::Edit. This method is used to open an item in edit mode, and is implemented on Windows Mobile 5.0 and above, with the exception of Appointments on Smartphone (Standard) devices. This makes no sense because otherwise the Appointment item follows the same pattern as the other item types. A dialog is implemented on the Smartphone/Standard platform and this has a perfectly usable edit screen - why this wasn't implemented in the API beggars belief. Below is a table of supported configurations for the various display methods:-

Method WM 5.0 Pocket PC WM 5.0 Smartphone WM 6 Professional WM 6 Standard
IAppointment::Display() Yes No Yes No
IContact::Display() Yes No Yes No
ITask::Display() Yes No Yes No
IItem::Display() (Appointment) Yes Yes Yes Yes
IItem::Display() (Contact) Yes Yes Yes Yes
IItem::Display() (Task) Yes Yes Yes Yes
IItem::Edit() (Appointment) Yes No Yes No
IItem::Edit() (Contact) Yes Yes Yes Yes
IItem::Edit() (Task) Yes No* Yes Yes

 

*WM5.0 Smartphone Tasks application doesn't support editing

#    |