关于BCB下ADO的一个BUG,请大家注意!!!!

Thumb168 2003-06-06 12:31:12
近来我得到了一个主程序,主程序是一个与数据库无关的程序,在运行时要挂接一

个DL,DLLL是一个开放式的,已经给了接口,别人可以自己写自己的,用来处理主

程序传来的数据。我自己写的这个DLL要用到数据库支持,我选用了ADO,可是把BCB

下的ADO控件放到DLL中后,系统在退出时就会报错,好象是关于内存出错了。我用

BDE中的控件,就没发现问题,大家看看,是什么原因。

注:ADO控件放到DLL的一个FORM上。不写任可以句代码也会报错。所以认定是一个BUG。
...全文
19 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
luokknd 2003-06-10
gz!
回复
warmwk 2003-06-10
好像有问题 随机的
回复
ThinkX 2003-06-07
只要在Win32平台开发,就躲不开COM/ActiveX,
不能说要对COM规范倒背如流,
但至少基本的原则也要知道,比如COM中资源分配的问题等等。
回复
Behard 2003-06-07
说了那么久,原来是楼主自己的问题
ADO 在 BCB 中是作为 COM 使用的
这样在 DLL 中要使用 CoInitialize(NULL); 和 CoUninitialize();


-----------------------------------
什么时候有时间、有朋友一起去踢球呢?
回复
孩皮妞野 2003-06-07
webmin(webmin)的代码漂亮。
回复
rookieme 2003-06-07
回复
nightfallrove 2003-06-07
唉。。。。
无言
回复
webmin 2003-06-06
建议你看一些关于DLL和COM方面知识,请不要随便就说XX有BUG。
上面的代码参考了 “invalid(空心菜)”以前回答类似所发表的代码。
回复
webmin 2003-06-06
//test
//Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
typedef int (__stdcall *_Open)(char *message);
typedef int (__stdcall *_Close)(char *message);
extern "C" __declspec(dllexport) int __stdcall OpenDB(char *message);
extern "C" __declspec(dllexport) int __stdcall CloseDB(char *message);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
cMessage = new char[100];

hInst = LoadLibrary("Project2.dll");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

OpenDB(cMessage);
ShowMessage(String(cMessage));
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
FreeLibrary(hInst);
delete []cMessage;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
CloseDB(cMessage);
ShowMessage(String(cMessage));
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
_Open _OpenDB;
(FARPROC &)_OpenDB=GetProcAddress(hInst,"OpenDB");
_OpenDB(cMessage);
ShowMessage(String(cMessage));
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
_Close _CloseDB;
(FARPROC &)_CloseDB=GetProcAddress(hInst,"CloseDB");
_CloseDB(cMessage);
ShowMessage(String(cMessage));
}
//---------------------------------------------------------------------------

//Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
char *cMessage;
HINSTANCE hInst;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
回复
webmin 2003-06-06
//Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#include <windows.h>
#pragma hdrstop

#pragma argsused
#include "Unit2.h"
USEFORM("unit2.cpp", DataModule2);
extern "C" __declspec(dllexport) int __stdcall OpenDB(char *message);
extern "C" __declspec(dllexport) int __stdcall CloseDB(char *message);
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
int __stdcall OpenDB(char *message)
{
HRESULT coret = CoInitialize(NULL);
int ret;

switch (coret)
{
case S_OK:
break;
case S_FALSE:
strcpy(message,"Indicates that the library is already initialized or that it could not release default allocator.");
break;
case E_OUTOFMEMORY:
strcpy(message,"Indicates that it was unable to initialize because the system is out of memory.");
break;
case E_INVALIDARG:
strcpy(message,"Indicates the argument is invalid.");
break;
case E_UNEXPECTED:
strcpy(message,"Indicates an unexpected error occurred.");
break;
}
if(coret!=S_OK&&coret!=S_FALSE)
{
return -1;
}
try{
DataModule2 = new TDataModule2(NULL);
}
catch(...)
{
strcpy(message,"创建数据模块失败!");
return -1;
}
try
{
DataModule2->Connect = true;
strcpy(message,"连接数据库成功!");
}
catch(Exception *E)
{
strcpy(message,"连接数据库失败!");
strcat(message,E->Message.c_str());
CoUninitialize();
return -1;
}
return ret;
}
int __stdcall CloseDB(char *message)
{
int ret;
try
{
DataModule2->Connect = false;
strcpy(message,"断开数据库成功!");
}
catch(Exception *E)
{
strcpy(message,"断开数据库失败!");
strcat(message,E->Message.c_str());
ret=-8888;
}
try
{
delete DataModule2;
}
catch(...)
{
strcpy(message,"释放数据模块失败");
ret=-9999;
}
CoUninitialize();
return ret;
}

//Unit2.cpp
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDataModule2 *DataModule2;
//---------------------------------------------------------------------------
__fastcall TDataModule2::TDataModule2(TComponent* Owner)
: TDataModule(Owner)
{
bState = false;
}
//---------------------------------------------------------------------------
void TDataModule2::SetConnectState(bool bOpen)
{
if (bOpen)
{
if (!bState)
{
ADOConnection1->Open();
}
}
else
{
ADOConnection1->Close();
}
bState = bOpen;
}
bool TDataModule2::GetConnectState(void)
{
return bState;
}
//Unit2.h
//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ADODB.hpp>
#include <DB.hpp>
//---------------------------------------------------------------------------
class TDataModule2 : public TDataModule
{
__published: // IDE-managed Components
TADOConnection *ADOConnection1;
TADODataSet *ADODataSet1;
private: // User declarations
protected:
bool bState;
bool GetConnectState(void);
void SetConnectState(bool bOpen);
public: // User declarations
__fastcall TDataModule2(TComponent* Owner);
__property bool Connect={read=GetConnectState,write=SetConnectState};
};
//---------------------------------------------------------------------------
extern PACKAGE TDataModule2 *DataModule2;
//---------------------------------------------------------------------------
#endif
回复
holerescue 2003-06-06
thank you
回复
发动态
发帖子
数据库及相关技术
创建于2007-08-02

1156

社区成员

C++ Builder 数据库及相关技术
申请成为版主
社区公告
暂无公告