问题创造了unmanged常规MFC DLL和从托管C ++ .NET应用程序调用它 [英] Problems creating a unmanged regular MFC DLL and call it from an managed C++ .NET app

查看:196
本文介绍了问题创造了unmanged常规MFC DLL和从托管C ++ .NET应用程序调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对DLL的几个问题。我尝试了很多,但我不能获得完整的信息。大多数的例子是在C#等。

I have a few questions about DLL's. I tried a lot but I can not get the complete picture. Most examples are in C# etc.

使用向导在VS2005我创建了一个非托管的MFC规则DLL(一定是因为剩余的code MFC)。然后我试图导入在托管的.NET C ++应用程序VS2005。见下面code。

With the wizard in VS2005 I created a unmanaged MFC regular DLL (must be MFC because of remaining code). Then I tried to import it in a VS2005 managed .NET C++ application. See code below.

mfc_main.h:

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:

//----------------------------------------------------------------
// 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);
     }

我的问题是:

My questions are:

1 - 为什么不工作在init = 12在功能的SayHello和应用程序崩溃(错误:试图读取或写入受保护的内存)

1 - Why is it working without the init = 12 in the function SayHello and with the application crashes (error: Attempted to read or write protected memory)?

2 - 在这种情况下的InitInstance()执行的,虽然我不把它(和为什么没有ExitInstance中)

2 - Is in this case the InitInstance() executed although I don't call it (and why is there no ExitInstance)?

3 - ?为什么我看一些例子使用的DllImport时给予入口点和一些不

3 - Why do I see some examples giving the EntryPoint when using DLLImport and some don't?

4 - ?我可以给一个委托作为参数,在MFC C ++ DLL,而不是一个正常的函数指针的函数,来创建一个回调

4 - Can I give a delegate as parameter to a function in a MFC C++ DLL instead of a normal function pointer, to create a callback?

推荐答案

方法不能被P /调用。如果你要导出的非托管的DLL一类管理的世界中使用,你必须将其压平,如:

Methods cannot be P/Invoked. If you want to export a class from unmanaged DLL to be used in managed world, you have to flatten it, eg.

  • 创建一个构造函数,它看起来像:

  • Create a constructor function, which looks like:

__declspec(dllexport) void * __stdcall MyClass_Create()
{
     return new MyClass();
}

  • 创建一个析构函数,它是这样的:

  • Create a destructor function, which looks like:

    __declspec(dllexport) void * __stdcall MyClass_Destroy(MyClass * instance)
    {
         delete instance;
    }
    

  • 拼合方法调用。让我们假设,你有下面的方法在您的类:

  • Flatten method calls. Let's suppose, that you have the following method in your class:

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

    然后,你必须创建如下功能:

    Then you have to create a following function:

    __declspec(dllexport) int __stdcall MyClass_MyMethod(MyClass * instance, int i, double j)
    {
        return instance->MyMethod(i, j);
    }
    

  • prepare P / C#中调用外部方法(你已经知道如何做到这一点,所以我会忽略这些)

  • Prepare P/Invoked external methods in C# (You already know how to do it, so I'll omit these)

    创建类的实例:

    IntPtr instance = MyClass_Create();
    

    然后调用它的方法:

    Then call its method:

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

    最后,破坏类:

    Finally, destroy the class:

    MyClass_Destroy(instance);
    

  • 不要忘记添加一些错误检查 - 我忽略它使例子清楚

    Don't forget to add some error checking - I omitted it to keep the example clear.

    这篇关于问题创造了unmanged常规MFC DLL和从托管C ++ .NET应用程序调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆