Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Sending Messages as the Microsoft NT command, net send

Title: Sending Messages as the Microsoft NT command, net send Question: How can I send Messages from an application, as like NT command net send? Answer: Well here it goes. The Program uses a Function not declared in the standard units in Delphi 5 Enterprise (Update). So we have to include our own file with constant's and the function. Here it goes: unit lmmsg; // Thanks to Win2k4u for providing me with this file. (dalNet, #delphi.se) interface //uses lmglobal; const // // Values for msgi1_forward_flag. // MSGNAME_NOT_FORWARDED = 0; // Name not forwarded MSGNAME_FORWARDED_TO = $04; // Name forward to remote station MSGNAME_FORWARDED_FROM = $10; // Name forwarded from remote station type MSG_INFO_0 = record msgi0_name : PWideChar end; PMSG_INFO_0 = ^MSG_INFO_0; MSG_INFO_1 = record msgi1_name : PWideChar; msgi1_forward_flag : Integer; msgi1_forward : PWideChar end; PMSG_INFO_1 = ^MSG_INFO_1; function NetMessageNameAdd ( serverName, msgName : PWideChar) : Integer; stdcall; function NetMessageNameEnum ( serverName : PWideChar; level : Integer; var buffer : Pointer; prefMaxLen : Integer; var entriesRead, totalEntries, resumeHandle : Integer) : Integer; stdcall; function NetMessageNameGetInfo ( servername, msgname : PWideChar; level : Integer; var buffer : Pointer) : Integer; stdcall; function NetMessageNameDel ( serverName, msgName : PWideChar) : Integer; stdcall; function NetMessageBufferSend ( serverName, msgName, fromName : PWideChar; buf : Pointer; bufLen : Integer) : Integer; stdcall; //NetAPIStatus == Integer implementation function NetMessageNameAdd; external 'NETAPI32.DLL'; function NetMessageNameEnum; external 'NETAPI32.DLL'; function NetMessageNameGetInfo; external 'NETAPI32.DLL'; function NetMessageNameDel; external 'NETAPI32.DLL'; function NetMessageBufferSend; external 'NETAPI32.DLL'; end. Copy above code and save it as the unit lmmsg.pas. Then place following components on your Form. * TLabel (Caption, Receiver) * TLabel (Caption, ''); * TComboBox * TButton (Caption, Send) * TMemo Woo! Half way done now.. Create an event for the Button you just created, call it Button1 and paste this code: procedure TForm1.Button1Click(Sender: TObject); var MsgBuff : pWideChar; Size, NewSize, Code : Integer; User, UName : Array[0..127] Of WideChar; begin Size := Length(Memo1.Text); StringToWideChar(ComboBox1.Text, User, SizeOf(User) div 2); StringToWideChar(UserName, UName, SizeOf(UName) div 2); NewSize := Size * 2; MsgBuff := VirtualAlloc(nil, Size, Mem_Commit, Page_ReadWrite); MultiByteToWideChar(CP_ACP, 0, PChar(Memo1.Text), Size, MsgBuff, NewSize); Code := NetMessageBufferSend('', User, UName, MsgBuff, lStrLenW(MsgBuff)* SizeOf(pWideChar)); If Code = 0 Then Label3.Caption := 'Message was sent Successfully!' Else Label3.Caption := 'Returned Error Code: ' + IntToStr(Code); End; I'm passing the logged on user's name rather then the computer, cuz often in networks, the computer names are so cryptical and make no sense. The Reason I set NewSize as Size * 2 is because, this function uses UniCode as character code, and UniCode uses 2 bytes to represent every charachter. The rest is hopefully kinda self explaining. So there's all the code needed to send a Network message to someone on you network. This code can be used in many ways, but this shows the basic idea behind the use of the function, NetMessageBufferSend. oh, one last thing.. I used a TComboBox, so that I either could load all names in it every time I started the app, so I don't have to re-type them again. Or even more fancy way, enumerate all current connected computer to the network, and present them there. I was too lazy for either way :D Good Luck!