NetMessageBufferSend (netapi32)
Last changed: -84.161.130.31

.
Summary
This function sends a buffer of information to a registered message alias.

C# Signature:

[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);

VB Signature:

    ''' <summary>
    ''' The NetMessageBufferSend function sends a buffer of information to a registered message alias.
    ''' </summary>
    ''' <param name="servername">Pointer to a constant string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute.
    ''' If this parameter is NULL, the local computer is used.</param>
    ''' <param name="msgname">Pointer to a constant string that specifies the message alias to which the message buffer should be sent.</param>
    ''' <param name="fromname">Pointer to a constant string specifying who the message is from.
    ''' If this parameter is NULL, the message is sent from the local computer name.</param>
    ''' <param name="buf">Pointer to a buffer that contains the message text.</param>
    ''' <param name="buflen">Specifies a value that contains the length, in bytes, of the message text pointed to by the buf parameter.</param>
    ''' <returns>If the function succeeds, the return value is NERR_Success.
    ''' If the function fails, the return value can be one of the following error codes.
    ''' Return code Description
    ''' ERROR_ACCESS_DENIED The user does not have access to the requested information.
    ''' ERROR_INVALID_PARAMETER The specified parameter is invalid.
    ''' ERROR_NOT_SUPPORTED This network request is not supported.
    ''' NERR_NameNotFound The user name could not be found.
    ''' NERR_NetworkError A general failure occurred in the network hardware.
    ''' </returns>
    ''' <remarks>If you call this function on a domain controller that is running Active Directory, access is allowed or denied based on
    ''' the access control list (ACL) for the securable object. The default ACL permits only Domain Admins and Account Operators to call this function.
    ''' On a member server or workstation, only Administrators and Server Operators can call this function.
    ''' For more information, see Security Requirements for the Network Management Functions. For more information on ACLs and ACEs, see Access Control Model.
    '''
    ''' Windows NT:  No special group membership is required to execute the NetMessageBufferSend function on a LAN Manager or a Windows NT system.
    ''' Admin, Accounts, Print, or Server Operator group membership is required to successfully execute NetMessageBufferSend on a remote server.
    ''' </remarks>
    Public Declare Function NetMessageBufferSend Lib "netapi32.dll" _
    (<MarshalAs(UnmanagedType.LPWStr)> ByVal servername As String, _
    <MarshalAs(UnmanagedType.LPWStr)> ByVal msgname As String, _
    <MarshalAs(UnmanagedType.LPWStr)> ByVal fromname As String, _
    <MarshalAs(UnmanagedType.LPWStr)> ByVal buf As String, _
    ByVal buflen As Integer) _
    As Integer

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

The Alerter service must be running for NetMessageBufferSend to function.

Sample Code:

C# Example code

This is a simple console program which lets you send messages (quite similar to the NET SEND command...).

Usage
sendnetmsg.exe srcName dstName message

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

Alternative Managed API:

Do you know one? Please contribute it!

Documentation