我有一个在VS 6.0中开发的非托管C++ MFC dll。我想在我的C#应用程序中使用它。我正在尝试使用PInvoke。从C#调用非托管C++ VS 6.0 MFC dll
这里是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#方法“测试”:
// 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(“使用MFC中共享DLL”)我得到这个错误:
未处理的异常:System.DllNotFoundException:无法加载DLL“TestDll.dll ': 指定的模块无法找到。 (异常来自HRESULT:0x8007007E) at PlatformInvokeTest.test(Int32号码)
谢谢!
它的工作原理!非常感谢!依赖沃克正是我所需要的。 TestDll.dll有一个相关的DLL - MSVCRTD.DLL。在将此dll复制到项目文件夹后,C#应用程序运行得很好。 – 2009-12-16 21:05:10
这意味着您对C++运行时具有依赖性。 MSVCRTD是运行时的调试版本。确保使用您的发行版本来重新分发非调试dll。 – 2009-12-17 20:38:28