【发布时间】:2012-09-10 05:56:33
【问题描述】:
我有如下 C++ DLL
#include "stdafx.h"
extern "C" __declspec(dllexport)double Add(double a, double b);
extern double Add(double a, double b)
{
return a + b;
}
n 这里我试图将此 DLL 与我的 C# 应用程序链接
using System.Text;
using System.Runtime.InteropServices;
namespace test
{
class Program
{
[DllImport("DLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double Add(double a, double b);
static void Main(string[] args)
{
Console.WriteLine(Add(1.0, 3.0)); // error here
Console.ReadLine();
}
}
}
我遇到错误:
“无法加载 DLL 'DLL.dll':找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)”
请帮帮我...如何将 c++ dll 与 c# 链接?
【问题讨论】:
-
只是为了避免混淆DLL被称为“DLL.dll”对吗?
-
还有,它是在构建的exe文件的路径中吗?
-
@james,您是否通过外部负载测试(例如依赖项步行器)确保不存在 .dll,但 it 所依赖的一切都同样存在? Windows 加载程序有一种可怕的趋势,即不仅在 LoadLibrary 无法加载 .dll 时报告该错误消息,而且如果您的 .dll 所依赖的 .dll 也无法找到,则同样的消息也是如此。你可能想检查一下。
-
你需要把你的DLL放在调试目录的原因是你没有提供DLL的路径。因此,默认情况下,它会在调试文件夹中查找它的存在。
-
应用程序必须在某处搜索dll,应用程序目录是一个地方,系统目录是另一个地方。但是,它不会开始在文件系统范围内搜索甚至可能根本不存在的东西。看这里:msdn.microsoft.com/en-us/library/windows/desktop/…