Archive for the ‘.Net Compact Framework’ Category

As you might know TechEd Australia ended few weeks back. I am sure lot of you might not have the chance to visit it. The good thing is you can watch the recorded sessions online now by visiting the following site.

http://channel9.msdn.com/events/teched/australia/tech-ed-australia-2011

I think this will be mostly useful to people who didn’t visit TechEd and also to people who visited it to refresh their minds.

When we are regularly using databases, sometimes those will get corrupted. This happened to one of my applications, it was a Windows Mobile application with SQL Server CE database. Since it is running in a mobile it is bit difficult for us to fix the database using the desktop machine. I used the following .Net Compact framework code to fix the corrupted database while the database is in the mobile.

  1. private void btnRepair_Click(object sender, EventArgs e)
  2. {
  3.     Cursor.Current = Cursors.WaitCursor;
  4.     Cursor.Show();
  5.     // txtDBPath will contain the path to the database.
  6.     engine = new SqlCeEngine(@"Data Source=" + txtDBPath.Text + ";Password=DBPassword");
  7.     try
  8.     {
  9.         if (!engine.Verify())
  10.         {
  11.             Cursor.Current = Cursors.Default;
  12.             Cursor.Hide();
  13.             DialogResult result = MessageBox.Show("Database is corrupted. Do you want to repair?", "My App", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  14.             if (result == DialogResult.Yes)
  15.             {
  16.                 Cursor.Current = Cursors.WaitCursor;
  17.                 Cursor.Show();
  18.                 // You can also use RepairOption.DeleteCorruptedRows.
  19.                 engine.Repair(null, RepairOption.RecoverCorruptedRows);
  20.                 Cursor.Current = Cursors.Default;
  21.                 Cursor.Hide();
  22.                 MessageBox.Show("Database repaired successfully.", "My App");
  23.             }
  24.         }
  25.         else
  26.         {
  27.             Cursor.Current = Cursors.Default;
  28.             Cursor.Hide();
  29.             MessageBox.Show("Database is not corrupted.", "My App");
  30.         }
  31.     }
  32.     catch (Exception ex)
  33.     {
  34.         Cursor.Current = Cursors.Default;
  35.         Cursor.Hide();
  36.         MessageBox.Show(ex.Message, "Error!");
  37.     }
  38. }

Next time you get a corrupted SQL CE database try it out.

Do you know that you can programmatically stop the user changing the date and time of a Pocket PC device?

This is useful if your applications need to take time accurately for some reason from the device.

The following code will disable the user from accessing the date time changing settings using his Windows Mobile powered device.

  1. private void btnDisableClock_Click(object sender, EventArgs e)
  2. {
  3.     RegistryKey hklm = Registry.LocalMachine;
  4.     hklm = hklm.OpenSubKey(@"\Software\Microsoft\Clock\", true);
  5.     System.Byte[] offValue = new byte[1];
  6.     offValue[0] = 0x30;
  7.     hklm.SetValue("AppState", offValue);
  8.     lblTitle.Text = "Change Clock Status – Disabled";
  9. }

 

If you want to re-enable the setting, to make the user able to change the data and time use the below code.

  1. private void btnEnableClock_Click(object sender, EventArgs e)
  2. {
  3.     RegistryKey hklm = Registry.LocalMachine;
  4.     hklm = hklm.OpenSubKey(@"\Software\Microsoft\Clock\", true);
  5.     System.Byte[] offValue = new byte[1];
  6.     offValue[0] = 0x11;
  7.     hklm.SetValue("AppState", offValue);
  8.     lblTitle.Text = "Change Clock Status – Enabled";
  9. }
Recently I had the issue while debugging a smart device application. All my breakpoints stop functioning. But if I ran the application in the device emulator the breakpoints are working fine.
So it was occurring only when I debug in an actual device (In my case Pocket PC).
 
After some trying I found that the problem is in the .NET CF which was installed in the Pocket PC.
To fix the thing,
1. Stop all the applications running in the Pocket PC (Do a soft restart or use the Runnning Programs tab in the System -> Memory).
2. Un install the .NET CF from the Pocket PC and soft reset the Pocket PC again.
3. Install the .NET CF in to the Pocket PC. (Also note that you can make the Visual Studio install the .NET CF by starting an application from Visual Studio.)
4. Connect the Pocket PC to the computer using Active Sync and try debugging. In most cases now the breakpoints should work.
 
If this did not fix the issue you will have to cold reset the Pocket PC and also I would instruct you to reinstall the SDKs (Windows Mobile 5/6 Pocket PC SDKs) if you have any installed.

 

Recently I have seen that getting the MAC address of a Pocket PC network card using the .Net Compact Framework requires calling to the APIiphlpapi.dll‘. This is because the Windows Management Instrumentation (WMI) classes are not in Compact Framework. Otherwise you should have used WMI to get the MAC address.
But believe me that the process involved in Compact Framework to get the MAC address is complex. So as a suggestion I would say to use the OpenNETCF class libraries. You have to use OpenNETCF.Net and OpenNETCF to successfully get the MAC address. If you want to know what is happening underneath you also can download the OpenNETCF source code too. 
 
OpenNETCF web site is http://www.opennetcf.com, visit them and see.
 
The code for getting the MAC address of the first adapter using OpenNETCF would be,

‘ Creating a variable to hold all the retrieved adapters.

Dim

adpcol As OpenNETCF.Net.AdapterCollection = OpenNETCF.Net.Networking.GetAdapters

If adpcol.Count = 1 Then

‘ adpcol.Item(0).MacAddress will be returning in Byte stream, you have to use Bit Converter to convert the stream to string.

GetDeviceMAC = BitConverter.ToString(adpcol.Item(0).MacAddress)

Else

GetDeviceMAC =

"No Adapters Fetched"

End If

(You have to refer OpenNETCF.Net and OpenNETCF to make this code run.)

 

Recently I tried analyzing my applications’ performance running on a Pocket PC. This can be achieved using .Net Compact Framework (.Net CF) Remote Performance Monitor (RPM). .Net CF RPM provides facility to monitor applications which run on devices that are connected to the computer using Active Sync or by general network.

To monitor your device application performance you have to have at least .Net CF SP1 since .Net CF RPM will come with .Net CF Service Pack 1. If you have not yet installed .Net CF SPs then download the SP2 from Microsoft using the following link.

RPM consists of 3 main files.
1. NetCFRPM.exe – This is the application file which will run on your computer. This will show performance related data of the device application. This file can be found at C:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\bin if you installed the Service Packs to the default location.

2. netcflaunch.exe – This will give you the connection parameters to the device. The details provided will be in handy if the device is not connected to the computer using Active Sync.

3. netcfrtl.dll – This dll facilitates the connection between the computer and the device.

You have to copy both above files to the device Windows folder (\Windows).

If you need more information on how to use .Net CF RPM visit Steven Pratschner’s .Net CF weblog articles.

Recently I did face a problem when I created the setup for my Pocket PC application. My application had several projects and each project is supposed to add a dll to the setup. The problem was when I build the project it didn’t add the reference dlls to the setup but only added the main application.
 
Later I found the solution for this which was to remove the main project output from the deployment project then go to debug mode and add the main project back again to the deployment project. The when the project is rebuilt all the dlls will be included properly.

I have seen many people asking how to play media files in device applications. Here I am playing media files using Windows Media Player.

This is the ProcessHandler class which imports Coredll Dll and uses it’s CreateProcess to create a seperate process with the required application.


This is the main form which has a button to start the Player, then it will ask the user to browse for a media file to play.

The ProcessHandler.CreateProcess function can be used to start any application which is installed in the device by simply replacing the ‘WMPlayer’ by the required application qualified name (IExplorer).