void CDllappDlg::OnButton1() { // TODO: Add your control notification handler code here FARPROC proc; HINSTANCE dllinstance; typedef int(* InitDataBaseDLL)(); InitDataBaseDLL InitBD; dllinstance=::LoadLibrary("..\\dlltest\\debug\\dlltest.dll"); if(dllinstance==NULL) AfxMessageBox("can't open dll file"); else InitBD=(InitDataBaseDLL)GetProcAddress(dllinstance,"InitDataBase"); int i=InitBD();
}// dlltest.cpp : Defines the entry point for the DLL application. //#include "stdafx.h" #include <stdio.h> BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch( ul_reason_for_call ) { case DLL_PROCESS_ATTACH: break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: break; } return TRUE; } _ConnectionPtr m_pConn; extern "C" __declspec(dllexport) InitDataBase() { char strAA[256]; CoInitialize(NULL); m_pConn.CreateInstance(__uuidof(Connection)); try { HRESULT hr=m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown); if(SUCCEEDED(hr)) { int a=0; } } catch(_com_error &e) { _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); char strAA[256]; sprintf(strAA,"Exception thrown for classes generated by #import"); sprintf(strAA,"\tCode=%08lx\n",e.Error()); sprintf(strAA,"\tCode meaning=%s\n",e.ErrorMessage()); sprintf(strAA,"\tSource=%s\n",(LPCTSTR)bstrSource); sprintf(strAA,"\tDescrition=%s\n",(LPCTSTR)bstrDescription); } catch(...) { sprintf(strAA,"*******Unhandled Exception*******"); } CoUninitialize(); return (0);
} 下面是dll中stdafx.h代码 // stdafx.h : include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently //#if !defined(AFX_STDAFX_H__F4C86276_4306_4058_9652_94E2F2819C80__INCLUDED_) #define AFX_STDAFX_H__F4C86276_4306_4058_9652_94E2F2819C80__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // Insert your headers here #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers#include <windows.h>// TODO: reference additional headers your program requires here #import "C:\Program Files\Common Files\System\ado\msado15.dll" \ no_namespace \ rename("EOF","adoEOF")//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__F4C86276_4306_4058_9652_94E2F2819C80__INCLUDED_)
在DLL中按下面方法加入ADO的引用
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
下面是我CPP文件的源代码
// SaveData.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
_ConnectionPtr m_pConn;
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecord;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
CoInitialize(NULL);
break;//Return FALSE to fail DLL load.你的问题就在这里哦 case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
CoUninitialize();
break;
} return TRUE;
}
extern "C" __declspec(dllexport) InitDataBase()
{
m_pConn.CreateInstance(__uuidof(Connection));
try
{
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TEST.mdb","","",adModeUnknown);
}
catch(_com_error e)
{
return (-1);
} _variant_t RecordsAffected;
try
{
m_pRecord= m_pConn->Execute("SELECT COUNT(*) FROM tbl_Temp",&RecordsAffected,adCmdText);
}
catch(_com_error e)
{
// AfxMessageBox("打开表不成功");
return(-1);
}
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecord->GetCollect(vIndex m_pRecord->Close return (int) vIndex.intVal;
}
extern "C" __declspec(dllexport) void go(){}
问题出来了,我在一个工程中调用go函数没有问题
在调用InitDataBase()时,直接反回0,于是我单步调试,发现运行到m_pConn->Open......这句后,直接反回了。这是什么原因?
我在工程中是这样调用的。
FARPROC proc;
HINSTANCE dllinstance;
typedef int(* InitDataBaseDLL)();
InitDataBaseDLL InitBD;
dllinstance=::LoadLibrary("SaveData.dll");
if(dllinstance==NULL) AfxMessageBox("can't open dll file");
else
InitBD=(InitDataBaseDLL)GetProcAddress(dllinstance,"InitDataBase");
int i=InitBD();
m_pConn.CreateInstance(__uuidof(Connection));
_CommandPtr m_pCommand;
_RecordsetPtr m_pRecord;不要使用全局变量,试试局部变量_ConnectionPtr m_pConn(__uuidof(Connection));
m_pConn.CreateInstance(__uuidof(Connection));
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TEST.mdb","","",adModeUnknown);
//这里前后不一致了.
另外InitDataBase()的类型好象也不对。
{
// TODO: Add your control notification handler code here
FARPROC proc;
HINSTANCE dllinstance;
typedef int(* InitDataBaseDLL)();
InitDataBaseDLL InitBD;
dllinstance=::LoadLibrary("..\\dlltest\\debug\\dlltest.dll");
if(dllinstance==NULL)
AfxMessageBox("can't open dll file");
else
InitBD=(InitDataBaseDLL)GetProcAddress(dllinstance,"InitDataBase");
int i=InitBD();
}// dlltest.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
#include <stdio.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
_ConnectionPtr m_pConn;
extern "C" __declspec(dllexport) InitDataBase()
{
char strAA[256];
CoInitialize(NULL); m_pConn.CreateInstance(__uuidof(Connection));
try
{
HRESULT hr=m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown);
if(SUCCEEDED(hr))
{
int a=0;
}
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
char strAA[256];
sprintf(strAA,"Exception thrown for classes generated by #import");
sprintf(strAA,"\tCode=%08lx\n",e.Error());
sprintf(strAA,"\tCode meaning=%s\n",e.ErrorMessage());
sprintf(strAA,"\tSource=%s\n",(LPCTSTR)bstrSource);
sprintf(strAA,"\tDescrition=%s\n",(LPCTSTR)bstrDescription);
}
catch(...)
{
sprintf(strAA,"*******Unhandled Exception*******");
}
CoUninitialize(); return (0);
}
下面是dll中stdafx.h代码
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//#if !defined(AFX_STDAFX_H__F4C86276_4306_4058_9652_94E2F2819C80__INCLUDED_)
#define AFX_STDAFX_H__F4C86276_4306_4058_9652_94E2F2819C80__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Insert your headers here
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers#include <windows.h>// TODO: reference additional headers your program requires here
#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
no_namespace \
rename("EOF","adoEOF")//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__F4C86276_4306_4058_9652_94E2F2819C80__INCLUDED_)