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

meimeizsq 2011-02-15 02:06:37
我在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;
}
}
//---------------------------------------------------------------------------
...全文
349 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
svmabingzjm 2012-03-23
  • 打赏
  • 举报
回复
呵呵,解决了吗,猜测是由于vc的动态库调用格式和bcb动态库调用格式不一致所致
meimeizsq 2011-03-01
  • 打赏
  • 举报
回复
楼上的朋友,你说你的项目全是dll,与在哪里调用无关,你觉得我的dll不能在vc中调用,到底是什么问题,如果有问题,我上面的贴的代码很全的,你可以适当的帮我改改,或者说bcb做dll的过程需要注意什么,或者vc调用bcb的dll时需要设置设置什么?可以帮我分析下。我做这个程序就是为了验证bcb用ADOQuery做动态库让vc去调用,再没有其他的代码了,就那些,帮我分析下。我的QQ:491192853
缘中人 2011-02-24
  • 打赏
  • 举报
回复
我的项目全是dll做的,与在哪里调用无关
bigfog 2011-02-23
  • 打赏
  • 举报
回复
会不会是结构的默认对齐方式不同
尝试将dll的导出函数接口的自定义改为基本变量
meimeizsq 2011-02-23
  • 打赏
  • 举报
回复
大家有没有更好的办法,或者大家谁用BCB做过数据库相关的动态库供其他语言(VC,VB)调用。有这方面经验的可以和我共同交流,我的QQ:491192853

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧