Click here to Skip to main content
15,880,503 members
Articles / Programming Languages / C#
Article

Batch Net Send

Rate me:
Please Sign up or sign in to vote.
3.96/5 (11 votes)
23 Mar 20042 min read 109K   2.1K   13   16
Send network messages to several recipients

Introduction

This article shows how to send multiple network messages using Platform Invoke and the Windows Network functions.

Background

A few weeks ago I felt the need to send several network messages. After some time sending them through the Windows command prompt I thought that there should be a easier way. So I stumbled on the internet with some multiple network send GUIs which did the trick for the moment. But this made me curious, what was the code behind those GUIs like? So I made a search here in CodeProject and found a similar article from a fellow contributor which provided a front-end for net send. But after looking at the source code, I realized that the only thing it did was to generate a .bat file with the net send commands and messages, and execute it on the shell. I tryed to replicate the idea but without using the .bat file, so I discovered a windows function that did exactly what I wanted. The NetMessageBufferSend function.

Using the code

In order to invoke the windows function, Platform Invoke was used. This enabled me to use C# as the development language instead of having to use MFC or Win32 API. This code basically invokes two main functions. NetMessageBufferSend to send the message and NetServerEnum to extract the list of available computers in the network.

C#
//
//  Invoking  the  NetServerEnum  function
//
int  ret  =  NetServerEnum(null,101,out  buf,-1,
    ref  entriesread,ref  totalentries,
    SV_101_TYPES.SV_TYPE_WORKSTATION,null,0);

SERVER_INFO_101  server  =  (SERVER_INFO_101)
  Marshal.PtrToStructure(new  IntPtr(ptr),typeof(SERVER_INFO_101));

The NetServerEnum function returns different types of structures according to the second parameter passed. In this case we want the information contained in a SERVER_INFO_101 structure, so parameter '101' is passed. entriesread and totalentries variables store the number of entries returned by this function call and the total number of available servers of that type in the network respectively. SV_TYPE_WORKSTATION tells the function the type of servers we want to list. After choosing which workstation we want the message to be sent to, its time to call the NetMessageBufferSend function.

C#
//
//  Invoking  the  NetMessageBufferSend  function
//
int  nRet  =  NetMessageBufferSend(null,  
  treeView1.Nodes[i].Text,  from,  textBox1.Text,  
  textBox1.Text.Length  *  2  +  2);

History

  • 22/03/2004 Initial Version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAwesome! Pin
BOSSNOSS2-Aug-07 7:28
BOSSNOSS2-Aug-07 7:28 
Generalwhere's the exe Pin
fordey427-Jun-07 0:11
fordey427-Jun-07 0:11 
so where is the exe
QuestionAcknowledge the message Pin
niketha25-Jan-07 12:42
niketha25-Jan-07 12:42 
Generalexe Pin
zfd4n13l26-Jul-05 7:44
zfd4n13l26-Jul-05 7:44 
GeneralNot working in windows XP Pin
chito29-Sep-04 22:09
chito29-Sep-04 22:09 
GeneralRe: Not working in windows XP Pin
Anonymous26-Oct-04 11:39
Anonymous26-Oct-04 11:39 
GeneralRe: Not working in windows XP Pin
dbdannydb9-Nov-04 12:13
dbdannydb9-Nov-04 12:13 
GeneralRe: Not working in windows XP Pin
Abxi17-Aug-05 14:30
sussAbxi17-Aug-05 14:30 
GeneralRe: Not working in windows XP Pin
VF2-Jun-06 22:15
VF2-Jun-06 22:15 
GeneralGreat Job! Pin
Glenn E. Lanier II6-Aug-04 3:57
Glenn E. Lanier II6-Aug-04 3:57 
QuestionNot working? Pin
TCavins3-Aug-04 7:17
TCavins3-Aug-04 7:17 
Questionhow to receive net send message? Pin
vutrunghieu20-Apr-04 15:29
vutrunghieu20-Apr-04 15:29 
AnswerRe: how to receive net send message? Pin
azazel009-May-04 8:30
azazel009-May-04 8:30 
Generalmessage spam Pin
No Brayn R4-Apr-04 0:56
No Brayn R4-Apr-04 0:56 
QuestionThanks. Can the user login be included? Pin
bparent24-Mar-04 5:07
bparent24-Mar-04 5:07 
AnswerRe: Thanks. Can the user login be included? Pin
GShravanKumar7-Apr-04 18:56
GShravanKumar7-Apr-04 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.