首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

关于BCB中用ADOConnection和ADOQuery做的动态链接库如何能在VC其他语言中访问的有关问题

2012-03-03 
关于BCB中用ADOConnection和ADOQuery做的动态链接库怎么能在VC其他语言中访问的问题我在BCB中做了一个关于

关于BCB中用ADOConnection和ADOQuery做的动态链接库怎么能在VC其他语言中访问的问题
我在BCB中做了一个关于数据保存和查询的动态连接库,在bcb中测试没问题一切正常,但是在vc中测试经常出错,我把代码贴出来大家帮我修改下。或者大家有什么好的保存数据和查询数据动态库的方法可以参考下。
//---------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include <ADODB.hpp>
#include <DB.hpp>
typedef struct _TCompanyMessage
{
DWORD CompanyID;
char CompanyName[40+1];
}TCompanyMessage;

TADOConnection *ADOConn;
TADOQuery *AD;
//---------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
  if (reason==DLL_PROCESS_ATTACH) // DLL入口
  CoInitialize(NULL);
  else if (reason==DLL_PROCESS_DETACH)
  CoUninitialize(); // DLL结束
  return 1;
}
//---------------------------------------
LRESULT __fastcall Database_Connect(void);
void __fastcall Database_Disconnect(void);
extern "C" __declspec(dllexport) LRESULT __stdcall Save_Company(TCompanyMessage *pCompanyMessage);
extern "C" __declspec(dllexport) LRESULT __stdcall Query_Company(int *iRecordCount,char * CompanyName,TCompanyMessage *pCompanyMessage);
//---------------------------------------
LRESULT __fastcall Database_Connect(void)
{
  AnsiString sExeName = ExtractFilePath(Application->ExeName);
  AnsiString Connstr="Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Persist Security Info=False;Data Source="+sExeName+"Company.mdb";
  ADOConn = new TADOConnection(NULL);
  ADOConn->Connected = false;
  ADOConn->ConnectionString = Connstr;

  ADOConn->LoginPrompt=false;
  ADOConn->Connected=true;
  try
  {
  AD = new TADOQuery(NULL);
  AD->Connection = ADOConn;
  return S_OK;
  }
  catch(...)
  {
  return S_FALSE;
  }
}
//---------------------------------------
void __fastcall Database_Disconnect(void)
{
  delete ADOConn;
  delete AD;
}
//---------------------------------------
LRESULT __declspec(dllexport) __stdcall Save_Company(TCompanyMessage *pCompanyMessage)


{
  if(Database_Connect() == S_OK)
  {
  AD->Close();
  AD->SQL->Clear();
  AD->SQL->Add("SELECT * FROM Company");

  AD->Open();
  AD->Insert();
  AD->FieldByName("CompanyID")->AsString = pCompanyMessage->CompanyID;
  AD->FieldByName("CompanyName")->AsString = pCompanyMessage->CompanyName;
  AD->Post();
  Application->MessageBox("客户信息添加成功!","恭喜",MB_OK|MB_ICONINFORMATION);

  Database_Disconnect();
  return S_OK;
  }
  else
  {
  Application->MessageBox("数据库连接出错!","提示",MB_OK|MB_ICONSTOP);
  return S_FALSE;
  }
}

//---------------------------------------
LRESULT __declspec(dllexport) __stdcall Query_Company(int *RecordCount,char * CompanyName,TCompanyMessage *pCompanyMessage)
{
  if(Database_Connect() == S_OK)
  {
  int iCount = 0;
  AD->Close();
  AD->SQL->Clear();
  AD->SQL->Add("SELECT * FROM Company");
  AD->Open();
  AD->First();
  for(int i=0;i<AD->RecordCount;i++)
  {
  if(strcmp((AD->FieldByName("CompanyID")->AsString).c_str(),CompanyName) == 0)
  {
  pCompanyMessage[iCount].CompanyID = AD->FieldByName("CompanyID")->Value;
  strcpy(pCompanyMessage[iCount].CompanyName,AD->FieldByName("CompanyName")->AsString.c_str());
  iCount++;
  }
  AD->Next();
  }
  *RecordCount = iCount;
  if(*RecordCount != 0)
  return S_OK;
  Database_Disconnect();
  return S_FALSE;
  }
  else
  {
  Application->MessageBox("数据库连接出错!","提示",MB_OK|MB_ICONSTOP);
  return S_FALSE;
  }
}
//---------------------------------------
 

[解决办法]
会不会是结构的默认对齐方式不同
尝试将dll的导出函数接口的自定义改为基本变量
[解决办法]
我的项目全是dll做的,与在哪里调用无关

热点排行