c++builder调用delphi编写的datasnap程序的问题
delphi编写datasnap服务器
new datasnap server向导
unit ServerContainerUnit1;
interface
uses
SysUtils, Classes,
DSTCPServerTransport,
DSServer, DSCommonServer;
type
TServerContainer1 = class(TDataModule)
DSServer1: TDSServer;
DSTCPServerTransport1: TDSTCPServerTransport;
DSServerClass1: TDSServerClass;
procedure DSServerClass1GetClass(DSServerClass: TDSServerClass;
var PersistentClass: TPersistentClass);
private
{ Private declarations }
public
end;
var
ServerContainer1: TServerContainer1;
implementation
uses Windows, ServerMethodsUnit1;
{$R *.dfm}
procedure TServerContainer1.DSServerClass1GetClass(
DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass);
begin
PersistentClass := ServerMethodsUnit1.TServerMethods1;
end;
end.
unit ServerMethodsUnit1;
interface
uses
SysUtils, Classes, DSServer, DBXJSON;
type
TServerMethods1 = class(TDSServerModule)
private
{ Private declarations }
public
{ Public declarations }
pecho: TDBXCallback;
function EchoString(Value: string): string;
procedure reg_echo(pecho: TDBXCallback);
end;
var
ServerMethods1: TServerMethods1;
implementation
{$R *.dfm}
function TServerMethods1.EchoString(Value: string): string;
var
param: TJSONObject;
returnresult: TJSONString;
begin
param := TJSONObject.Create(TJSONPair.Create('echostring',Value));
returnresult := pecho.Execute(param) as TJSONString;
Result := returnresult.Value;
end;
procedure TServerMethods1.reg_echo(pecho: TDBXCallback);
begin
self.pecho := pecho;
end;
end.
c++builder编写客户端代码
//---------------------------------------
#ifndef Unit4H
#define Unit4H
#include <DBXJSON.hpp>
//---------------------------------------
class TEchoCallback : TDBXCallback
{
public:
TJSONValue* __fastcall Execute(const TJSONValue* Arg);
};
#endif
//---------------------------------------
#pragma hdrstop
#include "Unit4.h"
//---------------------------------------
#pragma package(smart_init)
TJSONValue* __fastcall TEchoCallback::Execute(const TJSONValue* Arg)
{
TJSONString* returnstr;
TJSONObject* ArgObj = (TJSONObject*)Arg;
returnstr = new TJSONString(L"hello "+ArgObj->Get(0)->Value());
return returnstr;
}
自动生成的代码
#ifndef Unit3H
#define Unit3H
#include "DBXCommon.hpp"
#include "Classes.hpp"
#include "SysUtils.hpp"
#include "DB.hpp"
#include "SqlExpr.hpp"
#include "DBXDBReaders.hpp"
class TServerMethods1Client : public TObject
{
private:
TDBXConnection *FDBXConnection;
bool FInstanceOwner;
TDBXCommand *FEchoStringCommand;
TDBXCommand *Freg_echoCommand;
public:
__fastcall TServerMethods1Client::TServerMethods1Client(TDBXConnection *ADBXConnection);
__fastcall TServerMethods1Client::TServerMethods1Client(TDBXConnection *ADBXConnection, bool AInstanceOwner);
__fastcall TServerMethods1Client::~TServerMethods1Client();
System::UnicodeString __fastcall EchoString(System::UnicodeString Value);
void __fastcall reg_echo(TDBXCallback* pecho);
};
#endif
//
// Created by the DataSnap proxy generator.
//
#include "Unit3.h"
System::UnicodeString __fastcall TServerMethods1Client::EchoString(System::UnicodeString Value)
{
if (FEchoStringCommand == NULL)
{
FEchoStringCommand = FDBXConnection->CreateCommand();
FEchoStringCommand->CommandType = TDBXCommandTypes_DSServerMethod;
FEchoStringCommand->Text = "TServerMethods1.EchoString";
FEchoStringCommand->Prepare();
}
FEchoStringCommand->Parameters->Parameter[0]->Value->SetWideString(Value);
FEchoStringCommand->ExecuteUpdate();
System::UnicodeString result = FEchoStringCommand->Parameters->Parameter[1]->Value->GetWideString();
return result;
}
void __fastcall TServerMethods1Client::reg_echo(TDBXCallback* pecho)
{
if (Freg_echoCommand == NULL)
{
Freg_echoCommand = FDBXConnection->CreateCommand();
Freg_echoCommand->CommandType = TDBXCommandTypes_DSServerMethod;
Freg_echoCommand->Text = "TServerMethods1.reg_echo";
Freg_echoCommand->Prepare();
}
Freg_echoCommand->Parameters->Parameter[0]->Value->SetCallbackValue(pecho);
Freg_echoCommand->ExecuteUpdate();
}
__fastcall TServerMethods1Client::TServerMethods1Client(TDBXConnection *ADBXConnection)
{
if (ADBXConnection == NULL)
throw EInvalidOperation("Connection cannot be nil. Make sure the connection has been opened.");
FDBXConnection = ADBXConnection;
FInstanceOwner = True;
}
__fastcall TServerMethods1Client::TServerMethods1Client(TDBXConnection *ADBXConnection, bool AInstanceOwner)
{
if (ADBXConnection == NULL)
throw EInvalidOperation("Connection cannot be nil. Make sure the connection has been opened.");
FDBXConnection = ADBXConnection;
FInstanceOwner = AInstanceOwner;
}
__fastcall TServerMethods1Client::~TServerMethods1Client()
{
FreeAndNil(FEchoStringCommand);
FreeAndNil(Freg_echoCommand);
}
主窗口
//---------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <DB.hpp>
#include <DbxDatasnap.hpp>
#include <SqlExpr.hpp>
#include <WideStrings.hpp>
#include "Unit3.h"
#include "Unit4.h"
//---------------------------------------
class TForm2 : public TForm
{
__published:// IDE-managed Components
TSQLConnection *SQLConnection1;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private:// User declarations
public:// User declarations
__fastcall TForm2(TComponent* Owner);
TServerMethods1Client* Server1;
};
//---------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------
#endif
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
Server1 = new TServerMethods1Client(this->SQLConnection1->DBXConnection);
Server1->reg_echo((TDBXCallback*)new TEchoCallback());
}
//---------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
ShowMessage(Server1->EchoString(L"Project2"));
}
//---------------------------------------
[size=24px]远程调用EchoString时报错
Remote error: A callback parameter cannot be found at the provided index.[/size]
[解决办法]
这个问题没有回复?大概是google不到任何有用的信息。
如果楼主的问题还没有解决,请发一个demo工程到我的信箱:cbfans#163.com
我抽时间帮你分析。
[解决办法]
妖哥,你试出来了吗?
我对这个问题也很想知道答案,
更希望知道为什么?