B
Boblee
Unregistered / Unconfirmed
GUEST, unregistred user!
程序如下:
unit sendmsgThread;
interface
uses
Classes,Forms,Windows,SysUtils,Dialogs,Math;
type
TSendMsgThread = class(TThread)
Sender,Receiper,MSGString:String;
private
{ Private declarations }
protected
function splitMsgAndSendMsg(Toh,From,Msg:String):integer;
function SendMsg(Toh,From,Msg:string):integer;
procedure Execute;
override;
end;
function NetMessageBufferSend(servernameWideChar;
MsgNameWideChar;
FromNameWideChar;
Buf: PWideChar;
var BufLen:integer):integer;cdecl;//定义函数
function NetMessageBufferSend;
external 'netapi32.dll' name 'NetMessageBufferSend';
implementation
uses
main;
function TSendMsgThread.splitMsgAndSendMsg(Toh,From,Msg:String):integer;
const
MESSAGE_MAX_LEN = 10;
var
ml,segNum:integer;
fl,sn:Extended;
tempStr:string;
begin
Result := 0;
segNum := 0;
fl := Length(msg) / MESSAGE_MAX_LEN;
ml := Ceil(fl) - 1;
sn := fl - ml;
for segNum:=0 to ml do
begin
if ml = segNum then
tempStr := copy(Msg,(segNum*MESSAGE_MAX_LEN + 1), Floor(sn * MESSAGE_MAX_LEN))
else
tempStr := copy(Msg,(segNum*MESSAGE_MAX_LEN + 1),MESSAGE_MAX_LEN);
[gold] Result := Result + SendMsg(Toh,From,tempStr);
//运行了这个funciton后,segNum 就变成了一个很大的值而不是
//常规的for循环那样变成 1. 如果我把这句话注掉,segNum就正常了。
//这是为什么?[/gold]
end;
end;
function TSendMsgThread.SendMsg(Toh,From,Msg:string):integer;
const
COPYTRIGHTPLAIN = #13#10+#13#10+'----========================----'+#13#10+
'Message was sended by NM Messager Explorer';
TRY_SEND_TIMERS = 3;
var
ToName :array [0..64] of WideChar;
WMsgText:array [0..1000] of WideChar;
MsgLen,i,tryNum:integer;
begin
Result := 1;
tryNum := 0;
for i := 0 to 64do
ToName := #0;
for i := Length(Msg)+1 to 1000do
WMsgText := #0;
Msg := Msg + COPYTRIGHTPLAIN;
StringToWideChar(Toh,ToName,i);
StringToWideChar(Msg,WMsgText,i);
while (tryNum < 3) and (Result <> 0)do
begin
Result := NetMessageBufferSend(nil,ToName,nil,@WMsgText,MsgLen);
inc(tryNum);
sleep(2000);
end;
end;
procedure TSendMsgThread.Execute;
var
succ:integer;
errmsg,succmsg:string;
begin
FreeOnTerminate := true;
errmsg := 'Send %s message failed!'+
#13+'Maybe %s not exist or turndo
wn his pc';
succmsg := 'Send %s message Successfully!';
try
succ := splitMsgAndSendMsg(Receiper,Sender,MSGString);
except
On E:Exceptiondo
Application.MessageBox(PChar(errmsg),'Error',MB_OK+MB_ICONERROR);
end;
if (succ <> 0) then
begin
//Application.MessageBox(PChar(errmsg),'Send Report',MB_OK+MB_ICONERROR);
mainFrm.addMSGSendError(Format(errmsg,[Receiper]))
end
else
begin
mainFrm.addMSGSendError(Format(succmsg,[Receiper]));
end;
mainFrm.decReceiverCounter;
if (mainFrm.global_reciverNum < 0) then
mainFrm.ShowSendReport;
end;
end.
问题我加在了上面的程序中。
unit sendmsgThread;
interface
uses
Classes,Forms,Windows,SysUtils,Dialogs,Math;
type
TSendMsgThread = class(TThread)
Sender,Receiper,MSGString:String;
private
{ Private declarations }
protected
function splitMsgAndSendMsg(Toh,From,Msg:String):integer;
function SendMsg(Toh,From,Msg:string):integer;
procedure Execute;
override;
end;
function NetMessageBufferSend(servernameWideChar;
MsgNameWideChar;
FromNameWideChar;
Buf: PWideChar;
var BufLen:integer):integer;cdecl;//定义函数
function NetMessageBufferSend;
external 'netapi32.dll' name 'NetMessageBufferSend';
implementation
uses
main;
function TSendMsgThread.splitMsgAndSendMsg(Toh,From,Msg:String):integer;
const
MESSAGE_MAX_LEN = 10;
var
ml,segNum:integer;
fl,sn:Extended;
tempStr:string;
begin
Result := 0;
segNum := 0;
fl := Length(msg) / MESSAGE_MAX_LEN;
ml := Ceil(fl) - 1;
sn := fl - ml;
for segNum:=0 to ml do
begin
if ml = segNum then
tempStr := copy(Msg,(segNum*MESSAGE_MAX_LEN + 1), Floor(sn * MESSAGE_MAX_LEN))
else
tempStr := copy(Msg,(segNum*MESSAGE_MAX_LEN + 1),MESSAGE_MAX_LEN);
[gold] Result := Result + SendMsg(Toh,From,tempStr);
//运行了这个funciton后,segNum 就变成了一个很大的值而不是
//常规的for循环那样变成 1. 如果我把这句话注掉,segNum就正常了。
//这是为什么?[/gold]
end;
end;
function TSendMsgThread.SendMsg(Toh,From,Msg:string):integer;
const
COPYTRIGHTPLAIN = #13#10+#13#10+'----========================----'+#13#10+
'Message was sended by NM Messager Explorer';
TRY_SEND_TIMERS = 3;
var
ToName :array [0..64] of WideChar;
WMsgText:array [0..1000] of WideChar;
MsgLen,i,tryNum:integer;
begin
Result := 1;
tryNum := 0;
for i := 0 to 64do
ToName := #0;
for i := Length(Msg)+1 to 1000do
WMsgText := #0;
Msg := Msg + COPYTRIGHTPLAIN;
StringToWideChar(Toh,ToName,i);
StringToWideChar(Msg,WMsgText,i);
while (tryNum < 3) and (Result <> 0)do
begin
Result := NetMessageBufferSend(nil,ToName,nil,@WMsgText,MsgLen);
inc(tryNum);
sleep(2000);
end;
end;
procedure TSendMsgThread.Execute;
var
succ:integer;
errmsg,succmsg:string;
begin
FreeOnTerminate := true;
errmsg := 'Send %s message failed!'+
#13+'Maybe %s not exist or turndo
wn his pc';
succmsg := 'Send %s message Successfully!';
try
succ := splitMsgAndSendMsg(Receiper,Sender,MSGString);
except
On E:Exceptiondo
Application.MessageBox(PChar(errmsg),'Error',MB_OK+MB_ICONERROR);
end;
if (succ <> 0) then
begin
//Application.MessageBox(PChar(errmsg),'Send Report',MB_OK+MB_ICONERROR);
mainFrm.addMSGSendError(Format(errmsg,[Receiper]))
end
else
begin
mainFrm.addMSGSendError(Format(succmsg,[Receiper]));
end;
mainFrm.decReceiverCounter;
if (mainFrm.global_reciverNum < 0) then
mainFrm.ShowSendReport;
end;
end.
问题我加在了上面的程序中。