我试过了,可以,但接收到的数据不能调用ShowMessage,不知道为什么?下面是代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, CorbaDM_TLB,
StdCtrls, corbaobj;
type
TCallBack = class(TCorbaImplementation, ICallBack)
protected
procedure Recv(Data: OleVariant;
TargetID: Integer);
safecall;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FCallBack: TCallBack;
CorbaServer: ICorbaServer;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Str: String;
implementation
{$R *.DFM}
procedure OnData();
begin
//这里就停止了,不知道为什么?
//把ShowMessage去掉,一切正常
ShowMessage(Str);
Sleep(100);
end;
function ConvertPointer(Data: Pointer;
Size: Integer): Variant;
var
P: Pointer;
begin
Result := VarArrayCreate([0, Size], VarByte);
p := VarArrayLock(Result);
CopyMemory(p, Data, Size);
VarArrayUnlock(Result);
end;
function ConvertVariant(Data: Variant): Pointer;
var
p: Pointer;
begin
GetMem(Result, SizeOf(Data));
p := VarArrayLock(Data);
CopyMemory(Result, p, SizeOf(Data));
VarArrayUnlock(Data);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// CORBAInitialize;
FCallBack := TCallBack.Create(nil, nil);
CorbaServer := TCorbaServerCorbaFactory.CreateInstance('');
CorbaServer.Register(FCallBack, 1);
end;
{ TCorbaServer }
procedure TCallBack.Recv(Data: OleVariant;
TargetID: Integer);
var
p: Pointer;
test: Ttest;
begin
p := ConvertVariant(Data);
Test := TTest(p^);
Str := test.Str;
// OnData();
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Test: TTest;
begin
Test.Msg := 1;
Test.Str := 'KenII';
while Truedo
CorbaServer.Send(ConvertPointer(@Test, SizeOf(TTest)), 1);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Caption := Str;
end;
end.