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.
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.
int nRet = NetMessageBufferSend(null,
treeView1.Nodes[i].Text, from, textBox1.Text,
textBox1.Text.Length * 2 + 2);
History
- 22/03/2004 Initial Version