Reboot a device

This blog entry (courtesy of Robert Levy) shows how to programatically reset a device:-

Here is the VB.NET equivalent:-

Declare Function KernelIoControl Lib “coredll.dll” (ByVal dwIoControlCode As Integer, ByVal lpInBuf As IntPtr, ByVal nInBufSize As Integer, ByVal lpOutBuf As IntPtr, ByVal nOutBufSize As Integer, ByVal lpBytesReturned As Integer) As Integer

Declare Sub SetCleanRebootFlag Lib “coredll.dll” ()


Public Sub HardReset()

Dim IOCTL_HAL_REBOOT As Integer = &H101003C

Dim bytesReturned As Integer = 0


KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, IntPtr.Zero, 0, bytesReturned)

End Sub


To perform only a soft-reset exclude the call to SetCleanRebootFlag (Thank’s to Alex Feinman for highlighting this in a recent newsgroup post)

Back from San Francisco

I’ve got back home from MDC, my immediate task is to clear the backlog of emails and serious sleep deprivation! I’ll also be pulling together all my notes on the event for a Smartphone / Pocket PC Thoughts article.

The event included a great mixture of short term and longer-term announcements. Windows Mobile 2003 Second Edition brings support for a greater range of form factors for devices. After some deliberation I decided not to get my iPaq 2210 flashed as the beta image excluded any Bluetooth support, which would cripple the device which I use day-to-day. I have of course installed the Emulator images to target the platform. Longer term, Visual Studio 2005, .NET Compact Framework 2.0 and SQL Server Mobile are shaping up to be a powerful set of tools and technologies, and I can’t wait to play with the preview version of Visual Studio 2005 that was handed out!

Free space on Storage Cards

You can determine the total size of a Storage Card and available free bytes using the GetDiskFreeSpaceEx API function. Below is a “mini-wrapper” around the function which returns a structure with the three return values. You’ll notice in this example I’m marshalling longs (64bit Integers), values greater than 32bit cannot be marshalled by value but can be marshalled by reference as in this case. The DiskFreeSpace struct contains three long members to hold the accessible free bytes, total bytes and total free bytes.

public static DiskFreeSpace GetDiskFreeSpace(string directoryName)
DiskFreeSpace result = new DiskFreeSpace();

if(!GetDiskFreeSpaceEx(directoryName, ref result.FreeBytesAvailable,

        ref result.TotalBytes, ref result.TotalFreeBytes))
throw new Win32Exception(Marshal.GetLastWin32Error(), “Error retrieving free disk space”);
return result;

public struct DiskFreeSpace
public long FreeBytesAvailable;

public long TotalBytes;

public long TotalFreeBytes;

private static extern bool GetDiskFreeSpaceEx(string directoryName,
ref long freeBytesAvailable,
ref long totalBytes,
ref long totalFreeBytes);


Or if you prefer Visual Basic:-

Public Function GetDiskFreeSpace(ByRef directoryName As String) As DiskFreeSpace

    Dim result As New DiskFreeSpace

    If GetDiskFreeSpaceEx(directoryName, result.FreeBytesAvailable, result.TotalBytes, result.TotalFreeBytes) = False Then

        Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), “Error retrieving free disk space”)

    End If

    Return result

End Function

Public Structure DiskFreeSpace

    Public FreeBytesAvailable As Long

    Public TotalBytes As Long

    Public TotalFreeBytes As Long

End Structure

Private Declare Function GetDiskFreeSpaceEx Lib “coredll.dll” (ByVal directoryName As String, ByRef freeBytesAvailable As Long, ByRef totalBytes As Long, ByRef totalFreeBytes As Long)