从 C# 程序启动的 MFC dll(封装在 C++/CLI 中)中的访问冲突

发布于 2024-11-23 16:12:09 字数 2451 浏览 1 评论 0原文

我已经为 mfc dll (C++) 编写了一个托管 C++/CLI 包装器,并且在第二次调用 dll 后出现了一些访问冲突!

ReaderCommunication 和 CSmartMaskCmds 构造中的包装器

// in .h
typedef CKeyManagerServerApp* (*KeyManagerInstance)(CCommonUtils *);

ManagedKeyInterface::ManagedKeyInterface()
{
    HINSTANCE m_keyManagerLib = LoadLibrary("pathToDll");

    KeyManagerInstance _createInstance = (KeyManagerInstance)GetProcAddress(m_keyManagerLib, "GetInstance");

    // get native reader interface from managed reader interface
    CCommonUtils *nativeReaderInterface = static_cast<CCommonUtils*>(readerInterface->nativeReaderInterface.ToPointer());

    CKeyManagerServerApp *m_keyManagerApp = (_createInstance)(nativeReaderInterface );
}

ManagedKeyInterface::~ManagedKeyInterface()
{
    try
{
    DestroyKeyManagerInstance _destroyInstance = (DestroyKeyManagerInstance)GetProcAddress(m_keyManagerLib, "DestroyInstance");
    (_destroyInstance)(m_keyManagerApp);

    FreeLibrary(m_keyManagerLib);           
}
    catch(System::Exception ^e)
    {
        FreeLibrary(m_keyManagerLib);
    }
}

本机 MFC 类

extern "C" _declspec(dllexport) CKeyManagerServerApp* GetInstance(CCommonUtils *readerInterface)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    return new CKeyManagerServerApp(readerInterface);
}

extern "C" _declspec(dllexport) void DestroyInstance(CKeyManagerServerApp *ptr)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    delete ptr;
}

// constructor
CKeyManagerServerApp::CKeyManagerServerApp(CCommonUtils *readerInterface)   
{
    m_log = new Logging(loggingFilePath); // <--- ERROR at second call


    // reader interface object for communication 
    m_readerComm = new ReaderCommunication(readerInterface, m_log); 

    m_smartmaskcmds = new CSmartMaskCmds(m_readerComm, m_log);

    readerInterface = NULL;
}

// destructor
CKeyManagerServerApp::~CKeyManagerServerApp()
{
    // destruct objects     
    delete m_smartmaskcmds; 
    delete m_readerComm;    
    delete m_log;   
}

。该对象只会被分配!

在 C# 程序的第一次运行时(加载了带有添加引用的包装器)一切正常,但是当我再次启动它时,我得到:

TestKeyManagerApp.exe 中 0x76f85b57 处的第一次机会异常:0xC0000005:访问冲突读取位置 0xdddddddd。 TestKeyManagerApp.exe 中 0x75169617 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x0024e820 处的 CMemoryException..

当我调用 m_log = new Logging(loggingFilePath) 时,

析构函数似乎无法正常工作!?

有什么想法吗????

谢谢你!

I have written an managed C++/CLI wrapper for mfc dll (C++) and have some access violations after second call of dll!

Wrapper

// in .h
typedef CKeyManagerServerApp* (*KeyManagerInstance)(CCommonUtils *);

ManagedKeyInterface::ManagedKeyInterface()
{
    HINSTANCE m_keyManagerLib = LoadLibrary("pathToDll");

    KeyManagerInstance _createInstance = (KeyManagerInstance)GetProcAddress(m_keyManagerLib, "GetInstance");

    // get native reader interface from managed reader interface
    CCommonUtils *nativeReaderInterface = static_cast<CCommonUtils*>(readerInterface->nativeReaderInterface.ToPointer());

    CKeyManagerServerApp *m_keyManagerApp = (_createInstance)(nativeReaderInterface );
}

ManagedKeyInterface::~ManagedKeyInterface()
{
    try
{
    DestroyKeyManagerInstance _destroyInstance = (DestroyKeyManagerInstance)GetProcAddress(m_keyManagerLib, "DestroyInstance");
    (_destroyInstance)(m_keyManagerApp);

    FreeLibrary(m_keyManagerLib);           
}
    catch(System::Exception ^e)
    {
        FreeLibrary(m_keyManagerLib);
    }
}

NATIVE MFC CLASS

extern "C" _declspec(dllexport) CKeyManagerServerApp* GetInstance(CCommonUtils *readerInterface)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    return new CKeyManagerServerApp(readerInterface);
}

extern "C" _declspec(dllexport) void DestroyInstance(CKeyManagerServerApp *ptr)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    delete ptr;
}

// constructor
CKeyManagerServerApp::CKeyManagerServerApp(CCommonUtils *readerInterface)   
{
    m_log = new Logging(loggingFilePath); // <--- ERROR at second call


    // reader interface object for communication 
    m_readerComm = new ReaderCommunication(readerInterface, m_log); 

    m_smartmaskcmds = new CSmartMaskCmds(m_readerComm, m_log);

    readerInterface = NULL;
}

// destructor
CKeyManagerServerApp::~CKeyManagerServerApp()
{
    // destruct objects     
    delete m_smartmaskcmds; 
    delete m_readerComm;    
    delete m_log;   
}

in ReaderCommunication and CSmartMaskCmds constr. the object will only assigned!

At first runtime of the C# program (loaded the wrapper with add reference) everything works fine, but when I start it again I get:

First-chance exception at 0x76f85b57 in TestKeyManagerApp.exe: 0xC0000005: Access violation reading location 0xdddddddd.
First-chance exception at 0x75169617 in TestKeyManagerApp.exe: Microsoft C++ exception: CMemoryException at memory location 0x0024e820..

when I call m_log = new Logging(loggingFilePath)

It seems the destructor does not work right!?

Any ideas!!??

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上的星空 2024-11-30 16:12:09

当您看到值 0xdddddddd 时,这意味着 某些指针被删除(VC 将在调试版本上设置该值以帮助您识别这些情况)。您没有告诉我们什么是 loggingFilePath 以及 Logging 是如何实现的,但我猜测 loggingFilePath 在某个时候被删除,并且 >Logging 尝试访问其值或构造函数(或初始化列表)中的虚拟函数。

这也可以解释析构函数中的崩溃 - 您正在删除 m_log,它可能包含从 loggingFilePath 获得的非法指针。当您尝试再次使用它时,您会遇到同样的崩溃。

When you see the value 0xdddddddd, it means that some pointer was deleted (VC will set that value on debug builds to help you recognize these cases). You don't tell us what's loggingFilePath and how Logging is implemented, but my guess is that loggingFilePath is deleted at some point, and Logging tries to access its value or a virtual function in the constructor (or initialization list).

This could also explain the crash in the destructor - you're deleting m_log, which probably holds an illegal pointer it got from loggingFilePath. When you try to use it again, you get the same crash.

与君绝 2024-11-30 16:12:09

当我调用 m_log = new Logging(loggingFilePath)

幕后发生了什么?找出它到底崩溃的地方。如果您使用 C#,请启用非托管调试。我猜问题出在 Logging 构造函数下。

when I call m_log = new Logging(loggingFilePath)

What's happening behind the scenes? Find out where exactly it crashes. Enable unmanaged debugging if you are using C#. I guess the problem is under Logging constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文