Often when you have a number of people on a network who are logical "subscribers"
to certain events, changes or other business - related happenings, you want to
have a simple way to "Fire and forget" notifications to these people.
The following simple VB.NET Class Library provides a shell interface
to the "NET SEND" utility, which as we all know results in a popup
on our machine with the message that was sent (provided we have the Messenger
Service operating). Note this is the native Windows Messenger service like for
printer messages and so on, not "Windows Messenger" or "MSN Messenger".
Using the class to send out a notification message is as simple
as this:
Dim n As New PAB.Util.NETSender
n.SendMessageAsync(computername,messagetext)
By the way, there is also an API for this called NetSendMessageBuffer.
Unfortunately, I haven't been able to get it to work, so what you see below,
which works very reliably, is "Plan B":
Imports
Microsoft.VisualBasic
Imports System
Imports System.IO
Imports
System.Runtime.Remoting.Messaging
Namespace PAB.Util
Public
Class NETSender
Public Delegate Function SendMessageDelegate(ByVal Dest
As String, ByVal Message As String) As Integer
Public Function
SendMessage(ByVal strDestination As String, ByVal strMessage As String) As Integer
Dim str2 As String
Try
Dim str1 As String = String.Concat(New
String() {"net send ", strDestination, " ", strMessage, "
> C:\sendResult.log"})
Dim fileStream1 As FileStream = New FileStream("c:\NetSender.bat",
FileMode.Create, FileAccess.Write)
Dim streamWriter As streamWriter =
New streamWriter(fileStream1)
streamWriter.BaseStream.Seek(CLng(0), SeekOrigin.End)
streamWriter.Write(str1)
streamWriter.Flush()
streamWriter.Close()
fileStream1.Close()
Dim process As process = New process
process.StartInfo.FileName
= "C:\NetSender.bat"
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
process.Start()
process.WaitForExit()
process.Close()
Dim
fileStream2 As FileStream = New FileStream("C:\sendResult.log", FileMode.Open,
FileAccess.Read)
Dim streamReader As streamReader = New streamReader(fileStream2)
streamReader.BaseStream.Seek(CLng(0), SeekOrigin.Begin)
str2 =
streamReader.ReadLine()
streamReader.Close()
fileStream2.Close()
If str2.IndexOf("successfully sent") > -1 Then
Return
1
Else
Return 9
End If
Catch e As Exception
Return 9
End Try
End Function
Public Sub GetResultsOnCallback(ByVal
ar As IAsyncResult)
Dim res As Integer
Dim SendMessageDelegate
As SendMessageDelegate = CType(CType(ar, AsyncResult).AsyncDelegate, SendMessageDelegate)
Try
res = SendMessageDelegate.EndInvoke(ar)
Catch e As Exception
End Try
End Sub
Public Function SendMessageAsync(ByVal
Destination As String, ByVal Message As String) As String
Dim SendMessageDelegate
As SendMessageDelegate = AddressOf Me.SendMessage
Dim asyncCallback As
asyncCallback = AddressOf Me.GetResultsOnCallback
SendMessageDelegate.BeginInvoke(Destination,
Message, asyncCallback, Nothing)
Return "ok"
End Function
End Class
End Namespace
N.B. - Oops!
I spoke too soon. Got the API working now, in C#:
using System;
using System.Runtime.InteropServices;
namespace PAB.Util
{
public class NetSend
{
public NetSend()
{
}
public int netSend(string sFrom, string sTo, string sMessage)
{
byte [] bBuffer = System.Text.Encoding.Unicode.GetBytes(sMessage);
int
nRet = NetMessageBufferSend(null, sTo, null, sMessage, sMessage.Length *
2
+ 2);
return nRet;
}
[DllImport ("Netapi32",
CharSet=CharSet.Unicode)]
public static extern int NetMessageBufferSend(
string servername,
string msgname,
string fromname,
string
buf,
int buflen);
}
}
And again,
in VB.NET:
Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Namespace
PAB.Util
Public Class NetSend
Public Function netSend(sFrom
As String, sTo As String, sMessage As String) As Integer
Dim bBuffer As
Byte() = Encoding.Unicode.GetBytes(sMessage)
Return NetMessageBufferSend(Nothing,
sTo, Nothing, sMessage, sMessage.Length * 2 + 2)
End Function
<DllImportAttribute("Netapi32",
CharSet:=CharSet.Unicode)> _
Public Shared Function NetMessageBufferSend(servername
As String, msgname As String, fromname As String, buf As String, buflen As Integer)
As Integer
End Function
End Class
End Namespace
Submission Date: 9/23/2005 3:20:06 PM
Submitted
By: Peter Bromberg
My Home Page: http://www.eggheadcafe.com