Click here to Skip to main content
Click here to Skip to main content

Fake (or Anonymous) NetSend for Windows NT/2000/XP

By , 17 Jun 2003
 

Introduction

A few years ago I was working at a software development company and one day my co-worker asked me if would be possible to use the Windows Net Send mechanism to send messages through the corporate network using some programming language like C++. I found the idea very interesting since we could implement this functionality in some of our applications so they could send messages to our computers when an exception occurs and/or something goes wrong.

After some time of MSDN and Google, learning about MailSlots, I could write the first test program and, for our surprise, worked like a charm.

With our program ready, we've started do create a generic function to send the messages, so we could make it avaliable at our "shared" directory, when I got an idea: "What if we use an invalid User Name to send the message?". Well, I think I don't need to tell you how this history ends :)

Background Information

The FakeSend (and Net Send) mechanism is very simple. It uses an Interprocess Communication (IPC) resource called "MailSlots". We can create MailSlots to swap messages between applications on a network environment just like we do with Sockets, Named Pipes, etc... The MailSlot internally uses a datagram socket, which means that we cannot be sure if the message has arrived. To learn more about MailSlots read the MailSlot Reference from MSDN Library.

Using the code

The code is very straightforward. We have the main function NetSend() that receives three values:

  • szSender - The name of the person who is sending the message (Any name you want);
  • szReceiver - The User Name (Network Login) or Machine name of the person who will receive the message;
  • szMessage - The Message you want to send.
bool NetSend(const char * szSender, const char * szReceiver,
             const char * szMessage)
{
    // Our main variables
    char * pszMailSlot = NULL;
    unsigned char * pucMessage = NULL;
    unsigned int nMailSlotLen, nMsgFormatLen;

    HANDLE hHandle;
    DWORD dwBytesWritten;
    bool bRet = false;

    // Get the length of the strings
    nMailSlotLen  = strlen(szReceiver) + sizeof(szMailSlotPath);
    nMsgFormatLen = strlen(szSender) + strlen(szReceiver) +
                    strlen(szMessage) + sizeof(szMsgFormat);

    // Allocate necessary memory
    pszMailSlot = new char[nMailSlotLen];
    pucMessage  = new unsigned char[nMsgFormatLen];

    // Network path for <Receiver> MailSlot:
    // "\\Receiver\MAILSLOT\messngr"
    sprintf(pszMailSlot, szMailSlotPath, szReceiver);

    // Message Format:
    // "Sender\0Receiver\0Message\0"
    // sprintf doesn't work with \0 so here I'm using \r
    sprintf((char *)pucMessage, szMsgFormat, szSender,
                                szReceiver, szMessage);

    // Replace all '\r' characters with '\0'
    for (unsigned short i = 0; i < nMsgFormatLen; i++)
    {
        if (pucMessage[i] == '\r')
            pucMessage[i] = '\0';

        else if (pucMessage[i] == '\0')
            break;
    }

    // Create the file into Receiver's MailSlot and get a Handle to this file
    hHandle = CreateFile(pszMailSlot, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    // Do we have a valid handle?
    if (hHandle)
    {
        // Write the message to file
        bRet = (WriteFile(hHandle, pucMessage, (DWORD)nMsgFormatLen,
                    &dwBytesWritten, NULL) == TRUE);

        // Free the handle
        CloseHandle(hHandle);
    }

    return bRet;
}

History

  • 2003-06-08 - First version of FakeSend. Bugs: Zero, I hope :)
  • 2003-06-11 - Version 1.01. Just improving the code.

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

About the Author

Caio Proiete
Software Developer (Senior)
Portugal Portugal
Member
Caio Proiete has been programming for over 10 years, having first started with Clipper Summer'87.
 
He loves software development and reverse engineering. Today his main development technologies includes ASP .NET MVC, jQuery, HTML5, Silverlight, and WCF. He also has the following titles:
 
MVP - Microsoft Most Valuable Professional;
MCT - Microsoft Certified Trainer;
MCPD - Microsoft Certified Professional Developer;
MCTS - Microsoft Certified Technology Specialist;
MCSD - Microsoft Certified Solution Developer;
MCDBA - Microsoft Certified Database Administrator.
 
Technical blog:
http://caioproiete.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiondelphi?memberp2bf28 Feb '07 - 6:01 
plizzzz delphi code?
 
oooooooooo

GeneralNo messages receivedmemberPolovski11 Feb '07 - 2:27 
Hi, i've downloaded your program, but it isn't working properly. Even when I send a message to my own pc, the message sent text shows up, but no sign of a popup box. I and other computers in my network don't receive messages. I used ip's as wel as computernames, but i didn't work. Do i have to install some software for this?
QuestionForce line feed in messagememberzhur18 Oct '06 - 10:48 
There is a way to force a line feed in message when using the fakesend?
Like the following lines:
Hello
World!
 
Thank you!
 

Generalfake netSend from c# formmemberStojaneee17 May '06 - 13:41 
How I can send message from my c# form to lan (workgroup)?
NetMessageBufferSend() don't work!
I have Win XP prof.
 
If anyone now, please help me!!!
 

Thanks...
 
stojaneee{at}yahoo{dot}com
 
c# only
Generalsend to allmemberyaewo10 May '06 - 10:10 
dont no if anyones gonna read this message but il post it anyway
 
iv used the fakesend program (works a charm Big Grin | :-D ) but is there a way to send to all users like there is in the netsend function (net send * hi)
 
i dont think there is at the moment but is there a way i can code it into the source so it will do it?
 
cheers
GeneralProblem, program ends very fastmemberIustus-aTw31 Mar '06 - 10:47 
Ive tried the source code and even the compiled version, but both of them ends within a quarter of a second, or less... so i cant try the problem, do you know anything about this issue?
 
Greetz
GeneralRe: Problem, program ends very fastmemberIustus-aTw3 Apr '06 - 7:28 
I am using Windows XP Professional Version 2002 Service Pack 1....
GeneralRe: Problem, program ends very fastmemberStojaneee17 May '06 - 13:33 
Use cmd (command prompt)like this: c:\>FakeSend.exe
and use net send.
 
bye
 
c# only
QuestionI didn't get a popup box, Why ???memberKENyroj20 Mar '06 - 16:47 
hello,
I found your Fakesend progeam at the code projet site.
however, after I download it, I found it cannot work as I expect
 
E:\>FakeSend.exe ken 192.168.0.154 "......."
Fake NetSend for Windows NT/2000/XP v1.01
by Caio Proiete (caio@proiete.com.br)
 
Sending Fake Message:
Sender...: ken
Receiver.: 192.168.0.154
Message..: .......
Message sent!
E:\>
 
It seems to be OK, but I didn't get a popup box.
I'm sure that my messanger service is ON because I use "net send" OK
My Operation System is Windows server Enterprise
My friends use Windows XP SP2 also didn't got popupbox
 
thanks for your reading
 
Best Regards
GeneralVB6 Codemembermattwright21 Dec '05 - 9:59 
Could you pleas egive me a link to download the vb6 cource

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 18 Jun 2003
Article Copyright 2003 by Caio Proiete
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid