Sending a Message Like Net Send


Click here to change the theme.

The following code can be used to send a message like Net Send does.

void NetSend(const char *Sender, const char *RecipientHost, const char *Message) {
	HANDLE hFileOut;
	string Filename, Buffer;
	DWORD NumberOfBytesWritten;
// Filename in the format: \\Recipient\MAILSLOT\messngr
Filename = "\\\\;";
Filename += RecipientHost;
Filename += "\\MAILSLOT\\messngr";
// A message record in the format: Sender\0Recipient\0Message\0
Buffer = Sender;
Buffer += '\0';
Buffer += RecipientHost;
Buffer += '\0';
Buffer += Message;
Buffer += '\0';
hFileOut = CreateFile(Filename.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFileOut != INVALID_HANDLE_VALUE)
	WriteFile(hFileOut, Buffer.c_str(), Buffer.length(),
		&NumberOfBytesWritten, NULL);
}

This function can be used as in:

	char ComputerName[64];
unsigned long ComputerNameSize(sizeof ComputerName);
GetComputerName(ComputerName, &ComputerNameSize);
NetSend(ComputerName, ComputerName, "Test message");