在Application结束时创建ADO Command对象时出现异常

时间:2013-11-10 15:57:44

标签: com mfc ado createinstance

我现在正在将一个大型软件从OWL转移到MFC,大部分任务已经完成,但是当应用程序关闭此异常时,我面临很多类似的问题。 enter image description here 当我点击打破debuger引导我到这 enter image description here 那就是调用堆栈

    msdart.dll!_UMSEnterCSWraper()  Unknown
    msado15.dll!ATL::AtlModuleGetClassObject(struct ATL::_ATL_MODULE *,struct _GUID const &,struct _GUID const &,void * *)  Unknown
    msado15.dll!ATL::CComModule::GetClassObject(struct _GUID const &,struct _GUID const &,void * *) Unknown
    msado15.dll!_DllGetClassObject@12() Unknown
    ole32.dll!CClassCache::CDllPathEntry::DllGetClassObject(const _GUID & rclsid, const _GUID & riid, IUnknown * * ppUnk, int fMakeValid) Line 3317 C++
    ole32.dll!CClassCache::CDllFnPtrMoniker::BindToObject(const _GUID & riid, void * * ppvResult)   C++
    ole32.dll!CClassCache::SearchForLoadedClass(const ACTIVATION_PROPERTIES & ap, CClassCache::CDllClassEntry * * ppDCE)    C++
    ole32.dll!ICoCreateInstanceEx(const _GUID & Clsid, IUnknown * punkOuter, unsigned long dwClsCtx, _COSERVERINFO * pServerInfo, unsigned long dwCount, unsigned long dwActvFlags, tagMULTI_QI * pResults, ActivationPropertiesIn * pActIn) Line 1163  C++
    ole32.dll!CComActivator::DoCreateInstance(const _GUID & Clsid, IUnknown * punkOuter, unsigned long dwClsCtx, _COSERVERINFO * pServerInfo, unsigned long dwCount, tagMULTI_QI * pResults, ActivationPropertiesIn * pActIn) Line 332  C++
    ole32.dll!CoCreateInstanceEx(const _GUID & Clsid, IUnknown * punkOuter, unsigned long dwClsCtx, _COSERVERINFO * pServerInfo, unsigned long dwCount, tagMULTI_QI * pResults) Line 157    C++
    ole32.dll!CoCreateInstance(const _GUID & rclsid, IUnknown * pUnkOuter, unsigned long dwContext, const _GUID & riid, void * * ppv) Line 110  C++
>   DBCore.dll!_com_ptr_t<_com_IIID<ADODB::_Command,&_GUID_b08400bd_f9d1_4d02_b856_71d5dba123e9> >::CreateInstance(const _GUID & rclsid, IUnknown * pOuter, unsigned long dwClsContext) Line 586    C++
    DBCore.dll!TADOCommand::Init(TADOConnection * connection) Line 32   C++
    DBCore.dll!TADOCommand::TADOCommand(TADOConnection * connection) Line 8 C++
    AmnDbase.dll!TQuery::Init() Line 335    C++
    AmnDbase.dll!TQuery::TQuery(const TDatabase * db, TTable * Tbl) Line 304    C++
    AmnDbase.dll!TDirectQuery::TDirectQuery(const TDatabase * db) Line 932  C++
    AmnDbase.dll!CommitUnsavedTransactions(TDatabase * db) Line 674 C++
    AmnDbase.dll!TDatabase::Disconnect() Line 427   C++
    AmnDbase.dll!TDatabase::CloseDb() Line 455  C++
    AmnDbase.dll!TDatabase::Close() Line 417    C++
    AmnLib2.dll!TStoreFile::Close() Line 3275   C++
    AmnLib2.dll!TStoreFile::~TStoreFile() Line 1040 C++
    AmnLib2.dll!`GetStoreFile'::`2'::`dynamic atexit destructor for 'StoreFile''()  C++
    AmnLib2.dll!_CRT_INIT(void * hDllHandle, unsigned long dwReason, void * lpreserved) Line 415    C
    AmnLib2.dll!__DllMainCRTStartup(void * hDllHandle, unsigned long dwReason, void * lpreserved) Line 526  C
    AmnLib2.dll!_DllMainCRTStartup(void * hDllHandle, unsigned long dwReason, void * lpreserved) Line 476   C

这是调用CreateInstance

的代码
/// Init the command instance.
void TADOCommand::Init( TADOConnection* connection)
{
    m_CmdText = "";
    m_nRecordsAffected = 0;
    m_Command = NULL;
    m_ResultSet = NULL;
    m_CmdConnection = NULL;
    m_CmdRecordset = NULL;

    if( connection->IsNull())
        return;
    m_Command.CreateInstance( __uuidof( Command));
    SetConnection( connection);
    SetCommandTimeOut( connection->GetCommandTimeout());
}   

我希望代码能够在不更改任何代码的情况下继续工作,特别是在数据库层中这是由于MFC的初始化调用?我该如何检测或解决这个问题?

并且m_Command被定义为

_CommandPtr m_Command;

并且它是com接口首先将它设置为null然后调用 CreateInstance 它每次都正常工作,除非上次失败并出现此异常

1 个答案:

答案 0 :(得分:0)

在我卸载或加载DLL时要执行的代码接缝,就我在callstack中看到的那样。

当这是“加载代码”时,问题就出现了:为什么再次加载LDL。

但是在DllMain中使用COM并不是一件好事。见here 绝不能在DllMain代码中使用COM。

我想在MFC已经调用CoUninitialize之后卸载了你的DLL。或者您的DLL由某些代码重新加载,不应该在关闭应用程序时执行。