http://expert.csdn.net/Expert/topic/2475/2475732.xml?temp=.3599817
解决方案 »
- 程序在关闭的时候说内存错误,这个怎么检查?
- 修改MFC的编辑框(Edit box)命令ID后,无法运行
- 如何侦测CDHtmlDialog的错误讯息
- vc+access+odbc下如何判断指定数据库中已存在kkk的表?谢谢了!
- 小问题,字符串转换,TCHAR -> LPCSTR
- 请问:Windows的“管理工具”中的“计算机管理”是怎么实现在分区的???
- 视窗分割问题,出60分,到http://community.csdn.net/Expert/TopicView.asp?id=3317449 领分
- CDC问题:怎样从屏幕尺寸转换到厘米呢?反之呢?
- MFC编辑框输入非法字符程序崩溃,现象诡异。
- 关于Dialog的问题
- 500分求助!!!第五贴!解决了分给一个人!!!http://expert.csdn.net/Expert/topic/2475/2475732.xml?temp=.3599817
- 500分求助!!第三贴!解决了分给一个人!!http://expert.csdn.net/Expert/topic/2475/2475732.xml?temp=.3599817
在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));
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TEST.mdb","","",adModeUnknown);
//这里前后不一致了.
另外InitDataBase()的类型好象也不对。
改为
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TEST.mdb","","",-1);就不会出错了。见MSDN
ms-help://MS.MSDNQTR.2002JAN.1033/his/thorref4_5e8y.htm
{
// 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_)