Has anyone successfully worked with PPC Notifications using VB.NET or C#,
and the Compact Framework? I'm trying to use the API to do so, but think I
have problems with the way my SHNOTIFICATIONDATA structure is defined.
Any help would be EXTREMELY appreciated!
Thanks,
Steve
Here's some work that should give you a helping hand:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Pocket_Wx
{
public class Notification
{
#region API Declarations
private const uint SHNP_INFORM = 0x1B1;
private const uint SHNP_ICONIC = 0x0;
[StructLayout(LayoutKind.Sequential)]
public unsafe struct SHNOTIFICATIONDATA
{
public uint cbStruct;
public uint dwID;
public uint npPriority;
public uint csDuration;
public IntPtr hicon;
public uint grfFlags;
public Guid clsid;
public IntPtr hwndSink;
public void* pszHTML;
public void* pszTitle;
public uint lParam;
}
[DllImport("aygshell.dll", CallingConvention=CallingConvention.Cdecl,
EntryPoint="#155")]
private static extern int SHNotificationAdd(ref SHNOTIFICATIONDATA
shinfo);
//Registry APIs
[DllImport("rapi.dll")]
private static extern Int32 CeRegOpenKeyEx(
int hKey,
string lpszSubKey,
int ulOptions,
int samDesired,
ref int phkResult);
[DllImport("rapi.dll")]
public static extern int CeRegCloseKey(int hKey);
#endregion
#region Constructor & Deconstructor
public Notification()
{
}
#endregion
#region Methods
public unsafe void Add(string Message)
{
SHNOTIFICATIONDATA shinfo = new SHNOTIFICATIONDATA();
/*
* Define teh WAV file to play when an alert is displayed
*
[HKEY_CURRENT_USER\ControlPanel\Notifications\{85EE47B2-57D7-EEFE-8E7A-36480
443D062}]
@="Notify Test"
"Wave"="Alarm3"
"Duration"=dword:00000000
"Options"=dword:00000008
*/
//Null-terminate the strings
string Title = "\0";
Message += "\0";
shinfo.cbStruct = (uint)Marshal.SizeOf(shinfo);
shinfo.dwID = 1;
shinfo.npPriority = SHNP_INFORM;
shinfo.csDuration = 10;
fixed (char* pTitle = Title.ToCharArray())
{
fixed (char* pHTML = Message.ToCharArray())
{
shinfo.pszTitle = pTitle;
shinfo.pszHTML = pHTML;
try
{
int err = SHNotificationAdd(ref shinfo);
if (err != 0)
{
throw (new Exception("Invalid return from SHNotificationAdd"));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
public unsafe void Add(string Message, string Title)
{
SHNOTIFICATIONDATA shinfo = new SHNOTIFICATIONDATA();
//Null-terminate the strings
Title += "\0";
Message += "\0";
shinfo.cbStruct = (uint)Marshal.SizeOf(shinfo);
shinfo.dwID = 1;
shinfo.npPriority = SHNP_INFORM;
shinfo.csDuration = 10;
fixed (char* pTitle = Title.ToCharArray())
{
fixed (char* pHTML = Message.ToCharArray())
{
shinfo.pszTitle = pTitle;
shinfo.pszHTML = pHTML;
try
{
int err = SHNotificationAdd(ref shinfo);
if (err != 0)
{
throw (new Exception("Invalid return from SHNotificationAdd"));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
public unsafe void Add(string Message, string Title, uint Duration)
{
SHNOTIFICATIONDATA shinfo = new SHNOTIFICATIONDATA();
//Null-terminate the strings
Title += "\0";
Message += "\0";
shinfo.cbStruct = (uint)Marshal.SizeOf(shinfo);
shinfo.dwID = 1;
shinfo.npPriority = SHNP_INFORM;
shinfo.csDuration = Duration;
fixed (char* pTitle = Title.ToCharArray())
{
fixed (char* pHTML = Message.ToCharArray())
{
shinfo.pszTitle = pTitle;
shinfo.pszHTML = pHTML;
try
{
int err = SHNotificationAdd(ref shinfo);
if (err != 0)
{
throw (new Exception("Invalid return from SHNotificationAdd"));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
public unsafe void Add(string Message, string Title, IntPtr hIcon)
{
SHNOTIFICATIONDATA shinfo = new SHNOTIFICATIONDATA();
//Null-terminate the strings
Title += "\0";
Message += "\0";
shinfo.cbStruct = (uint)Marshal.SizeOf(shinfo);
shinfo.dwID = 1;
shinfo.npPriority = SHNP_INFORM;
shinfo.csDuration = 10;
shinfo.hicon = hIcon;
fixed (char* pTitle = Title.ToCharArray())
{
fixed (char* pHTML = Message.ToCharArray())
{
shinfo.pszTitle = pTitle;
shinfo.pszHTML = pHTML;
try
{
int err = SHNotificationAdd(ref shinfo);
if (err != 0)
{
throw (new Exception("Invalid return from SHNotificationAdd"));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
#endregion
}
}
Seth Demsey
Program Manager
Net Compact Framework
This posting is provided "AS IS" with no warranties, and confers no rights.