Nadav and the wonders of the web


Archive for the ‘WM’ Category

How to: Get unique device id in C# (Windows Mobile)

Friday, March 28th, 2008

While looking for a full article about this I found out it won’t hurt to have another quick explanation about this process.

Sometimes when creating an application you want some sort of verification about which phone is accessing your service (service-client application). A good way to do this is to use the unique device id.

Now I will explain how we do it, we are going to get a 40 char string that is hashed with an AppString we will provide which will be unique to each and every device, and will be constant for this device and AppString.

First we define the P/Invoke signature:

[DllImport("coredll.dll")]
private static extern int GetDeviceUniqueID([In, Out] byte[] appdata, int cbApplictionData,
                                            int dwDeviceIDVersion,
                                            [In, Out] byte[] deviceIDOuput, ref uint pcbDeviceIDOutput);

Now we are going to define a private static member so we don’t go through the entire process every time we need this and then create it with a public static method:

private static string s_UniqueDeviceID;

/// <summary>
/// Gets the unique device id, this is an unique id of the phone
/// which is hashed with the AppString
/// </summary>
/// <returns>a 40 characters unique id</returns>
public static string GetUniqueDeviceID()
{
    if (string.IsNullOrEmpty(s_UniqueDeviceID))
    {
        // define AppString
        string AppString = "MyAppString";

        // get the byte array for the string
        byte[] AppData = Encoding.ASCII.GetBytes(AppString);

        // some parameters for the native call
        int appDataSize = AppData.Length;
        uint SizeOut = 20;

        // our out parameter
        byte[] DeviceOutput = new byte[20];

        // Call the native GetDeviceUniqueID method from coredll.dll
        GetDeviceUniqueID(AppData, appDataSize, 1, DeviceOutput, ref SizeOut);

        // check that we got something
        if (SizeOut > 0)
        {
            // build text from received bytes
            StringBuilder resultText = new StringBuilder();
            for (int i = 0; i < DeviceOutput.Length; i++)
            {
                resultText.Append(string.Format("{0:x2}", DeviceOutput[i]));
            }

            // set up result
            s_UniqueDeviceID = resultText.ToString();
        }
    }

    return s_UniqueDeviceID;
}

As you can see the AppString can really be whatever we want it to be. the rest is a little work in order to transform it all from bytes to string but that is why we do it only once!

The only important thing I can see about the AppString is that it won’t be null\empty and that it will be constant throughout your application and usage.

Hope this was clear.

Cheers,

Nadav

How to: Get system up time in C#

Monday, March 17th, 2008

I guess this is quite the easy one but the fact is that while I was searching the web for this answer it was very hard to find.

This is something I knew and forgot so it is good that I will have it here for future quick reference.

Just use:

Environment.TickCount

This will return the time in milliseconds that has passed since the last boot.

Quick Note:

I have read here that this might overflow on systems running for a few days, they also give a solution. as I am using this for a Windows Mobile device I don’t have that option. (CF.Net being deprecated and all…)

Hope you found this faster then I did.

Update:

After further looking into this I have found a nice article about how to overcome the overflow problem, here is the solution:

public static TimeSpan GetTimeSinceBoot()
{
    long _newTicks = Convert.ToInt64("0x" + Environment.TickCount.ToString("X"), 16);

    int days = (int) (_newTicks/(1000*60*60*24));
    int hours = (int) ((_newTicks/(1000*60*60)) - (days*24));
    int minutes = (int) ((_newTicks/(1000*60)) - (days*24*60) - (hours*60));
    int seconds = (int) ((_newTicks/1000) - (days*24*60*60) - (hours*60*60) - (minutes*60));
    int milliseconds =
        (int) (_newTicks - (days*24*60*60*1000) - (hours*60*60*1000) - (minutes*60*1000) - (seconds*1000));

    return new TimeSpan(days, hours, minutes, seconds, milliseconds);
}

The ticks represent milliseconds and when I just call the constructor for TimeSpan it doesn’t treats it this way, that is why i am doing all of this.

Good luck,

Nadav

How to: Control the keyboard(SIP - Soft Input Panel) on Windows Mobile

Friday, February 1st, 2008

As you might have noticed, what I do here in this blog is write about the things I look for, than, when I can’t find them or I find too complicated and evasive answers I write simple ones here.

So, let’s get to the reason we’re all here.

I will give here an example on how you can change the visibility of the keyboard and you will probably be able to figure out the rest.

the steps are as follows:

  1. Add a reference to Microsoft.WindowsCE.Forms.dll
  2. Create our new class:
    using Microsoft.WindowsCE.Forms;
    
    namespace Environment.Keyboard
    {
        public class Keyboard
        {
  3. Create our private InputPanel member:

    private static InputPanel m_inputPanel = new InputPanel();
  4. Create the property to check if the Keyboard is open:

    public static bool KeyboardOpen
    {
        get { return m_inputPanel.Enabled; }
        set { m_inputPanel.Enabled = value; }
    }
  5. Were done!

It’s that simple. the Enabled property in the InputPanel signifies if the keyboard is currently open or not.

Check out this link for a more comprehensive article.

See you next time,

Nadav

How to: Run a program on start up on Windows mobile

Wednesday, January 30th, 2008

Thinking about how to run a program on the Smart Phone’s boot sequence got me searching and I did find the answer but it was not as easy and as clear to find as I hoped. That is the reason that i came to write this post.

The very simple and easy way to do this is to add a shortcut to the program we want to run under \Windows\StartUp

Yes its that simple.

There might be a different way to do this using the registry, but i couldn’t find one and quite frankly, I don’t see the need for it.

What you might want to do is add your created shortcut to the your installation CAB[I will try and have a different post regarding this one, it is a big subject.] under this folder so it will be done on installment and you won’t have to do it programmatically in a later phase. On the other hand, doing it programmatically leaves you the option to have this as a setting in your application that the user can set. I will leave the choice down to you. =]

Hope this was helpful!

Nadav

How To: Bring your program to the foreground on WM6

Sunday, January 13th, 2008

This topic is one I have been spending some time studying.
How do you get your program to the foreground when a certain event accurse ?

The answer is quite simple actually, you just need to call a UI component. like:

MessageBox.Show("Show Me");

And it will show the top most form in your application. Another thing you can do is call a form with Show().

Now, for a little bonus I have for you the…

How to send it back to the background!

What we need to do here is to call Hide() on all the visible forms in our application, this will essentially cause all our UI to go to the background and will bring back the last active application.

A little hint, you might want to use what you learned, and combine it with the previous post ;].

Nadav

How to: Using the State and Notification broker on WM6

Sunday, January 13th, 2008

Sometimes we want to be notified of different events that happen in the operating system, even when we are not “looking”. the State and Notification broker is built especially for this.

I will give an example of how to be notified when the current active application changes and how to handle it.

First create the correct SystemState member:

SystemState _activeProgram = new SystemState(SystemProperty.ActiveApplication);

Now set the event handler on your program initiation:

_activeProgram.Changed += new ChangeEventHandler(_activeProgram_Changed);

Now handle it ;]:

void _activeProgram_Changed(object sender, ChangeEventArgs args)
{
    MessageBox.Show(((string)args.NewValue));
}

You would want to know a few interesting things:

  1. The NewValue string is formatted in this way:

    PreviousActiveProgramNameCurrentActiveProgramName

    So, you might want to use StartsWith\EndsWith methods in order to check them ;].

  2. It seams that every UI involved action(MessageBox, Form, etc.) causes your own application to come back from the background(if it was there) and show itself ;]. while other non-UI related actions does not cause this. This only a major “feeling” i got from my little workings with this so far, i checked this with a Thread.Sleep that did not cause my application to come to the foreground.

Hope this helps.

Nadav

Jajah is the VoIP player that brought you web-activated telephony.