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.
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);
}