K
kyq
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是局域网中异步传递信息可用实例<br>(大富翁中找到的源代码不同程度上存在错误,无法使用):<br><br>以下代码未处理退出时检查是否仍在发送的问题。<br><br>设Form1中有Memo1, Button1两个控件。<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, SendNetMessageImpl;<br><br>type<br> TForm1 = class(TForm, ISendNetMessageCallback)<br> Button1: TButton;<br> Memo1: TMemo;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> procedure OnSendNetMessageComplete(const Succeeded: Boolean; const<br> FromComputer, ToComputer, FromName, Text: string);<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> SendNetMessage(Self, 'andy', 'andy1', 'kyq', 'OK123中文321');<br> SendNetMessage(Self, 'andy', 'andy', 'kyq', 'OK123中文321');<br> SendNetMessage(Self, 'andy', 'andy2', 'kyq', 'OK123中文321');<br>end;<br><br>procedure TForm1.OnSendNetMessageComplete(const Succeeded: Boolean;<br> const FromComputer, ToComputer, FromName, Text: string);<br>begin<br> if Succeeded then<br> Memo1.Lines.Append(<br> Format(<br> 'Succeeded in sending message to %s',<br> [ToComputer]<br> )<br> )<br> else<br> Memo1.Lines.Append(<br> Format(<br> 'Failed in sending message to %s',<br> [ToComputer]<br> )<br> );<br>end;<br><br>end.<br><br><br>unit SendNetMessageImpl;<br><br>interface<br><br>type<br> ISendNetMessageCallback = interface<br> procedure OnSendNetMessageComplete(const Succeeded: Boolean; const<br> FromComputer, ToComputer, FromName, Text: string);<br> end;<br><br>procedure SendNetMessage(const SendNetMessageCallback: ISendNetMessageCallback;<br> const FromComputer, ToComputer, FromName, Text: string);<br><br>implementation<br>uses<br> Classes;<br><br>function NetMessageBufferSend(ServerName: PWideChar;<br> MsgName: PWideChar;<br> FromName: PWideChar;<br> Buf: PWideChar;<br> BufLen: integer): Integer; stdcall;<br> external 'netapi32.dll'<br><br>type<br> TTrdSendNetMessage = class(TThread)<br> protected<br> _Succeeded: Boolean;<br><br> _SendNetMessageCallback: ISendNetMessageCallback;<br> _FromComputer, _ToComputer, _FromName, _Text: string;<br><br> procedure Execute; override;<br><br> procedure InvokeCallback;<br> public<br> constructor Create(const SendNetMessageCallback: ISendNetMessageCallback;<br> const FromComputer, ToComputer, FromName, Text: string);<br> end;<br><br> { TTrdSendNetMessage }<br><br>constructor TTrdSendNetMessage.Create(<br> const SendNetMessageCallback: ISendNetMessageCallback;<br> const FromComputer, ToComputer, FromName, Text: string<br> );<br>begin<br> Assert(SendNetMessageCallback <> nil);<br><br> _SendNetMessageCallback := SendNetMessageCallback;<br><br> _FromComputer := FromComputer;<br> _ToComputer := ToComputer;<br> _FromName := FromName;<br> _Text := Text;<br><br> FreeOnTerminate := True;<br><br> inherited Create(False);<br>end;<br><br>procedure TTrdSendNetMessage.Execute;<br> function SendNetMessage(<br> const FromComputer, ToComputer, FromName, Text: WideString<br> ): Boolean;<br> begin<br> Result := NetMessageBufferSend(<br> PWideChar(FromComputer),<br> PWideChar(ToComputer),<br> PWideChar(FromName),<br> PWideChar(Text),<br> Length(Text) * SizeOf(PWideChar)<br> ) = 0;<br> end;<br>begin<br> _Succeeded := SendNetMessage(<br> _FromComputer,<br> _ToComputer,<br> _FromName,<br> _Text<br> );<br><br> Synchronize(InvokeCallback);<br>end;<br><br>procedure TTrdSendNetMessage.InvokeCallback;<br>begin<br> _SendNetMessageCallback.OnSendNetMessageComplete(<br> _Succeeded,<br> _FromComputer,<br> _ToComputer,<br> _FromName,<br> _Text<br> );<br>end;<br><br>procedure SendNetMessage(const SendNetMessageCallback: ISendNetMessageCallback;<br> const FromComputer, ToComputer, FromName, Text: string);<br>begin<br> TTrdSendNetMessage.Create(<br> SendNetMessageCallback,<br> FromComputer,<br> ToComputer,<br> FromName,<br> Text<br> );<br>end;<br><br>end.