【发布时间】:2009-12-16 20:03:20
【问题描述】:
我有一个在 VS 6.0 中开发的非托管 C++ MFC dll。我想在我的 C# 应用程序中使用它。我正在尝试使用 PInvoke。
这是 C++ 代码:
// testDll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
extern "C" {
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
__declspec(dllexport) int test(int a)
{
return a * a;
}
}
这就是我尝试从 C# 调用“test”方法的方式:
// PInvokeTest.cs
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest
{
[DllImport("TestDll.dll")]
internal static extern int test(int number);
public static void Main()
{
Console.WriteLine(test(5));
}
}
当我将 C++ dll 设置为普通的 Win32 dll 时,这种方法效果很好。
但是,一旦我将项目类型更改为 MFC(“在共享 DLL 中使用 MFC”),我就会收到此错误:
未处理的异常:System.DllNotFoundException:无法加载 DLL“TestDll.dll”:找不到指定的模块。 (来自 HRESULT 的异常:0x8007007E) 在 PlatformInvokeTest.test(Int32 number)
谢谢!
【问题讨论】: