简体   繁体   中英

Problems creating a unmanaged regular MFC DLL and call it from an managed C++ .NET app

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.

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 : 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;
};

in application

[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 - 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 - Is in this case the InitInstance() executed although I don't call it (and why is there no ExitInstance)?

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

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?

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/Invoked external methods in C# (You already know how to do it, so I'll omit these)

  • Create instance of your class:

     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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM