如何在BCB中开发多层系统(和Delphi真的不大一样)。。。 (100分)

  • 主题发起人 主题发起人 springhill
  • 开始时间 开始时间
S

springhill

Unregistered / Unconfirmed
GUEST, unregistred user!
公司要用BCB做多层的开发,我是用李维的书来学习的,但公司现在只有我在做这一块的贮备工作,没有人交流,遇到了很大的困难,如下问题如何解决(例子程序):
服务端:
STDMETHODIMP TDynamicSQLDemoServerImpl::GetAllTables(VARIANT* vTables)
{
TStringList *tblList=new TStringList();
try
{
DynamicSQLDemoServer->ADOConnection1->GetTableNames(tblList);
const int tblcnt=tblList->Count;
ShowMessage(IntToStr(tblcnt));
int bounds[2]={0,tblcnt-1};
vTables=VarArrayCreate(bounds,tblcnt,varVariant);
for (int i=0;i<tblcnt;++i)
{
vTables=(Variant)tblList->Strings;
}
ShowMessage(IntToStr(VarArrayHighBound(vTables,1)));
}
__finally
{
delete tblList;
}
return S_OK;
}
客户端:
void __fastcall TForm2::btnConnectClick(TObject *Sender)
{
Variant vtblList;
SocketConnection1->Connected=true;
IDispatch* disp = (IDispatch*)(SocketConnection1->AppServer);
IDynamicSQLDemoServerDisp TempInterface((IDynamicSQLDemoServer*)disp);
TempInterface.GetAllTables(vtblList);

//ShowMessage(VarToStr(vtblList));
if (VarIsArray(vtblList))
{
ShowMessage("Is Array");
for (int i=VarArrayLowBound(vtblList,1);i<=VarArrayHighBound(vtblList,1);++i)
{
lbTables->Items->Add((&amp;vtblList));
}
}
else
{
ShowMessage("Is not Array");
}
}
没有效果,请做过多层开发的或照着李维书学习的BCBer帮我解决一下。
 
调用方式是对的。只不过这函数?变量类型能存储数组吗?
最好是返回一串包,然后客户端自己解包了。比如中间用~分隔。
 
问题出在Variant数组上:
服务端GetAllTables中:
vTables = VarArrayCreate( bounds, 1, varVariant );
for( int i = 0;
i < bounds;
i++ )
vTables.PutElement( (Variant)tblList->Strings, i );
客户端btnConnectClick中:
for(int i=VarArrayLowBound(vtblList,1);i<=VarArrayHighBound(vtblList,1);++i)
lbTables->Items->Add(vtblList.GetElement(i));
 
感谢apw的回答,但还是不行
以下是所有的代码,请大虾看看,这一关过不了,没法向下学。
客户端:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DynamicSQLDemoClient.h"
#include "PDynamicSQLDemoServer_TLB.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//***********************************************************************
void __fastcall TForm2::btnConnectClick(TObject *Sender)
{
Variant *vtblList=new Variant();
//int iCnt;
SocketConnection1->Connected=true;
IDispatch* disp = (IDispatch*)(SocketConnection1->AppServer);
IDynamicSQLDemoServerDisp TempInterface((IDynamicSQLDemoServer*)disp);
TempInterface.GetAllTables((tagVARIANT*)vtblList);
//if (VarIsArray(vtblList))
//{
//ShowMessage("Is Array");
for (int i=VarArrayLowBound(vtblList,1);i<=VarArrayHighBound(vtblList,1);++i)
{
lbTables->Items->Add(vtblList->GetElement(i));
}
//}
//else
//{
//ShowMessage("Is not Array");
//}
}
//***********************************************************************
//---------------------------------------------------------------------------
void __fastcall TForm2::btnRunClick(TObject *Sender)
{
const static AnsiString sSQL="select * form ";
AnsiString sSelect;
if (PageControl1->ActivePageIndex==0)
{
sSelect=sSQL+lbTables->Items->Strings[lbTables->ItemIndex];
ClientDataSet1->Close();
ClientDataSet1->CommandText=sSelect;
ClientDataSet1->Open();
}
else
{
sSelect=mmSQL->Text;
ClientDataSet1->Close();
ClientDataSet1->CommandText=sSelect;
ClientDataSet1->Open();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
int *pivalue=new int();
SocketConnection1->Connected=true;
//if ((IDispatch*)SocketConnection1->AppServer!=0) ShowMessage("Is not NULL");
IDispatch* disp = (IDispatch*)(SocketConnection1->AppServer);
IDynamicSQLDemoServerDisp TempInterface((IDynamicSQLDemoServer*)disp);
if (TempInterface)
{
TempInterface.GetAnInteger(pivalue);
ShowMessage(IntToStr(*pivalue));
//ShowMessage("Is not NULL");
}
else
ShowMessage("Is NULL");
}
//---------------------------------------------------------------------------
主意//*和*//扩起来的函数
服务段:
#include <vcl.h>
#pragma hdrstop
//#include "DYNAMICSQLDEMOSERVERIMPL.H"
#include "DynamicSQLDemoServerImpl.h"
#if !defined(__PRAGMA_SMART_INIT_DEFINED)
#define __PRAGMA_SMART_INIT_DEFINED
#pragma package(smart_init)
#endif
#pragma resource "*.dfm"

/////////////////////////////////////////////////////////////////////////////
// Global instance of TDynamicSQLDemoServerImpl
/////////////////////////////////////////////////////////////////////////////
TDynamicSQLDemoServer *DynamicSQLDemoServer;

/////////////////////////////////////////////////////////////////////////////
// TDynamicSQLDemoServer::TDynamicSQLDemoServer
/////////////////////////////////////////////////////////////////////////////
__fastcall TDynamicSQLDemoServer::TDynamicSQLDemoServer(TComponent* Owner) : TCRemoteDataModule(Owner)
{
}
//*************************************************
STDMETHODIMP TDynamicSQLDemoServerImpl::GetAllTables(VARIANT* vTables)
{
TStringList *tblList=new TStringList();
try
{
DynamicSQLDemoServer->ADOConnection1->GetTableNames(tblList);
int tblcnt=tblList->Count;
int bounds[2]={0,tblcnt-1};
vTables=VarArrayCreate(bounds,tblcnt,varVariant);
for (int i=0;i<tblcnt;++i)
{
((Variant*)vTables)->PutElement((Variant)tblList->Strings, i);
}
}
__finally
{
delete tblList;
}
return S_OK;
}
//*************************************************
STDMETHODIMP TDynamicSQLDemoServerImpl::GetAnInteger(int* ivalue)
{
*ivalue=10;
return S_OK;
}
顺便也请谈谈VARIANT和OleVARIANT的用法
 
后退
顶部