Visual C ++ creates a Windows Messenger Service Program

Source: Internet
Author: User
Tags net send

The messenger service is a typeCommunicationService, through which we canQQSame as real-time communication,QQThe difference is that the messenger service does not need to own a number. It only needs to know the IP address of the other party-if it is in the LAN, it only needs to know the computer name of the other party. We can use "net send" to send messages under the command line, you can also use "all tasks-send console messages" in the "operations" menu in "Control Panel-management tools-Computer Management... . The recipient's computer must be Win2000/XP, because Win9x itself does not provide convincing services unless you manually start WinPopup .. After the program is compiled and run, start the console in Windows2000 and enter netsend 192.168.0.10 127.0.0.1 "My messenger program! "After running the preceding command, a dialog box will pop up on the Windows desktop, marked with" my messenger program! "Character and related IP information.
  I. Implementation Method
The Messenger Service provides a simple and quick way to chat on the Internet, so there are some improved messaging programs on the Internet, so that information can be sent according to your own wishes.Microsoft"Net send. If you frequently access the Internet, you may receive advertisements or other inexplicable information from the messenger service, the sender's computer name or IP address displayed in the "courier service" window is the same as your own. If you have encountered this situation, do you want to know how the improved Messenger Service sending program is implemented?
This example will take you to analyze the Windows Messenger Service. We will look at how this messenger service sender program is implemented from a programmer's perspective. In fact, the entire process of implementing the messenger service is very simple. We only need to call an API function:
NET_API_STATUS NetMessageBufferSend(
LPWSTR servername,
LPWSTR msgname,
LPWSTR fromname,
LPBYTE buf,
DWORD buflen
);

The parameters of this function are described as follows:
Servername: computer name. This function will be executed on the specified computer. If it is NULL, it indicates a local computer, which is usually set to NULL.
Msgname: Name or IP address of the target computer.
Fromname: Name or IP address of the source computer.
Buf: pointer to an information buffer. Here you can fill in the information you want to send. Note that this parameter uses Unicode encoding.
Buflen: number of bytes in the preceding information buffer.
Now the function has been introduced. Through this function, we canFoundIf you want to implement the anonymous messenger service, it is too simple-you just need to enter a fake computer name or IP address in fromname. The following is an example. In this program, we accept the name (IP address) of the target computer and the source computer from the command line parameters, and we can also set the number of times the information is sent.
  II,ProgrammingProcedure
1. Start Visual C ++ 6.0, create the project netsend, and select Console mode;
2. Add the new file netsend. c to the project;
3. Add code and compile and run the program.
  3. program code
//////////////////////////////////////// /////////////////////////////////
# Define _ UNICODE
# Define UNICODE
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <wchar. h>
# Include <Windows. H>
# Include <locale. h>
# Include <lm. h>
# Pragma comment (lib, "netapi32.lib ")
Int wmain (int argc, wchar_t * argv [])
{
Int count;
Wchar_t * wdest, * wfrom, * buffer, * wtarget;
DWORD dwReturn;
If (argc <4) | (argc> 5 ))
{
Printf ("Usge: % S <DestIP> <SourIP> <Message> [Count]", argv [0]);
Printf ("Count: Count means number of times to send message, default is 1 .");
Return 0;
}
Wdest = argv [1]; // target computer
Wfrom = argv [2]; // source computer
Buffer = argv [3]; // message sent
Count = _ wtoi (argv [4]); // Number of sent requests. The default value is 1.
If (count = 0)
Count = 1;
Printf ("count = % d", count );
DwReturn = NetMessageBufferSend (NULL, wdest, wfrom,
(LPBYTE) buffer, 2 * lstrlen (buffer); // because the buffer is Unicode encoded, multiply by 2
If (dwReturn = NERR_Success)
{
Printf ("Send OK! ");
While (count --> 1)
{
NetMessageBufferSend (NULL, wtarget, wfrom, (LPBYTE) buffe, 2 * lstrlen (buffer ));
}
Return 0;
}
If (dwReturn = NERR_NameNotFound)
{
Printf ("The user name cocould not be found .");
Return 0;
}
If (dwReturn = NERR_NetworkError)
{
Printf ("A general failure occurred in the network hardware .");
Return 0;
}
If (dwReturn = ERROR_NOT_SUPPORTED)
{
Printf ("This network request is not supported .");
Return 0;
}
If (dwReturn = ERROR_INVALID_PARAMETER)
{
Printf ("The specified parameter is invalid .");
Return 0;
}
If (dwReturn = ERROR_ACCESS_DENIED)
{
Printf ("The user does not have access to the requested information .");
Return 0;
}
Else
Printf ("Unknown error! ");
Return 0;
}

 Iv. Summary
This instanceDevelopmentA Windows Messenger Service Program to help readers gain a deeper understanding of the implementation of Windows Messenger Service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.