Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

net message

I'm trying to send a message to multiple IP's using the OS "net send" command.  Does anyone know of an API, available VCL, or a method of doing that in Delphi?

According to the Delphi help I need to use the "TNMMsg component for sending simple ASCII text messages".

Where is that compinent?


Thanks
0
alspivey
Asked:
alspivey
  • 4
  • 4
1 Solution
 
nestoruaCommented:
HI,
First that came to my mind:
Use CreateProcess, ShellExecute, WinExec.
For example, you can send the message to the computer with IP "192.168.1.1" the following way:
ShellExecute(0,
             'open',
             'net',
             ' send 192.168.37.2 HI',
              NIL,
              SW_SHOWNORMAL);
Sure, the more interesting things you be able to do using
TServerSocket/TClientSocket components.
Sincerely,
Nestorua.
0
 
SvisjCommented:
Hi alspivey!
TNMMsg is a component in the Fastnet library. However you can not use it as "Net send"! The function of TNMMsg is similar but requires an application with the TNMGeneralServer component running on all machines that should get messages.

But anyway! This example will send real "net send" messages! 8=)

Explanation:
1.Create a new application
2.add these to the public section of the main form
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    function NetSend(dest, Source, Msg: string): Longint; overload;
    function NetSend(Dest, Msg: string): Longint; overload;
    function NetSend(Msg: string): Longint; overload;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3.After the {$R *.DFM} line, paste in this code:
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
function TForm1.NetSend(dest, Source, Msg: string): Longint;
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 TForm1.NetSend(Dest, Msg: string): Longint;
begin
  Result := NetSend(Dest, '', Msg);
end;

function TForm1.NetSend(Msg: string): Longint;
begin
  Result := NetSend('', '', Msg);
end;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4.Save your project.
5.Drop 2 TEdits, 1 TMemo and 1 Button on the form.
6.On the buttons OnClick Event put this code:
//!!!!!!!!!!!!!!!!
procedure TForm1.Button1Click(Sender: TObject);
var ToName,FromName,Msg : String;
begin
ToName := Edit1.Text;
FromName := Edit2.Text;
Msg := Memo1.Text;
NetSend(ToName,FromName,Msg);
end;
//!!!!!!!!!!!!!!!!
7.Save your project!
8.Run Application

Now you should be able to write the ip or hostname of the
computer you wish to "net send" in edit1. FromName can be
anything you like :=). Write your message and click the button!

To send to multiple recivers you can do something like this:
1.Replace the Button1Click procedure with
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
procedure TForm1.Button1Click(Sender: TObject);
const ANT_RECIVERS = 4;
var FromName,Msg : String;
    ToNames : array[1..ANT_RECIVERS] of String;
    i : Integer;
begin

//You can have more TEdits to provide more
ToNames[1] := Edit1.Text;
ToNames[2] := 'COMPUTERNAME';
ToNames[3] := '192.168.0.2';
ToNames[4] := '192.168.0.3';

FromName := Edit2.Text;
Msg := Memo1.Text;

for i := 1 to ANT_RECIVERS do
Begin
NetSend(ToNames[i],FromName,Msg);
End;

end;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If you want the entire source code I can post the hole
project Good luck! 8=)

0
 
alspiveyAuthor Commented:
Hey great, I was on the wrong track.  Worked great, and I'm bumping your points to the max and giving you a big "A".  You can post the prog if you like at. I would like to see the project.


Many Thanks
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
alspiveyAuthor Commented:
On a valid IP it sends in split seconds. On a invalid IP it takes about 30 seconds.  How can I decrease the timeout to a couple of seconds.

Thanks
0
 
SvisjCommented:
Hi!
To improve the code use this:
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
procedure TForm1.Button1Click(Sender: TObject);
var ReturnMsg: String;
Res := NetSend(ToName,FromName,Msg);
  case Res of
    ERROR_ACCESS_DENIED: ReturnMsg:= 'No Accsess';
    ERROR_INVALID_PARAMETER: ReturnMsg:= 'Specified parameter is invalid!';
    ERROR_NOT_SUPPORTED: ReturnMsg:= 'Request is not supported!';
    NERR_NameNotFound: ReturnMsg:= 'User name not found!';
    NERR_NetworkError: ReturnMsg:= 'Problems with the network hardware!';
    NERR_Success: ReturnMsg:= 'Message sent!';
  end;
showmessage(ReturnMsg);
end;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
instead of just:
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
procedure TForm1.Button1Click(Sender: TObject);
begin
Netsend(ToName,FromName,Msg)
end;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

if this doesn't help, you can for example ping the ip's first to verify their existence. However writing a ping function in delphi can be a pretty difficult task. I have made it but there are free components out there that does this faster.

A free component that can do that can be downloaded from
http://www.hoerstemeier.com/index.htm
0
 
SvisjCommented:
8=)
hi sorry!
You need to add this " res : LongInt;" variable along with:
"var ReturnMsg: String;"!
0
 
alspiveyAuthor Commented:
On a valid IP it sends in split seconds. On a invalid IP it takes about 30 seconds.  How can I decrease the timeout to a couple of seconds.

Thanks
0
 
SvisjCommented:
Hi again!
Thanx for the points!

I am sorry for the typos in the past two comments
here is the working code:
//!!!!!!!!!!!!!!!!!!!!!!
procedure TForm1.Button1Click(Sender: TObject);
var ReturnMsg: String;
         res : LongInt;
         ToName,FromName,Msg : String;
begin
ToName := Edit1.Text;
FromName := Edit2.Text;
Msg := Memo1.Text;
Res := NetSend(ToName,FromName,Msg);
 case Res of
   ERROR_ACCESS_DENIED: ReturnMsg:= 'No Accsess';
   ERROR_INVALID_PARAMETER: ReturnMsg:= 'Specified parameter is invalid!';
   ERROR_NOT_SUPPORTED: ReturnMsg:= 'Request is not supported!';
   NERR_NameNotFound: ReturnMsg:= 'User name not found!';
   NERR_NetworkError: ReturnMsg:= 'Problems with the network hardware!';
   NERR_Success: ReturnMsg:= 'Message sent!';
 end;
showmessage(ReturnMsg);
end;
//!!!!!!!!!!!!!!!!!!!!!!

I don't understand why you whould ask exactly the same question two times, but to answer it better:

You cannot "to my knowledge" interrupt the "net send" once it has been initialized. Use hoerstenmeiers TPing component to ping the ip adresses before you net send them to ensure that they are valid.

I will try to figure out a way to interupt the message if it uses too long time, but I recomend that you use ping to verify the ip adresses/hostnames.

I am currently making a "net send" application for fun, a demo will be available to download from: http://www.frogeye.org/supersend/




0
 
alspiveyAuthor Commented:
Yes, pinging was what I was trying. I think that is going to work best. I had that vcl and I have used it before.

Thinks for all you help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now