[DllImport("netapi32.dll")]
static extern int NetMessageBufferSend(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string msgname,
[MarshalAs(UnmanagedType.LPWStr)] string fromname,
[MarshalAs(UnmanagedType.LPWStr)] string buf,
int buflen);
Declare Function NetMessageBufferSend Lib "netapi32.dll" (<MarshalAs(UnmanagedType.LPWStr)> servername As String, <MarshalAs(UnmanagedType.LPWStr)> msgname As String, <MarshalAs(UnmanagedType.LPWStr)> fromname As String, <MarshalAs(UnmanagedType.LPWStr)> buf As String, buflen As Integer) As Integer
None.
None.
The Alerter service must be running for NetMessageBufferSend to function.
C# Example code
This is a simple console program which lets you send messages (quite similar to the NET SEND command...).
For example you can use the name of your machine as srcName a'nd dstName and the message will be sent to yourself
using System;
using System.Runtime.InteropServices;
public class SendNetMsg
{
const int NERR_Success = 0;
const int NERR_BASE = 2100;
const int NERR_NameNotFound = (NERR_BASE+173); // The message alias could not be found on the network.
const int NERR_NetworkError = (NERR_BASE+36); // A general network error occurred.
const int ERROR_ACCESS_DENIED = 5;
const int ERROR_INVALID_PARAMETER = 87;
const int ERROR_NOT_SUPPORTED = 50;
[DllImport("netapi32.dll")]
public static extern int NetMessageBufferSend(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string msgname,
[MarshalAs(UnmanagedType.LPWStr)] string fromname,
[MarshalAs(UnmanagedType.LPWStr)] string buf,
int buflen);
public static void Main (String[] argv)
{
if (argv.Length < 3)
{
Console.WriteLine ("sendnetmsg.exe srcName dstName msg");
return;
}
int retVal = NetMessageBufferSend (null, argv[1], argv[0], argv[2], argv[2].Length * 2);
if (retVal == NERR_NameNotFound)
Console.WriteLine ("Error: NERR_NameNotFound") ;
else if (retVal == NERR_NetworkError)
Console.WriteLine ("Error: NERR_NetworkError") ;
else if (retVal == ERROR_ACCESS_DENIED)
Console.WriteLine ("Error: ERROR_ACCESS_DENIED") ;
else if (retVal == ERROR_INVALID_PARAMETER)
Console.WriteLine ("Error: ERROR_INVALID_PARAMETER") ;
else if (retVal == ERROR_NOT_SUPPORTED)
Console.WriteLine ("Error: ERROR_NOT_SUPPORTED") ;
else if (retVal != 0)
Console.WriteLine ("Unknown error returned.\nError code {0}", retVal) ;
}
}
Do you know one? Please contribute it!