?
Solved

net message

Posted on 2003-02-22
9
Medium Priority
?
356 Views
Last Modified: 2012-05-04
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
Comment
Question by:alspivey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 7999440
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
 
LVL 1

Accepted Solution

by:
Svisj earned 2000 total points
ID: 8000277
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
 

Author Comment

by:alspivey
ID: 8001807
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.

 

Author Comment

by:alspivey
ID: 8003562
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
 
LVL 1

Expert Comment

by:Svisj
ID: 8003818
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
 
LVL 1

Expert Comment

by:Svisj
ID: 8003829
8=)
hi sorry!
You need to add this " res : LongInt;" variable along with:
"var ReturnMsg: String;"!
0
 

Author Comment

by:alspivey
ID: 8003903
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
 
LVL 1

Expert Comment

by:Svisj
ID: 8004025
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
 

Author Comment

by:alspivey
ID: 8004047
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question