下面是一个小例子,但得不到想要的结果,请高手们帮忙分析!
{服务端}
{接口定义部分代码}
{ Invokable interface IMYTest }unit MYTestIntf;interfaceuses InvokeRegistry, Types, XSBuiltIns;type { Invokable interfaces must derive from IInvokable }
IMYTest = interface(IInvokable)
['{EA425D3A-DC44-4778-92EA-24E376B19792}']
function GetCount():Integer;stdcall;
function SetCount(value: Integer):Integer;stdcall;
{ Methods of Invokable interface must not use the default }
{ calling convention; stdcall is recommended }
end;implementationinitialization
{ Invokable interfaces must be registered }
InvRegistry.RegisterInterface(TypeInfo(IMYTest));end.{接口实现部分代码}
{ Invokable implementation File for TMYTest which implements IMYTest }unit MYTestImpl;interfaceuses InvokeRegistry, Types, XSBuiltIns, MYTestIntf;type { TMYTest }
TMYTest = class(TInvokableClass, IMYTest)
private
v_Count:Integer;
public
function GetCount():Integer;stdcall;
function SetCount(value: Integer):Integer;stdcall;
end;implementation{ TMYTest }function TMYTest.GetCount: Integer;
begin
result := v_Count;
end;function TMYTest.SetCount(value:Integer): Integer;
begin
v_Count := value;
result := v_Count;
end;initialization
{ Invokable classes must be registered }
InvRegistry.RegisterInvokableClass(TMYTest);end.{客户端}unit UMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, UIMyTest, InvokeRegistry, StdCtrls, Rio, SOAPHTTPClient;type
TForm1 = class(TForm)
Edit1: TEdit;
HTTPRIO1: THTTPRIO;
SetButton1: TButton;
GetButton1: TButton;
procedure SetButton1Click(Sender: TObject);
procedure GetButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.SetButton1Click(Sender: TObject);
var Obj: IMyTest;
i: Integer;
begin
Obj := HTTPRIO1 as IMyTest;
i := Obj.SetCount(StrToInt(Edit1.Text));
ShowMessage(IntToStr(i));
Obj := nil;
end;procedure TForm1.GetButton1Click(Sender: TObject);
var obj: IMyTest;
i: Integer;
begin
Obj := HTTPRIO1 as IMyTest;
i := Obj.GetCount;
ShowMessage(IntToStr(i));
Obj := nil;
end;end.要实现在EDIT1中输入一个数字,然后点击<SetButton>后将值传到服务端,然后存储到一个变量中,当客户端点击<GetButton>时,服务端将变量的值传回客户端显示,但每次<GetButton>传回的值都是0.而不是刚刚SET的值,哪位可以解决该问题?
分不够可加!
{服务端}
{接口定义部分代码}
{ Invokable interface IMYTest }unit MYTestIntf;interfaceuses InvokeRegistry, Types, XSBuiltIns;type { Invokable interfaces must derive from IInvokable }
IMYTest = interface(IInvokable)
['{EA425D3A-DC44-4778-92EA-24E376B19792}']
function GetCount():Integer;stdcall;
function SetCount(value: Integer):Integer;stdcall;
{ Methods of Invokable interface must not use the default }
{ calling convention; stdcall is recommended }
end;implementationinitialization
{ Invokable interfaces must be registered }
InvRegistry.RegisterInterface(TypeInfo(IMYTest));end.{接口实现部分代码}
{ Invokable implementation File for TMYTest which implements IMYTest }unit MYTestImpl;interfaceuses InvokeRegistry, Types, XSBuiltIns, MYTestIntf;type { TMYTest }
TMYTest = class(TInvokableClass, IMYTest)
private
v_Count:Integer;
public
function GetCount():Integer;stdcall;
function SetCount(value: Integer):Integer;stdcall;
end;implementation{ TMYTest }function TMYTest.GetCount: Integer;
begin
result := v_Count;
end;function TMYTest.SetCount(value:Integer): Integer;
begin
v_Count := value;
result := v_Count;
end;initialization
{ Invokable classes must be registered }
InvRegistry.RegisterInvokableClass(TMYTest);end.{客户端}unit UMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, UIMyTest, InvokeRegistry, StdCtrls, Rio, SOAPHTTPClient;type
TForm1 = class(TForm)
Edit1: TEdit;
HTTPRIO1: THTTPRIO;
SetButton1: TButton;
GetButton1: TButton;
procedure SetButton1Click(Sender: TObject);
procedure GetButton1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.SetButton1Click(Sender: TObject);
var Obj: IMyTest;
i: Integer;
begin
Obj := HTTPRIO1 as IMyTest;
i := Obj.SetCount(StrToInt(Edit1.Text));
ShowMessage(IntToStr(i));
Obj := nil;
end;procedure TForm1.GetButton1Click(Sender: TObject);
var obj: IMyTest;
i: Integer;
begin
Obj := HTTPRIO1 as IMyTest;
i := Obj.GetCount;
ShowMessage(IntToStr(i));
Obj := nil;
end;end.要实现在EDIT1中输入一个数字,然后点击<SetButton>后将值传到服务端,然后存储到一个变量中,当客户端点击<GetButton>时,服务端将变量的值传回客户端显示,但每次<GetButton>传回的值都是0.而不是刚刚SET的值,哪位可以解决该问题?
分不够可加!
把创建的obj对象保存下来!就可以了。
你设置了,就把obj=nil,不是把这个对象给释放啦!那还跟你保存这个值呢?
Get时,等于是重新创建一个新的服务器对象,当然值为0啦!
但是GetButton1方法中你又创建了一个新的obj: IMyTest,这个不是你要的那个对象啦!
所以还是0
在TForm1的private声明 Obj: IMyTest;
把setbutton,getbutton中的obj声明都去掉
SetButton1把obj=nil去掉
getbutton1写成
procedure TForm1.GetButton1Click(Sender: TObject);
var
i: Integer;
begin
i := Obj.GetCount;
ShowMessage(IntToStr(i));
Obj := nil;
end;