unit NetSend;
interfaceuses
Windows;
const
netapi = 'netapi32.dll';
type
LPBYTE = PByte;
NET_API_STATUS = DWord;
const
// success
NERR_Success = 0;
// Base address of error codes
NERR_BASE = 2100;
// A general network error occurred.
NERR_NetworkError = NERR_BASE + 36;
// The user name could not be found.
NERR_NameNotFound = NERR_BASE + 173;
// The user does not have access to the requested information.
// ERROR_ACCESS_DENIED - declared in Windows.pas (= 5)
// This network request is not supported.
// ERROR_NOT_SUPPORTED - declared in Windows.pas (=50)
// The specified parameter is invalid.
// ERROR_INVALID_PARAMETER - declared in Windows.pas (=87)
function NetMessageBufferSend(servername, msgname, fromname: LPCWSTR;
buf: LPBYTE; buflen: DWORD
): NET_API_STATUS; stdcall;
function NetSendMsg(serv, name, text: string): NET_API_STATUS;
implementation
function NetMessageBufferSend; external netapi name 'NetMessageBufferSend';
// serv = name of the remote server on which the function is to execute
// (The string must begin with // or empty ('') for local computer
// (you need some privileges to do this on other machines
// name = the message alias to which the message should be sent
// text = string that contains the message text (multiline is no problem)
function NetSendMsg(serv, name, text: string): NET_API_STATUS;
var
msgserv: PWideChar;
servlen: Cardinal;
msgname: PWideChar;
namelen: Cardinal;
msgtext: PWideChar;
textlen: Cardinal;
begin
Result := ERROR_INVALID_PARAMETER;
servlen := 2 * (Length(serv) + 1);
namelen := 2 * (Length(name) + 1);
textlen := 2 * (Length(text) + 1);
if (servlen > 2) then msgserv := GetMemory(servlen) else msgserv := nil;
msgname := GetMemory(namelen);
msgtext := GetMemory(textlen);
if Assigned(msgname) and Assigned(msgtext) then
try
if Assigned(msgserv) then StringToWideChar(serv, msgserv, servlen div 2);
StringToWideChar(name, msgname, namelen div 2);
StringToWideChar(text, msgtext, textlen div 2);
Result := NetMessageBufferSend(msgserv, msgname, nil, PByte(msgtext), textlen);
finally
if Assigned(msgtext) then FreeMemory(msgtext);
if Assigned(msgname) then FreeMemory(msgname);
if Assigned(msgserv) then FreeMemory(msgserv);
end;
end;
end.