It took more than two days to develop a "courier service" program. Haha, I have seen it online before, and I have to pay for registration. Actually, the netmessagebuffersend function is used, and a netmessagenameadd function is used to add any name, so that you can hide your identity. In the past, due to the use of fixed IP addresses, many advertisements were often sent in this way. Sometimes the teacher is giving a lecture in class, and suddenly such a window is coming up, so we usually block the messenger service. Hey hey, now I am actually doing this.
You can use the net send command through the command line to send this message, but it will expose your computer name because it does not select the parameter of the name, netmessagebuffersend allows you to use any "name" on your computer, which is also called a "message alias" and seems to be different from the computer name, however, the default "message alias" is the computer name. At the beginning, I thought I could assign a value to the from parameter of netmessagebuffersend. Many people on the Internet also said this, but I always encountered an error when I tried it. It was later discovered that the name must be one of the existing "message aliases" on the computer. How can I use a name at will? Therefore, the netmessagenameadd function must be used. This function can add a random message alias, and then the name can be sent to netmessagebuffersend.
The prototype of netmessagebuffersend is:
Net_api_status Netmessagebuffersend (
Lpcwstr Servername,
Lpcwstr Msgname,
Lpcwstr Fromname,
Lpbyte Buf,
DWORD Buflen
);
At the beginning, I thought that the first parameter servername was the name of the target. Later I realized that the name of the server where the function was to be run was used for LAN Management, sometimes you need to control a server to run the program. If you are running on the local machine, set this parameter to null. Many functions starting with "Net" have this parameter.
The second parameter msgname is shown on the Internet as a pointer to the message string to be sent, which is the same as the 4th parameter Buf. I tried it very wrong and all failed. Later, when I saw an explanation from a cool-man, I realized that this msgname does not mean the name of the message string, but the "message alias" of the target machine "! This is generally the name of the target computer. If there are other message aliases on that computer, this can also be one of them, however, the machine that sends the message also needs to know that the alias refers to that computer.
The third parameter is the most difficult. I always thought that I could just give it a name. Then I tracked the error message and said that the name was not found, I think that I used a net name when I used net send. I can add a name xxx through net name xxx/Add, and then use net send to send messages to XXX. Since the name is not found, isn't the name added through net name? So I used the oldest method to execute the net name command through the system () function, add the randomly entered name, and send a message with that name. Haha, it succeeded. However, it is difficult to display the black cmd window. Let's look at msdn. There is a netnameadd function, which can be used to do such a thing.
It is also uncomfortable to use lpcwstr and lpbyte. The value of the lpcwstr is a constant wide string, because the net functions use Unicode. So how can we convert cstring into this? The cstring itself can be seen as the lpctstr, that is, the "constant t string". Haha, the T character is the "riding the wall" character. If you define _ Unicode, it indicates W and width characters, if _ Unicode is not defined, it indicates an ordinary 8-bit character. To be compatible with both the ASCII and Unicode versions, t characters are preferred when defining strings. So how can we convert a cstring into a wide string? On the Internet, we can see that some macros can be used. Those macros are defined in atlconv. H, including:
ATL String Conversion macros
A2bstr |
Ole2a |
T2A |
W2a |
A2cole |
Ole2bstr |
T2bstr |
W2bstr |
A2ct |
Ole2ca |
T2ca |
W2ca |
A2cw |
Ole2ct |
T2cole |
W2cole |
A2ole |
Ole2cw |
T2cw |
W2ct |
A2t |
Ole2t |
T2ole |
W2ole |
A2w |
Ole2w |
T2W |
W2t |
Before each use, add the following sentence to the front:
Uses_conversion;
Then you can easily convert these types. For example, if you want to convert cstring STR to the lpw of the lpcwstr, As long:
# Include <atlconv. h>
...
Uses_conversion;
Lpcwstr lpw = T2W (STR );
In this way, the first three parameters are ready.
The fourth parameter is lpbyte, which is a strange type. It is literally a pointer to the byte type. In C, there is no byte type, and the most like it is the unsigned char type, which is actually it. So how to convert cstring to this? First, we need to convert the cstring into the lpcwstr, because the net uses Unicode, and the character encoding in it must be Unicode. Then, because each character of the lpcwstr is 16bit, it does not meet the requirements of lpbyte, therefore, it must be forcibly converted to lpbyte. In this way, you can write it:
Lpbyte = (lpbyte) T2W (cstring );
However, step-by-step conversion is better:
Lpcwstr = T2W (cstring );
Lpbyte = (lpbyte) lpcwstr;
Because the length of the lpcwstr is required for the buflen parameter. Buflen is the length of the Buf calculated in bytes. We can use wcslen (lpcwstr) to obtain the number of characters in the lpcwstr. Then, the length of each character is sizeof (wchar_t) bytes, therefore, the length of the lpcwstr is wcslen (lpcwstr) * sizeof (wchar_t) bytes.
I want to use the getlength () function of cstring, but it always fails and I don't know why. Cstring. getlength () * sizeof (tchar) is always insufficient. One or more characters are truncated.
Here, the input is done. After this is done, the list of computers in the group and LAN will not be a problem. Write down your experience next time.