Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#

Using CeSetUserNotification to Start a Program

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
30 Jan 2010CPOL 14.2K   2   3
How to use CeSetUserNotification to start a program

A few days ago, I got a message from a developer who was trying to use CeRunAppAtTime to schedule an application to start up. As I had mentioned in a previous post, instead of using this function, one should use CeSetUserNotification since on more recent devices CeRunAppAtTime is not as reliable (not to mention it is marked as deprecated in the MSDN documentation).

The developer asked me for an example of how to call the method from .NET so I put together an example for him. I am sharing the example below and plan to later update my article at CodeProject.com on Automatically Starting a Windows Mobile Program with this same code.

C#
public static extern IntPtr CeSetUserNotification(IntPtr hNotification, string application, 
                                                   SystemTime startTime,
                                                   UserNotification notification);

[DllImport("CoreDLL.dll")]
public static extern int FileTimeToSystemTime(ref long lpFileTime, SystemTime lpSystemTime);
[DllImport("CoreDLL.dll")]
public static extern int FileTimeToLocalFileTime(ref long lpFileTime, ref long lpLocalFileTime);

    [StructLayout(LayoutKind.Sequential)]
    public class UserNotification
    {
        public int ActionFlags;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string DialogTitle;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string DialogText;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string SoundPath;
        public int MaxSoundPath;
        public IntPtr Reserved;

        public UserNotification()
        {
            DialogTitle = String.Empty;
            DialogText = String.Empty;
            SoundPath = String.Empty;
            MaxSoundPath = 0;
            Reserved = IntPtr.Zero;
            ActionFlags = 0;
        }
    }

    public static void RunAppAtTime(string applicationEvent, DateTime startTime)
    {
        long fileTimeUTC = startTime.ToFileTime();
        long fileTimeLocal = 0;
        UserNotification notification;
        notification = new UserNotification();
        notification.ActionFlags = 0;
        notification.Reserved = IntPtr.Zero;

        SystemTime systemStartTime = new SystemTime();
        FileTimeToLocalFileTime(ref fileTimeUTC, ref fileTimeLocal);
        FileTimeToSystemTime(ref fileTimeLocal, systemStartTime);
        CeSetUserNotification(IntPtr.Zero ,applicationEvent, systemStartTime, 
                              notification);
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
QuestionKill the notification been set? Pin
HGolfer5-Feb-14 7:56
HGolfer5-Feb-14 7:56 
GeneralWake Pin
KentCampbell28-Apr-10 10:15
KentCampbell28-Apr-10 10:15 
AnswerRe: Wake Pin
Paul Heil22-Jun-11 3:34
Paul Heil22-Jun-11 3:34 
Use SetDevicePower to turn the radio on when your program starts. See: Radio Power[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.