我在写一个关于数据库DLL的dll封装操作,结构如下
Database.dll
|
Mana.dll
|
user program
各个.CPP .H
文件在同一个工程下都可以运行,没有错误,但是
如果按照上面的结构生成的DLL问题就出现了
后来我又将Database的文件打成dll文件,在user program中加入了其它的类文件并使这些类文件使用Database.dll,结果没有问题
但是将所有文件打成一个DLL问题又出现了
下面将所有的类库文件发出来供大家参考,希望有人能告诉我 ,万分感激ODatabase类//用于数据库连接操作
// ODatabase.h: interface for the ODatabase class.
//
//////////////////////////////////////////////////////////////////////
#import "C:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
class __declspec(dllexport) ODatabase
{
public:
bool DisconnectDB();
bool OConnectToDB();
_RecordsetPtr OGetRecordsets(_bstr_t vSql);
ODatabase();
virtual ~ODatabase();
void ODeltDBError(_com_error &e);
_ConnectionPtr pConnection;
_CommandPtr pCommand;
_RecordsetPtr pRecordset;
private:
void OPrintComError(_com_error &e);
void OPrintProviderError(_ConnectionPtr pCnn);
};
// ODatabase.cpp: implementation of the ODatabase class.
//
//////////////////////////////////////////////////////////////////////#include "ODatabase.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////ODatabase::ODatabase()
{}ODatabase::~ODatabase()
{
try
{
if(pRecordset)
if(pRecordset->State == adStateOpen)
pRecordset->Close();
if(pConnection)
if(pConnection->State == adStateOpen)
pConnection->Close();
}
catch(_com_error e)
{
MessageBox(NULL,e.Description(),"系统结束调用错误",MB_ICONINFORMATION);
}
}
void ODatabase::OPrintProviderError(_ConnectionPtr pCnn)
{
ErrorPtr pErr = NULL; //if( (pCnn->Errors->Count) > 0)
{
long nCount = pCnn->Errors->Count;
for(long i = 0; i < nCount; i++)
{
pErr = pCnn->Errors->GetItem(i);
MessageBox(NULL,pErr->GetDescription(),"IDispatch系统调用错误",MB_ICONINFORMATION);
}
}}void ODatabase::OPrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
MessageBox(NULL,(LPCSTR) (bstrSource+bstrDescription),"COM系统调用错误",MB_ICONINFORMATION);
}
void ODatabase::ODeltDBError(_com_error &e)
{
_variant_t vt_Connect = pRecordset->GetActiveConnection();
switch(vt_Connect.vt)
{
case VT_BSTR:
OPrintComError(e);
break;
case VT_DISPATCH:
OPrintComError(e);
break;
//case VT_DISPATCH:
//OPrintProviderError(vt_Connect);
//break;
default:
MessageBox(NULL,"未知系统错误","系统提示",MB_ICONINFORMATION);
break;
}
}_RecordsetPtr ODatabase::OGetRecordsets(_bstr_t vSql)
{
try
{
if(pRecordset)
if(pRecordset->State == adStateClosed)
{
pRecordset->CursorLocation = adUseClient;
pRecordset->Open(vSql,pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adOptionUnspecified);
}
}
catch(_com_error e)
{
ODeltDBError(e);
}
return pRecordset;}bool ODatabase::OConnectToDB()
{
try
{
::CoInitialize(NULL);
_bstr_t cnn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=zhangli;Initial Catalog=POWINSP;Data Source=OCEAN-ZHANGLI";
pConnection.CreateInstance("ADODB.Connection");
pConnection->Open(cnn,"","",adModeUnknown);
pRecordset.CreateInstance("ADODB.Recordset");
return true;
}
catch(_com_error e)
{
MessageBox(NULL,e.Description(),"系统连接错误",MB_ICONINFORMATION);
return false ;
}
}bool ODatabase::DisconnectDB()
{
try
{
if(pConnection->State == adStateOpen)
{
pConnection->Close();
CoUninitialize();
}
else
CoUninitialize();
return true;
}
catch(_com_error e)
{
ODeltDBError(e);
return false;
}
}
Database.dll
|
Mana.dll
|
user program
各个.CPP .H
文件在同一个工程下都可以运行,没有错误,但是
如果按照上面的结构生成的DLL问题就出现了
后来我又将Database的文件打成dll文件,在user program中加入了其它的类文件并使这些类文件使用Database.dll,结果没有问题
但是将所有文件打成一个DLL问题又出现了
下面将所有的类库文件发出来供大家参考,希望有人能告诉我 ,万分感激ODatabase类//用于数据库连接操作
// ODatabase.h: interface for the ODatabase class.
//
//////////////////////////////////////////////////////////////////////
#import "C:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
class __declspec(dllexport) ODatabase
{
public:
bool DisconnectDB();
bool OConnectToDB();
_RecordsetPtr OGetRecordsets(_bstr_t vSql);
ODatabase();
virtual ~ODatabase();
void ODeltDBError(_com_error &e);
_ConnectionPtr pConnection;
_CommandPtr pCommand;
_RecordsetPtr pRecordset;
private:
void OPrintComError(_com_error &e);
void OPrintProviderError(_ConnectionPtr pCnn);
};
// ODatabase.cpp: implementation of the ODatabase class.
//
//////////////////////////////////////////////////////////////////////#include "ODatabase.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////ODatabase::ODatabase()
{}ODatabase::~ODatabase()
{
try
{
if(pRecordset)
if(pRecordset->State == adStateOpen)
pRecordset->Close();
if(pConnection)
if(pConnection->State == adStateOpen)
pConnection->Close();
}
catch(_com_error e)
{
MessageBox(NULL,e.Description(),"系统结束调用错误",MB_ICONINFORMATION);
}
}
void ODatabase::OPrintProviderError(_ConnectionPtr pCnn)
{
ErrorPtr pErr = NULL; //if( (pCnn->Errors->Count) > 0)
{
long nCount = pCnn->Errors->Count;
for(long i = 0; i < nCount; i++)
{
pErr = pCnn->Errors->GetItem(i);
MessageBox(NULL,pErr->GetDescription(),"IDispatch系统调用错误",MB_ICONINFORMATION);
}
}}void ODatabase::OPrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
MessageBox(NULL,(LPCSTR) (bstrSource+bstrDescription),"COM系统调用错误",MB_ICONINFORMATION);
}
void ODatabase::ODeltDBError(_com_error &e)
{
_variant_t vt_Connect = pRecordset->GetActiveConnection();
switch(vt_Connect.vt)
{
case VT_BSTR:
OPrintComError(e);
break;
case VT_DISPATCH:
OPrintComError(e);
break;
//case VT_DISPATCH:
//OPrintProviderError(vt_Connect);
//break;
default:
MessageBox(NULL,"未知系统错误","系统提示",MB_ICONINFORMATION);
break;
}
}_RecordsetPtr ODatabase::OGetRecordsets(_bstr_t vSql)
{
try
{
if(pRecordset)
if(pRecordset->State == adStateClosed)
{
pRecordset->CursorLocation = adUseClient;
pRecordset->Open(vSql,pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adOptionUnspecified);
}
}
catch(_com_error e)
{
ODeltDBError(e);
}
return pRecordset;}bool ODatabase::OConnectToDB()
{
try
{
::CoInitialize(NULL);
_bstr_t cnn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=zhangli;Initial Catalog=POWINSP;Data Source=OCEAN-ZHANGLI";
pConnection.CreateInstance("ADODB.Connection");
pConnection->Open(cnn,"","",adModeUnknown);
pRecordset.CreateInstance("ADODB.Recordset");
return true;
}
catch(_com_error e)
{
MessageBox(NULL,e.Description(),"系统连接错误",MB_ICONINFORMATION);
return false ;
}
}bool ODatabase::DisconnectDB()
{
try
{
if(pConnection->State == adStateOpen)
{
pConnection->Close();
CoUninitialize();
}
else
CoUninitialize();
return true;
}
catch(_com_error e)
{
ODeltDBError(e);
return false;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------OEmployee类为员工
// OEmployee.h: interface for the OEmployee class.
//
//////////////////////////////////////////////////////////////////////
#include "ODatabase.h" // Added by ClassView
class __declspec(dllexport) OEmployee
{
public:
_bstr_t emp_perm;
_bstr_t emp_password;
_bstr_t emp_id;
_bstr_t emp_desc;
_bstr_t emp_name;
_bstr_t emp_no;
OEmployee();
virtual ~OEmployee();};// OEmployee.cpp: implementation of the OEmployee class.
//
//////////////////////////////////////////////////////////////////////#include "OEmployee.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////OEmployee::OEmployee()
{}OEmployee::~OEmployee()
{}OTeam类为员工的队伍集合
// OTeam.h: interface for the OTeam class.
//
////////////////////////////////////////////////////////////////////////#include "ODatabase.h" // Added by ClassView
#include "OEmployee.h" // Added by ClassView
class __declspec(dllexport) OTeam
{
public:
void OCreateTeamEmp();
bool FindAEmployee(OEmployee emp);
_bstr_t team_id;
bool OIsEof();
void Next();
void Begin(); _bstr_t team_no;
_bstr_t team_name;
_bstr_t team_desc;
OEmployee employee;
bool OAddAEmployee(OEmployee emp,OTeam team);
bool ODeleteAEmployee(_bstr_t emp_id);
void OUpdateData(bool ifupdate);
ODatabase *ActiveDatabase;
OTeam();
virtual ~OTeam();};
// OTeam.cpp: implementation of the OTeam class.
//
//////////////////////////////////////////////////////////////////////#include "OTeam.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////OTeam::OTeam()
{}OTeam::~OTeam()
{}void OTeam::OUpdateData(bool ifupdate)
{
try
{
if(ActiveDatabase->pRecordset->State == adStateOpen)
if(!ifupdate)
{
ActiveDatabase->pRecordset->PutCollect("id",employee.emp_id);
ActiveDatabase->pRecordset->PutCollect("emp_no",employee.emp_no);
ActiveDatabase->pRecordset->PutCollect("emp_name",employee.emp_name);
ActiveDatabase->pRecordset->PutCollect("emp_desc",employee.emp_desc);
ActiveDatabase->pRecordset->Update();
}
else
{
employee.emp_id = (bstr_t)ActiveDatabase->pRecordset->GetCollect("id");
employee.emp_no = (bstr_t)ActiveDatabase->pRecordset->GetCollect("emp_no");
employee.emp_name = (bstr_t)ActiveDatabase->pRecordset->GetCollect("emp_name");
employee.emp_desc = (bstr_t)ActiveDatabase->pRecordset->GetCollect("emp_desc");
}
}
catch(_com_error e)
{
ActiveDatabase->ODeltDBError(e);
}
}bool OTeam::ODeleteAEmployee(_bstr_t emp_id)
{
try
{
_bstr_t sql;
sql = "DELETE FROM TBL_EMPLOYEE WHERE ID = " + emp_id ;
ActiveDatabase->pConnection->Execute(sql,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
ActiveDatabase->ODeltDBError(e);
return false;
}
}bool OTeam::OAddAEmployee(OEmployee emp,OTeam team)
{
try
{
_bstr_t sql;
sql = "INSERT INTO TBL_EMPLOYEE (EMP_NO,EMP_NAME,TEAM_ID,EMP_DESC,PASSWORD,EMP_PERM) VALUES ('" ;
sql += emp.emp_no + "','" ;
sql += emp.emp_name + "',";
sql += team.team_id + ",'";
sql += emp.emp_desc + "'";
sql += emp.emp_password + "',";
sql += emp.emp_perm + ")";
ActiveDatabase->pConnection->Execute((_bstr_t)sql,NULL,adCmdText);
sql = "SELECT SCOPE_IDENTITY() AS ID FROM TBL_EMPLOYEE";
ActiveDatabase->pRecordset = ActiveDatabase->OGetRecordsets(sql);
employee.emp_id = ActiveDatabase->pRecordset->GetCollect("ID");
employee.emp_no = emp.emp_no;
employee.emp_name = emp.emp_name;
employee.emp_password = emp.emp_password;
employee.emp_perm = emp.emp_perm;
return true;
}
catch(_com_error e)
{
ActiveDatabase->ODeltDBError(e);
return false;
}
}void OTeam::Begin()
{
if(ActiveDatabase->pRecordset->RecordCount != 0)
{
ActiveDatabase->pRecordset->MoveFirst();
OUpdateData(true);
}
}void OTeam::Next()
{
ActiveDatabase->pRecordset->MoveNext();
if(!ActiveDatabase->pRecordset->adoEOF)
OUpdateData(true);
}bool OTeam::OIsEof()
{
return (ActiveDatabase->pRecordset->adoEOF);
}bool OTeam::FindAEmployee(OEmployee emp)
{
return true;
}void OTeam::OCreateTeamEmp()
{
if(ActiveDatabase->OConnectToDB())
{
ActiveDatabase->OGetRecordsets("SELECT * FROM TBL_EMPLOYEE WHERE TEAM_ID = " + team_id);
}
}
下接上文OTeams类为队伍集合
// OTeams.h: interface for the OTeams class.
//
////////////////////////////////////////////////////////////////////////#include "ODatabase.h" // Added by ClassView
#include "OTeam.h" // Added by ClassView
class __declspec(dllexport) OTeams
{
public:
bool FindATeam(OTeam team);
void Begin();
void Next();
bool OIsEof();
void OUpdateData(bool ifupdate);
OTeam team;
bool ODeleteATeam(_bstr_t team_id);
bool OAddATeam(_bstr_t team_no,_bstr_t team_name,_bstr_t team_desc);
ODatabase *ActiveDatabase;
void OCreateTeams(_bstr_t vSql);
OTeams();
virtual ~OTeams();};// OTeams.cpp: implementation of the OTeams class.
//
//////////////////////////////////////////////////////////////////////#include "OTeams.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////OTeams::OTeams()
{}OTeams::~OTeams()
{}void OTeams::OCreateTeams(_bstr_t vSql)
{
if(ActiveDatabase->OConnectToDB())
{
ActiveDatabase->OGetRecordsets(vSql);
}
}bool OTeams::OAddATeam(_bstr_t team_no, _bstr_t team_name, _bstr_t team_desc)
{
try
{
_bstr_t sql;
sql = "INSERT INTO TBL_TEAM (TEAM_NO,TEAM_NAME,TEAM_DESC) VALUES ('" ;
sql += team_no + "','" ;
sql += team_name + "','";
sql += team_desc + "')";
ActiveDatabase->pConnection->Execute((_bstr_t)sql,NULL,adCmdText);
sql = "SELECT SCOPE_IDENTITY() AS ID FROM TBL_TEAM";
ActiveDatabase->pRecordset = ActiveDatabase->OGetRecordsets(sql);
team.team_id = ActiveDatabase->pRecordset->GetCollect("ID");
team.team_no = team_no;
team.team_name = team_name;
team.team_desc = team_desc;
return true;
}
catch(_com_error e)
{
ActiveDatabase->ODeltDBError(e);
return false;
}
}bool OTeams::ODeleteATeam(_bstr_t team_id)
{
try
{
_bstr_t sql;
sql = "DELETE FROM TBL_TEAM WHERE ID = " + team_id ;
ActiveDatabase->pConnection->Execute(sql,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
ActiveDatabase->ODeltDBError(e);
return false;
}
}void OTeams::OUpdateData(bool ifupdate)
{
try
{
if(ActiveDatabase->pRecordset->State == adStateOpen)
if(!ifupdate)
{
ActiveDatabase->pRecordset->PutCollect("id",team.team_id);
ActiveDatabase->pRecordset->PutCollect("team_no",team.team_no);
ActiveDatabase->pRecordset->PutCollect("team_name",team.team_name);
ActiveDatabase->pRecordset->PutCollect("team_desc",team.team_desc);
ActiveDatabase->pRecordset->Update();
}
else
{
team.team_id = (bstr_t)(ActiveDatabase->pRecordset->GetCollect("ID"));
team.team_no = (bstr_t)(ActiveDatabase->pRecordset->GetCollect("TEAM_NO"));
team.team_name = (bstr_t)(ActiveDatabase->pRecordset->GetCollect("TEAM_NAME"));
team.team_desc = (bstr_t)(ActiveDatabase->pRecordset->GetCollect("TEAM_DESC"));
}
}
catch(_com_error e)
{
ActiveDatabase->ODeltDBError(e);
}
}bool OTeams::OIsEof()
{
return (ActiveDatabase->pRecordset->adoEOF);
}void OTeams::Next()
{
ActiveDatabase->pRecordset->MoveNext();
if(!ActiveDatabase->pRecordset->adoEOF)
OUpdateData(true);
}void OTeams::Begin()
{
if(ActiveDatabase->pRecordset->RecordCount != 0)
{
ActiveDatabase->pRecordset->MoveFirst();
OUpdateData(true);
}
}bool OTeams::FindATeam(OTeam team)
{
return true;
}-------------------------------------------------------------------------------------
下面是USER PROGRAM使用dll的方法内容
void CUDlg::OnOK()
{
ODatabase d;
d.OConnectToDB();
OTeams teams;
teams.ActiveDatabase = &d;
_bstr_t sql;
_bstr_t m;
sql = "SELECT * FROM TBL_TEAM";
teams.OCreateTeams(sql);
teams.Begin();
while(!teams.OIsEof())
{
MessageBox(teams.team.team_name);
teams.Next();
}}
可以出现一个对话框,下面就出错了
错误内容如下
Debug Assertion Failed!
Program :...rogram files/Microsoft Visual Studio/**********/**.exe
File : dbgheap.c
Line :1011
Expression:_CrtIsValidHeapPointer(pUserData)For information on your program can cause a assertion failure ,see the
Visual c++ documentation on asserts;去掉MessageBox那句就没有问题,真是奇怪,解决问题者立刻送分加感激万分。呵呵。
Line :1011
我也遇到类似的问题,不知是怎么回事。
应该是pUserData所指的数据的内存块已经被破坏了!
{
::MessageBox(NULL, teams.team.team_name, MB_OK);
teams.Next();
}