Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (<MarshalAs(UnmanagedType.LPStr)> ByVal lpString As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal nMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Structure MPL_TOAST_INFO
Dim sTitle As Integer
Dim sMessage As Integer
Dim sProgram As Integer
Dim sSoundFile As Integer
Dim nReserved As Integer
End Structure
Public Function DisplayToast(ByVal sMessage As String, ByVal sTitle As String, ByVal sProgram As String, ByVal bPlaySound As Boolean) As Boolean
If (sMessage.Length() > 255) Then
sMessage = sMessage.Substring(0, 255)
End If
Dim info As MPL_TOAST_INFO
Dim titlePtr As System.Runtime.InteropServices.GCHandle
If (sTitle.Length() < 1) Then
info.sTitle = 0
Else
titlePtr = System.Runtime.InteropServices.GCHandle.Alloc(sTitle, Runtime.InteropServices.GCHandleType.Pinned)
info.sTitle = titlePtr.AddrOfPinnedObject.ToInt32
End If
Dim progPtr As System.Runtime.InteropServices.GCHandle
If (sProgram.Length() < 1) Then
info.sProgram = 0
Else
progPtr = System.Runtime.InteropServices.GCHandle.Alloc(sProgram, Runtime.InteropServices.GCHandleType.Pinned)
info.sProgram = progPtr.AddrOfPinnedObject.ToInt32
End If
Dim msgPtr As System.Runtime.InteropServices.GCHandle
If (sMessage.Length() < 1) Then
info.sMessage = 0
Else
msgPtr = System.Runtime.InteropServices.GCHandle.Alloc(sMessage, Runtime.InteropServices.GCHandleType.Pinned)
info.sMessage = msgPtr.AddrOfPinnedObject.ToInt32
End If
If (bPlaySound) Then
info.sSoundFile = -1
Else
info.sSoundFile = 0
End If
'Get the message identifier
Dim nMsg As Integer
nMsg = RegisterWindowMessage("MessengerPlus_DisplayToast")
Dim infoPtr As System.Runtime.InteropServices.GCHandle
infoPtr = System.Runtime.InteropServices.GCHandle.Alloc(info, Runtime.InteropServices.GCHandleType.Pinned)
'Send the message to Messenger Plus! to display the toast
Dim nResult As Integer
nResult = SendMessage(HWND_BROADCAST, nMsg, infoPtr.AddrOfPinnedObject.ToInt32, 1)
'Free the memory
If (info.sTitle <> 0) Then
titlePtr.Free()
End If
If (info.sProgram <> 0) Then
progPtr.Free()
End If
If (info.sMessage <> 0) Then
msgPtr.Free()
End If
infoPtr.Free()
DisplayToast = True 'There is no absolute way to detect if the function was successful
End Function