Private Declare Function NetMessageBufferSend Lib "NETAPI32.DLL" (Server As Any, yToName As Byte, yFromName As Any, yMsg As Byte, ByVal lSize As Long) As Long
Private Declare Function NetMessageBufferSend Lib _ "NETAPI32.DLL" (yServer As Any, yToName As Byte, _ yFromName As Any, yMsg As Byte, ByVal lSize As Long) As Long Private Const NERR_Success As Long = 0& Public Function SendMessage(RcptToUser As String, _ FromUser As String, BodyMessage As String) As Boolean
Dim RcptTo() As Byte Dim From() As Byte Dim Body() As Byte RcptTo = RcptToUser & vbNullChar From = FromUser & vbNullChar Body = BodyMessage & vbNullChar If NetMessageBufferSend(ByVal 0&, RcptTo(0), ByVal 0&, _ Body(0), UBound(Body)) = NERR_Success Then SendMessage = True End IfEnd Function Private Sub Form_Load() Dim RetVal As Boolean RetVal = SendMessage("目标机器名", "本机器名", "消息") End Sub
#include <lm.h>
#include <lmmsg.h>#pragma comment(lib,"netapi32.lib")int main()
{
WCHAR msg[]=L"hello."; //所发的信息
WCHAR serv[]=L"nowcan-do-it"; //目标计算机名,如果要给一个工作组广播消息,要在工作组名后面加个"*",如"workgroup*"
WCHAR from[]=L"NowCan"; //你的名字,可以是NULL(这时默认用你的登陆名)
NetMessageBufferSend(NULL,serv,from,(unsigned char *)msg,14);
return 0;
}
或者#define _UNICODE //程序全部使用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[])//这里是个问题,我用BCB怎么也无法链接,说 [Linker Error] Unresolved external '_main' referenced from C:\PROGRAM FILES\BORLAND\CBUILDER5\LIB\C0X32.OBJ,我一直不知道怎么解决,谁有解决方案可要告诉我。
{
wchar_t *wtarget, *wfrom, *buffer;
DWORD dwReturn;
if(argc < 4)
{
printf("Usge: MsgSend <Target> <From> <Message>");
return 0;
}
wtarget = argv[1];
wfrom = argv[2];
buffer = argv[3];dwReturn = NetMessageBufferSend(NULL, wtarget, wfrom, (LPBYTE)buffer, 2*lstrlen(buffer));//注意这里要对得出的长度乘2才是字节数,否则是字符数(一个Unicode字符占2字节)
if(dwReturn == NERR_Success)
{
printf("Send Success!");
}
else
{
printf("Error!");
}
return 0;
}
还有一个小问题,你知道怎么得到Unicode吗?用AnsiString的一些成员函数--WideChar打头的函数都是与Unicode相关的。-*-*-PATCH-*-*- 上面那个wmain的问题已经得到解答。直接用命令行bcc32,如下。bcc32 -WU -WC netmsg.cpp-WU 指定使用Unicode-WC 指定为控制台应用程序-*-*-PATCH-*-*-
yFromName As Any, yMsg As Byte, ByVal lSize As Long) As Long
"NETAPI32.DLL" (yServer As Any, yToName As Byte, _
yFromName As Any, yMsg As Byte, ByVal lSize As Long) As Long
Private Const NERR_Success As Long = 0&
Public Function SendMessage(RcptToUser As String, _
FromUser As String, BodyMessage As String) As Boolean
Dim RcptTo() As Byte
Dim From() As Byte
Dim Body() As Byte RcptTo = RcptToUser & vbNullChar
From = FromUser & vbNullChar
Body = BodyMessage & vbNullChar If NetMessageBufferSend(ByVal 0&, RcptTo(0), ByVal 0&, _
Body(0), UBound(Body)) = NERR_Success Then
SendMessage = True
End IfEnd Function
Private Sub Form_Load() Dim RetVal As Boolean
RetVal = SendMessage("目标机器名", "本机器名", "消息")
End Sub