创建非托管常规 MFC DLL 并从托管 C++ .NET 应用调用它时出现问题
Problems creating a unmanaged regular MFC DLL and call it from an managed C++ .NET app
我有几个关于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 并且应用程序崩溃的情况下工作(错误:尝试读取或写入受保护的内存)?
2 - 在这种情况下,InitInstance() 是否执行,尽管我不调用它(为什么没有 ExitInstance)?
3 - 为什么在使用 DLLImport 时,我会看到一些示例给出入口点,而有些则没有?
4 - 是否可以将委托作为参数提供给 MFC C++ DLL 中的函数,而不是普通函数指针,以创建回调?
方法不能被 P/调用。如果要从非托管 DLL 导出类以在托管世界中使用,则必须将其平展,例如。
-
创建一个构造函数,如下所示:
__declspec(dllexport) void * __stdcall MyClass_Create() { return new MyClass(); }
-
创建一个析构函数,如下所示:
__declspec(dllexport) void * __stdcall MyClass_Destroy(MyClass * instance) { delete instance; }
-
平展方法调用。假设您的类中有以下方法:
int MyClass::MyMethod(int i, double j) { ... }
然后,您必须创建以下函数:
__declspec(dllexport) int __stdcall MyClass_MyMethod(MyClass * instance, int i, double j) { return instance->MyMethod(i, j); }
-
在 C# 中准备 P/调用的外部方法(您已经知道如何操作,所以我将省略这些)
-
创建类的实例:
IntPtr instance = MyClass_Create();
然后调用其方法:
int i = MyClass_MyMethod(instance, 4, 2.0);
最后,销毁类:
MyClass_Destroy(instance);
不要忘记添加一些错误检查 - 我省略了它以保持示例清晰。
相关文章:
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?
- 调用"curl_easy_send"和"curl_easy_recv"时应用哪些
- C# DLL 从C++应用程序调用 SOAP Web 服务
- 如何在 Win32 C++控制台应用程序中调用 UWP 类库
- 什么是仅调用一次并调用参数的控制台应用
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 如果在 DLL 和调用应用程序中使用 GPGPU API,会发生什么情况
- 在 Windows 启动时调用应用程序时无法正常启动
- Intel Pin调用应用程序中的函数
- 我可以在OpenFrameWorks中调用应用程序以外的其他内容
- 调用应用程序libjingle,用户自动从花名册中删除.为什么?
- C#使用命令行调用应用程序
- 如何在C++中从DLL到调用应用程序进行通信
- Clr Dll是由测试程序加载的,但不是由调用应用程序加载的
- 从 PHP 调用 C++ 应用程序 - 输出显示"Array"
- 以系统用户 (Windows) 身份调用应用程序
- C++可变参数模板函数调用应用了什么模式匹配(如果有)
- Sudo Java调用C应用程序和权限
- 如何编写不会立即将控制权释放给调用应用程序的 Windows 应用程序