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

一个DLL调用ACCESS库的奇怪有关问题(有源码,帮调试下,谢 )

2012-02-11 
一个DLL调用ACCESS库的奇怪问题(有源码,帮调试下,谢 )调用DLL,DLL功能就是连接库、打开表,分开调用,也就是

一个DLL调用ACCESS库的奇怪问题(有源码,帮调试下,谢 )
调用DLL,DLL功能就是连接库、打开表,分开调用,也就是一个按钮调用一个函数没有问题,但三个函数合在一个按钮调用就会出错,不知怎么回事?编译没问题,哪个老大帮调试下,谢谢
DLL:
#include <ADODB.hpp>
#include <DB.hpp>
extern "C"__declspec(dllexport) int _stdcall LinkDatabase(TADOConnection *m_AccessConn,String m_DatabaseFile,String m_Pwd="");
extern "C"__declspec(dllexport) bool _stdcall Connection1(TADOConnection *m_AccessConn,TADOQuery *m_Query);

extern "C"__declspec(dllexport) int _stdcall OpenTable(TADOQuery *m_Query,String m_TableName,int m_Flag=0,String m_SQL="");
//=========================
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
  switch (reason)
  {
  case DLL_PROCESS_ATTACH:
  CoInitialize(NULL);
  break;
  case DLL_PROCESS_DETACH:
  CoUninitialize();
  break;
  case DLL_THREAD_ATTACH: break;
  case DLL_THREAD_DETACH: break;
  default:
  break;
  }
  return 1;
}
//---------------------------------------
int LinkDatabase(TADOConnection *m_AccessConn,String m_DatabaseFile,String m_Pwd)
{
  WideString Link;
  if(m_AccessConn==NULL)
  return -1;
  if(m_DatabaseFile.Trim().Length()==0)
  return -2;
  if(FileGetAttr(m_DatabaseFile) & faReadOnly==faReadOnly)  
  FileSetAttr(m_DatabaseFile,0);
  Link="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+m_DatabaseFile+";\
  User ID=admin;Jet OleDb:Database Password="+m_Pwd;
  if(m_AccessConn->Connected==false) //如果没有连接到Access
  {
  try
  {
  m_AccessConn->ConnectionString=Link;
  m_AccessConn->LoginPrompt=false;
  m_AccessConn->Connected=true;
  return 0;
  }
  catch(...)
  {
  return -100;
  }
  }
  return -100;
}
//-----------------------------------------
bool Connection1(TADOConnection *m_AccessConn,TADOQuery *m_Query)
{
  if(m_AccessConn==NULL || m_Query==NULL)
  return false;
  m_Query->Connection=m_AccessConn;
  m_AccessConn->Connected=true;
  return true;
}
//-----------------------
int OpenTable(TADOQuery *m_Query,String m_TableName,int m_Flag,String m_SQL)
{
  String m_str;
  if(m_Query==NULL)
  return -1;
  if(m_TableName.Trim().Length()==0)
  return -2;
  m_Query->Close(); m_Query->SQL->Clear();
  switch(m_Flag)
  {
  case 0:
  default:
  m_str="Select * From "+m_TableName;
  break;
  case 1:
  m_str=m_SQL;
  break;
  }
  try
  {
  m_Query->SQL->Add(m_str);
  m_Query->Active=true;
  m_Query->Open();

  }
  catch(...)
  {
  return -100;
  }
  return m_Query->RecordCount;
}
//-----------------------------------------

调用:
HINSTANCE hDLL;
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  hDLL=LoadLibrary("Database.dll");
}
//----------------------------------------Database.dll-----------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)//一个按钮连续调用三个函数有问题,但分开三个按钮分别调用就没问题,不知怎么回事?错误提示:EAccessViolation


{
 try
 {
  typedef int (*TLinkDatabase)(TADOConnection *m_AccessConn,String m_DatabaseFile,String m_Pwd="");
  TLinkDatabase LinkDatabase;
  LinkDatabase=(TLinkDatabase)GetProcAddress(hDLL,"LinkDatabase");
  LinkDatabase(ADOConnection1,"c:\\系统文件.mdb");
  }
  catch(Exception &E)
  {
  ShowMessage("1"+E.Message);
  }
  try
  {
  typedef int (*TConnection1)(TADOConnection *m_AccessConn,TADOQuery *m_Query);
  TConnection1 Connection1;
  Connection1=(TConnection1)GetProcAddress(hDLL,"Connection1");
  Connection1(ADOConnection1,ADOQuery1);
  }
  catch(Exception &E)
  {
  ShowMessage("2"+E.Message);
  }
  try
  {
  typedef int (*TOpenTable)(TADOQuery *m_Query,String m_TableName,int m_Flag=0,String m_SQL="");
  TOpenTable OpenTable;
  OpenTable=(TOpenTable)GetProcAddress(hDLL,"OpenTable");
  Caption=(int)OpenTable ;
  OpenTable(ADOQuery1,"打印报表");
  }
  catch(Exception &E)
  {
  ShowMessage("3"+E.Message);
  }

}
//---------------------------------------

[解决办法]
DLL
//===================================================================================
#include <vcl.h>
#include <ADODB.hpp >
#include <DB.hpp >

#include <windows.h>
#pragma hdrstop

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
CoInitialize(NULL);
break;
case DLL_PROCESS_DETACH:
CoUninitialize();
break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
default:
break;
}
return 1;
}
//---------------------------------------
int __stdcall LinkDatabase(TADOConnection *m_AccessConn,String m_DatabaseFile,String m_Pwd)
{
WideString Link;
if(m_AccessConn==NULL)
return -1;
if(m_DatabaseFile.Trim().Length()==0)
return -2;
if(FileGetAttr(m_DatabaseFile) & faReadOnly==faReadOnly)
FileSetAttr(m_DatabaseFile,0);
Link="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+m_DatabaseFile
+";User ID=admin;Password="+m_Pwd;

// "Provider=microsoft.jet.oledb.4.0;data source=your_database_path;user id=admin;password=pass;"

if(m_AccessConn->Connected==false) //如果没有连接到Access
{
try
{
m_AccessConn->ConnectionString=Link;
m_AccessConn->LoginPrompt=false;
m_AccessConn->Connected=true;
return 0;
}
catch(...)
{
return -100;
}

return -100;
}
//-----------------------------------------
bool __stdcall Connection1(TADOConnection *m_AccessConn,TADOQuery *m_Query)
{
if(m_AccessConn==NULL || m_Query==NULL)
return false;
m_Query->Connection=m_AccessConn;
m_AccessConn->Connected=true;
return true;
}
//----------------------- 


int __stdcall OpenTable(TADOQuery *m_Query,String m_TableName,int m_Flag,String m_SQL)
{
String m_str;
if(m_Query==NULL)
return -1;
if(m_TableName.Trim().Length()==0)
return -2;
m_Query->Close(); m_Query->SQL->Clear();
switch(m_Flag)

case 0:
default:
m_str="Select * From "+m_TableName;
break;
case 1:
m_str=m_SQL;
break;
}
try
{
m_Query->SQL->Add(m_str);
m_Query->Open();

}
catch(...)
{
return -100;
}
return m_Query->RecordCount;
}
//================================================================================
调用: 
//================================================================================
HINSTANCE hDLL;

//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
hDLL=LoadLibrary("Database.dll"); 
}
//---------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
typedef int __stdcall (*TLinkDatabase)(TADOConnection *m_AccessConn,String m_DatabaseFile,String m_Pwd="");
TLinkDatabase LinkDatabase;
LinkDatabase=(TLinkDatabase)GetProcAddress(hDLL,"LinkDatabase");
int i = LinkDatabase(ADOConnection1,"c:\\FixedWeight.mdb");
ShowMessage(i);
}
catch( Exception &E)
{
ShowMessage("1"+E.Message);
}
try
{
typedef int (*TConnection1)(TADOConnection *m_AccessConn,TADOQuery *m_Query);
TConnection1 Connection1;
Connection1=(TConnection1)GetProcAddress(hDLL,"Connection1");
int i =Connection1(ADOConnection1,ADOQuery1);
ShowMessage(i);

catch(Exception &E)
{
ShowMessage("2"+E.Message);
}
try

typedef int __stdcall (*TOpenTable)(TADOQuery *m_Query,String m_TableName,int m_Flag=0,String m_SQL="");
TOpenTable OpenTable;
OpenTable=(TOpenTable)GetProcAddress(hDLL,"OpenTable");
Caption=(int)OpenTable ;
int i = OpenTable(ADOQuery1,"Weighing");
ShowMessage(i);
}
catch(Exception &E)
{
ShowMessage("3"+E.Message);
}
}

热点排行