Tuesday, March 25, 2008

PInvoke /DLLImport for Windows Mobile


Share

Using a Native DLL (c++) in a managed (c#) project is  straightforward. you just write the following in your managed code:

[DllImport("coredll.dll")]
public static extern bool CeRunAppAtEvent(string AppName, int Event);


and use the native function like it was managed.



The problem happens when you want to write your own native dll. It seems that the function in the native code must look like this



extern "C" __declspec(dllexport) void function()

confusing a bit, since when not programming for  Windows Mobile you may also write your native function without extern "C"  and add the EntryPoint attribute, and it will work... (for Windows Mobile this is not the case, you have to make sure you write extern "C") 
Continue Reading...

Wednesday, March 19, 2008

Windows Mobile: How to run a method when device wakes up/ comes back from sleep mode


Share

Its very surprising that there is no wakeup event or member in the managed SystemState class that indicates that the device has returned from sleep mode.

Fortunately, we can use the CeRunAppAtEvent function from coredll.

public static extern bool CeRunAppAtEvent(string AppName,
int WhichEvent);



Starting an application when the device wakes up, is very straight forward,  and all you need is to pass the path of the application to run (and also remember to remove it...)



Running a method is a bit more complicated; instead of the application path you would write 



"\\\\.\\Notifications\\NamedEvents\\eventName"



Where the eventName is a name of a native event. here is the code you will need to start a wakeup event, catch it and run your method



 private const int NOTIFICATION_EVENT_WAKEUP = 11;
private const UInt32 INFINITE = 0xFFFFFFFF;

[DllImport("coredll.dll", SetLastError = true)]
public static extern bool CeRunAppAtEvent(string AppName,
int WhichEvent);

[DllImport("coredll.dll",SetLastError = true)]
private static extern int WaitForSingleObject(IntPtr hHandle,
UInt32 dwMilliseconds);

[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes,
bool bManualReset, bool bInitialState, string spName);

private static General.NoParamsFunctionHandler _eventToRunOnWakeup;

public static General.NoParamsFunctionHandler EventToRunOnWakeup
{
set
{
if (_eventToRunOnWakeup == null)// so we only run one thread
StartOnWakeUpService();
_eventToRunOnWakeup = value;
}
}

private static void StartOnWakeUpService()
{
try
{
CeRunAppAtEvent(
"\\\\.\\Notifications\\NamedEvents\\NativeDeviceWakeUpEvent",
NOTIFICATION_EVENT_WAKEUP);

_wakeupServiceThread = new Thread(new ThreadStart(WakeupProc));
_wakeupServiceThread.Start();
}
catch { }
}


private static void WakeupProc()
{
IntPtr hEvent = CreateEvent(IntPtr.Zero, false, false,
"NativeDeviceWakeUpEvent");
while (!_close)
{
WaitForSingleObject(hEvent, INFINITE);
if (_eventToRunOnWakeup != null)
_eventToRunOnWakeup();
}
}


since this thread will be running in the background, we want to make sure it will stop when we are done. hence the below method



public static void Close()
{
_close = true;
if(_wakeupServiceThread != null)
_wakeupServiceThread.Abort();
}

Continue Reading...

Sunday, March 16, 2008

My MWC (3GSM) 2008 Notes


Share
P1020292

This was my first time at the Mobile World Congress (also known as 3GSM), which was held in Barcelona. I arrived straight to a mobile developer session called Mobile Jam Session. The event was hosted by Rudy de Waele of Dotopen/mTrends and Caroline Lewko of WIP, which did a wonderful job with her great humor, and was located on the 20th floor with a great view of Barcelona. The event was targeted to developers and discussed hot issues and problems. never the less it was very inspiring. I have met some great people in this event, the same people that I have spent most of my time with, and made the World Mobile Congress totally fun!

P1020240 P1020253

After the Jam session we went to the Swedish beer Mobile Networking Event, Organized by the lovely Helen Keegen, probably  now called the Beer fairy, which indeed she was...The party was packed with happy people, mostly men... (is it a mans world out there???) one of the highlights of the party (for me), was meeting James Body from Truphone...

In order  for us to be well prepared for the Swedish hardcore event, we first had to have a proper tapas dinner

P1020278

I woke up early in the morning ready for my next day (yes morning, not afternoon!).my day included walking around, seeing interesting technologies and devices (more about this in my next post) and attended the Women in Mobile Data Association meeting, organized by Helen Keegen (yes, she's done it again) and Gillian Kennedy. This time, Helen was more of a Cava fairy. It was a ladies event, which I'm sure all of you men would have loved to attend. well, there was one brave man that wasn't threatened by smart and beautiful women :) The event included a variety of women in the mobile industry, starting from marketing and development to a business owner of a company for mobile porn content.

It was very surprising, but I, an energetic and a person that can always keep on talking could not talk or walk anymore. I had to have a quiet evening with chicken soup, and relax for my last day of the conference...

P1020296
Continue Reading...

Wednesday, March 12, 2008

Windows Mobile: How to measure execution time in C#


Share

I wanted to test a function's duration (for various  cases).

I thought it to be simple and be as in . Net C#.

I wrote something like this:

DateTime start = DateTime.Now;
//function code......

DateTime end = DateTime.Now;
TimeSpan duration = end - start;


I got that the duration is 0!! I also tried using



duration.TotalMilliseconds; and DateTime.Now.Ticks;


but the ticks for the start and end were the same. this seemed very strange.



To solve the problem, instead of using  DateTime.Now I used



System.Environment.TickCount;


It seems that the time resolution for DateTime.Now depends on the operation system, and for .Net compact framework its  one Second!!!.



 

Continue Reading...
 

Blogroll

Site Info

Text