1
2 function NetSend(dest, Source, Msg: string): Longint; overload;
3 type
4 TNetMessageBufferSendFunction = function(servername, msgname, fromname: PWideChar;
5 buf: PWideChar; buflen: Cardinal): Longint; stdcall;
6 var
7 NetMessageBufferSend: TNetMessageBufferSendFunction;
8 SourceWideChar: PWideChar;
9 DestWideChar: PWideChar;
10 MessagetextWideChar: PWideChar;
11 Handle: THandle;
12 begin
13 Handle := LoadLibrary('NETAPI32.DLL');
14 if Handle = 0 then
15 begin
16 Result := GetLastError;
17 Exit;
18 end;
19 @NetMessageBufferSend := GetProcAddress(Handle, 'NetMessageBufferSend');
20 if @NetMessageBufferSend = nil then
21 begin
22 Result := GetLastError;
23 Exit;
24 end;
25
26 MessagetextWideChar := nil;
27 SourceWideChar := nil;
28 DestWideChar := nil;
29
30 try
31 GetMem(MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);
32 GetMem(DestWideChar, 20 * SizeOf(WideChar) + 1);
33 StringToWideChar(Msg, MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);
34 StringToWideChar(Dest, DestWideChar, 20 * SizeOf(WideChar) + 1);
35
36 if Source = '' then
37 Result := NetMessageBufferSend(nil, DestWideChar, nil,
38 MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1)
39 else
40 begin
41 GetMem(SourceWideChar, 20 * SizeOf(WideChar) + 1);
42 StringToWideChar(Source, SourceWideChar, 20 * SizeOf(WideChar) + 1);
43 Result := NetMessageBufferSend(nil, DestWideChar, SourceWideChar,
44 MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);
45 FreeMem(SourceWideChar);
46 end;
47 finally
48 FreeMem(MessagetextWideChar);
49 FreeLibrary(Handle);
50 end;
51 end;
52
53 function NetSend(Dest, Msg: string): Longint; overload;
54 begin
55 Result := NetSend(Dest, '', Msg);
56 end;
57
58 function NetSend(Msg: string): Longint; overload;
59 begin
60 Result := NetSend('', '', Msg);
61 end;
62
63 Example:
64
65 procedure TForm1.Button1Click(Sender: TObject);
66 const
67 NERR_BASE = 2100;
68 NERR_NameNotFound = NERR_BASE + 173;
69 NERR_NetworkError = NERR_BASE + 36;
70 NERR_Success = 0;
71 var
72 Res: Longint;
73 sMsg: string;
74 begin
75 Res := NetSend('LoginName', 'Your Message...');
76 case Res of
77 ERROR_ACCESS_DENIED: sMsg :=
78 'user does not have access to the requested information.';
79 ERROR_INVALID_PARAMETER: sMsg := 'The specified parameter is invalid.';
80 ERROR_NOT_SUPPORTED: sMsg := 'This network request is not supported.';
81 NERR_NameNotFound: sMsg := 'The user name could not be found.';
82 NERR_NetworkError: sMsg := 'A general failure occurred in the network
83 hardware.'
84 NERR_Success: sMsg := 'Message sent!';
85 end;
86 ShowMessage(sMsg);
87 end;
|