Windows Mobile Unmanaged Code (Coredll.dll)

Tower

Gawd
Joined
Oct 11, 2001
Messages
840
Hey all,

I'm trying to register an application to run during a Windows Mobile event using CeRunAppAtEvent (from CoreDll.dll). I'm having difficulty getting this to happen.

Here is the syntax for the native method:

Code:
BOOL CeRunAppAtEvent(
  TCHAR* pwszAppName, 
  LONG lWhichEvent 
);

Here is the code in my application (C#):

Code:
[DllImport("coredll.dll", EntryPoint = "CeRunAppAtEvent", SetLastError = true)]
        private static extern bool CeRunAppAtEvent(string pwszAppName, int lWhichEvent);

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (CeRunAppAtEvent(@"\Windows\Calc.exe", 3))
            {
                MessageBox.Show("Event registered!");
            }
            else
            {
                MessageBox.Show("Event did not register.");
            }
        }

Note that I'm using the argument '3' for lWhichEvent, which is based upon this understanding of how lWhichEvent argument works:

Code:
>  NOTIFICATION_EVENT_NONE                =  0,
>  NOTIFICATION_EVENT_TIME_CHANGE         =  1,
>  NOTIFICATION_EVENT_SYNC_END            =  2,
>  NOTIFICATION_EVENT_ON_AC_POWER         =  3,
>  NOTIFICATION_EVENT_OFF_AC_POWER        =  4,
>  NOTIFICATION_EVENT_NET_CONNECT         =  5,
>  NOTIFICATION_EVENT_NET_DISCONNECT      =  6,
>  NOTIFICATION_EVENT_DEVICE_CHANGE       =  7,
>  NOTIFICATION_EVENT_IR_DISCOVERED       =  8,
>  NOTIFICATION_EVENT_RS232_DETECTED      =  9,
>  NOTIFICATION_EVENT_RESTORE_END         = 10,
>  NOTIFICATION_EVENT_WAKEUP              = 11,
>  NOTIFICATION_EVENT_TZ_CHANGE           = 12,
>  NOTIFICATION_EVENT_MACHINE_NAME_CHANGE = 13

I've tried registering Calc and a few other applications unsuccessfully--the program tells me the event registers (so the method call is returning true), yet the applications do not launch upon the event change (plugging into AC power, for example). Any ideas to what I'm doing wrong?
 
Install the Windows 6 SDK from here if you haven't already: http://www.microsoft.com/downloads/...3A-A651-4745-88EF-3D48091A390B&displaylang=en

Take a look at the NotificationBrokerViewer solution here if you install the SDK in the default location:

C:\Program Files (x86)\Windows Mobile 6 SDK\Samples\PocketPC\CS\StateAndNotificationBrokerViewer

This code hooks into all monitored events and hooks up event handlers. The one you'll be intrested in the CradlePresent as there is no NOTIFICATION_EVENT_ON_AC_POWER event.

The main thing here is the ChangeOccurred event. Here's the equivalent functionality that you're trying to achieve. It's the only thing that I changed in the sample code provided in the SDK

Code:
        public void ChangeOccurred(object sender, ChangeEventArgs args)
        {
            SystemState state = (SystemState)sender;
            txtChanges.Text += state.Property.ToString() + " = " + ConvertToString(args.NewValue) + "\r\n";
            if (state.Property == SystemProperty.CradlePresent)
            {
                MessageBox.Show("Yeay!");
            }
        }

This should get you going. Let me know if you have any questions about setting up the SDK and checking out the sample code.
 
Thanks Heatlesssun--figures that they provided a managed way to handle this state in the newer version of the OS.

My own lack of programming knowledge is why I didn't implement this--I'm still grasping Event Handlers, but then again I'm (mostly) teaching myself.

I did manage to get the older method of STATE_NOTIFICATIONS to register, but only after certain events (ActiveSync ends, etc.) It was nice because it was registered with the OS. I do believe with this ChangeEvent process, I'll have to have my program running in the background all the time? Once I finish it, I'll find out.

The other problem I have is that I'd like to programmatically turn Bluetooth on or off depending on the state change. The idea is to build a customizable app. to set certain OS settings depending on whether or not the device is powered (like turning on BT, rotating the screen, etc.)

The end goal is to make a fully-automated Car-Kit application that handles all the changes I make when I get in/out of my vehicle. :)

Thanks for the help, btw!

EDIT: As I was concerned, it does only monitor changes actively while the application's running. Not terrible, I suppose. I'll just have to change my philosophy on how this application will work. Being as it is, it's such a small application that the memory footprint and resource usage should be almost nil. Now the problem left is how to programmatically enable Bluetooth. This seems to be unsupported by the framework; although 32feet.net has written a managed API to handle it, it also requires the inclusion of their .dll. Is this really the only way to enable/disable a Bluetooth radio in WM? This seems like a rather glaring oversight..
 
Last edited:
Back
Top