C#导入C ++ dll [英] C# importing C++ dll

查看:122
本文介绍了C#导入C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管的dll文件,该文件将功能从C ++ dll导入到托管的环境中。我在程序中使用了它的某些功能,但问题是,使用它时出现此错误:

I have a managed dll file which imports functions from a C++ dll to the managed environment. I'm using some of its functions in my program but the problem is, I get this error when I use it:

无法加载DLL'Libraries\lib。 dll':找不到指定的模块。 (来自HRESULT的异常:0x8007007E)

Unable to load DLL 'Libraries\lib.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

我将.dll文件放在程序的目录和system32文件夹中。但是,它仍然不起作用。我想我必须使用DLLImport,但我不知道如何使用它。即使看了一些示例,我仍然感到困惑。有人可以在这里帮我吗?

I placed the .dll file in the program's directory and in the system32 folder. However, it still doesn't work. I think I have to use DLLImport but I have no idea how to use it.. even after looking at some examples I am still confused. Can someone help me here?

推荐答案

您说:


我将.dll文件放在程序目录中...

I placed the .dll file in the program's directory...

但是:


无法加载DLL 'Libraries\lib.dll'

我们需要查看您的 DLLImport 属性的创建,即本机方法的C#签名。在我看来,您似乎已指定路径,即

We need to see your DLLImport attribute creation, i.e., the C# signature of the native method. It looks to me like you probably specify the path, i.e.,

[DLLImport( "Libraries\lib.dll" )];
static extern void MyNativeMethod();

尝试使用此代替:

[DLLImport( "lib.dll" )];
static extern void MyNativeMethod();

这将搜索运行目录以及通过您的 PATH 环境变量。如果您按原样指定文件路径,老实说,我不知道如果找不到该文件,它是否会通过 PATH 搜索(我找不到在其中提及该文件的路径) 文档)。

That will search the running directory as well as through your PATH environment variable. If you specify a file path as you do I honestly don't know if it will search through PATH if the file is not found (I couldn't find mention of it in the docs).

这篇关于C#导入C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆