c++ - 创建非托管常规 MFC DLL 并从托管 C++ .NET 应用程序调用它时出现问题

标签 c++ .net dll mfc

我有几个关于 DLL 的问题。我尝试了很多,但无法获得完整的图片。大多数示例都在 C# 等中。

使用 VS2005 中的向导,我创建了一个非托管 MFC 常规 DLL(由于剩余代码,必须是 MFC)。然后我尝试将它导入到 VS2005 托管的 .NET C++ 应用程序中。请参阅下面的代码。

mfc_main.h:

//---------------------------------------------------------
// mfc_main.h : main header file for the mfc_main DLL
//---------------------------------------------------------

#pragma once

#ifndef __AFXWIN_H__
    #error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"       // main symbols

class __declspec(dllexport) Cmfc_mainApp : public CWinApp
{
public:
    Cmfc_mainApp();

// Overrides
public:
    virtual BOOL InitInstance();

    int SayHello(int j);

    int init;
    DECLARE_MESSAGE_MAP()
};

mfc_main.cpp:

//----------------------------------------------------------------
// mfc_main.cpp : Defines the initialization routines for the DLL.
//----------------------------------------------------------------

#include "stdafx.h"
#include "mfc_main.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

BEGIN_MESSAGE_MAP(Cmfc_mainApp, CWinApp)
END_MESSAGE_MAP()

Cmfc_mainApp::Cmfc_mainApp()
{
}

Cmfc_mainApp theApp;

BOOL Cmfc_mainApp::InitInstance()
{
    CWinApp::InitInstance();

    return TRUE;
}

int Cmfc_mainApp::SayHello(int j)
{
    init = 12;  // Comment this out the application works !!!!

    return j * 6;
};

应用中

[DllImport("mfc_main.dll",
      EntryPoint    = "?SayHello@Cmfc_mainApp@@QAEHH@Z",
      ExactSpelling = true)]
static int SayHello(int a);

......

private: System::Void button_Click(System::Object^ sender, System::EventArgs^ e) 
     {
         int retval = SayHello(2);
     }

我的问题是:

1 - 为什么它在 SayHello 函数中没有 init = 12 的情况下工作并且应用程序崩溃(错误:尝试读取或写入 protected 内存)?

2 - 在这种情况下是否执行了 InitInstance(),尽管我没有调用它(为什么没有 ExitInstance)?

3 - 为什么我看到一些示例在使用 DLLImport 时提供了 EntryPoint 而有些则没有?

4 - 我可以将委托(delegate)作为参数提供给 MFC C++ DLL 中的函数而不是普通函数指针,以创建回调吗?

最佳答案

方法不能被 P/调用。如果您想从非托管 DLL 导出一个类以在托管世界中使用,则必须将其展平,例如。

  • 创建一个构造函数,如下所示:

    __declspec(dllexport) void * __stdcall MyClass_Create()
    {
         return new MyClass();
    }
    
  • 创建一个析构函数,如下所示:

    __declspec(dllexport) void * __stdcall MyClass_Destroy(MyClass * instance)
    {
         delete instance;
    }
    
  • 扁平化方法调用。假设您的类(class)中有以下方法:

    int MyClass::MyMethod(int i, double j) { ... }
    

    然后您必须创建以下函数:

    __declspec(dllexport) int __stdcall MyClass_MyMethod(MyClass * instance, int i, double j)
    {
        return instance->MyMethod(i, j);
    }
    
  • Preprepare P/Invoked external methods in C#(你已经知道怎么做了,所以我会省略这些)

  • 创建类的实例:

    IntPtr instance = MyClass_Create();
    

    然后调用它的方法:

    int i = MyClass_MyMethod(instance, 4, 2.0);
    

    最后,销毁类:

    MyClass_Destroy(instance);
    

不要忘记添加一些错误检查 - 我省略了它以使示例清晰。

关于c++ - 创建非托管常规 MFC DLL 并从托管 C++ .NET 应用程序调用它时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13664753/

相关文章:

c++ - Windows : How to protect process from getting killed in C/C++

.net - WPF 项目容器回收

c# - 使用事件 'source' 的嵌套对象指针是否正确?

c++ - Windows调用的DLL与主应用程序如何通信?

未找到 C++ graphics.h -lbgi

c++ - 将右值引用传递给 boost::in_place 函数

c++ - 推断模板参数对象成员的类型

c# - TaskScheduler 始终在同一个线程上运行

c# - 如何将包装器 DLL 连接到 API DLL 以便在 C# 项目中使用?

c++ - 查找句柄打开的位置