whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

22 Visitors Online


 
...implement net send?
Autor: Unknown
[ Print tip ]  

Tip Rating (14):  
     


function NetSend(dest, Source, Msg: string): Longint; overload;
type
  
TNetMessageBufferSendFunction = function(servername, msgname, fromname: PWideChar;
    buf: PWideChar; buflen: Cardinal): Longint; 
  stdcall;
var
  
NetMessageBufferSend: TNetMessageBufferSendFunction;
  SourceWideChar: PWideChar;
  DestWideChar: PWideChar;
  MessagetextWideChar: PWideChar;
  Handle: THandle;
begin
  
Handle := LoadLibrary('NETAPI32.DLL');
  if Handle = 0 then
  begin
    
Result := GetLastError;
    Exit;
  end;
    @NetMessageBufferSend := GetProcAddress(Handle, 'NetMessageBufferSend');
  if @NetMessageBufferSend = nil then
  begin
    
Result := GetLastError;
    Exit;
  end;

  MessagetextWideChar := nil;
  SourceWideChar      := nil;
  DestWideChar        := nil;

  try
    
GetMem(MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);
    GetMem(DestWideChar, 20 * SizeOf(WideChar) + 1);
    StringToWideChar(Msg, MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);
    StringToWideChar(Dest, DestWideChar, 20 * SizeOf(WideChar) + 1);

    if Source = '' then
      
Result := NetMessageBufferSend(nil, DestWideChar, nil,
        MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1)
    else
    begin
      
GetMem(SourceWideChar, 20 * SizeOf(WideChar) + 1);
      StringToWideChar(Source, SourceWideChar, 20 * SizeOf(WideChar) + 1);
      Result := NetMessageBufferSend(nil, DestWideChar, SourceWideChar,
        MessagetextWideChar, Length(Msg) * SizeOf(WideChar) + 1);
      FreeMem(SourceWideChar);
    end;
  finally
    
FreeMem(MessagetextWideChar);
    FreeLibrary(Handle);
  end;
end;

function NetSend(Dest, Msg: string): Longint; overload;
begin
  
Result := NetSend(Dest, '', Msg);
end;

function NetSend(Msg: string): Longint; overload;
begin
  
Result := NetSend('', '', Msg);
end;

// Example:

procedure TForm1.Button1Click(Sender: TObject);
const
  
NERR_BASE = 2100;
  NERR_NameNotFound = NERR_BASE + 173;
  NERR_NetworkError = NERR_BASE + 36;
  NERR_Success = 0;
var
  
Res: Longint;
  sMsg: string;
begin
  
Res := NetSend('LoginName', 'Your Message...');
  case Res of
    
ERROR_ACCESS_DENIED: sMsg := 'user does not have access to the requested information.';
    ERROR_INVALID_PARAMETER: sMsg := 'The specified parameter is invalid.';
    ERROR_NOT_SUPPORTED: sMsg := 'This network request is not supported.';
    NERR_NameNotFound: sMsg := 'The user name could not be found.';
    NERR_NetworkError: sMsg := 'A general failure occurred in the network hardware.';
    NERR_Success: sMsg := 'Message sent!';
  end;
  ShowMessage(sMsg);
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners