简体   繁体   English

C# 导入 C++ dll

[英]C# importing C++ dll

I have a managed dll file which imports functions from a C++ dll to the managed environment.我有一个托管 dll 文件,它将函数从 C++ dll 导入托管环境。 I'm using some of its functions in my program but the problem is, I get this error when I use it:我在我的程序中使用了它的一些功能,但问题是,当我使用它时出现这个错误:

Unable to load DLL 'Libraries\lib.dll': The specified module could not be found.无法加载 DLL 'Libraries\lib.dll':找不到指定的模块。 (Exception from HRESULT: 0x8007007E) (来自 HRESULT 的异常:0x8007007E)

I placed the .dll file in the program's directory and in the system32 folder.我将 .dll 文件放在程序目录和 system32 文件夹中。 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.我想我必须使用 DLLImport 但我不知道如何使用它.. 即使看了一些例子我仍然感到困惑。 Can someone help me here?有人可以在这里帮助我吗?

You say:你说:

I placed the .dll file in the program's directory...我将 .dll 文件放在程序的目录中...

But:但:

Unable to load DLL 'Libraries\lib.dll'无法加载 DLL 'Libraries\lib.dll'

We need to see your DLLImport attribute creation, ie, the C# signature of the native method.我们需要查看您的DLLImport属性创建,即本机方法的 C# 签名。 It looks to me like you probably specify the path, ie,在我看来,您可能指定了路径,即

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

Try using this instead:尝试改用这个:

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

That will search the running directory as well as through your PATH environment variable.这将搜索运行目录以及您的PATH环境变量。 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 ).如果您像以前一样指定文件路径,老实说,如果找不到文件,我真的不知道它是否会通过PATH搜索(我在docs中找不到提及它)。

There isn't enough information here to help, as you're not showing the API (in native code) you're trying to import, etc.这里没有足够的信息来提供帮助,因为您没有显示您尝试导入的 API(在本机代码中)等。

That being said, I'd strongly recommend reading the Platform Invoke Tutorial as well as A Closer Look at Platform Invoke on MSDN.话虽如此,我强烈建议阅读Platform Invoke Tutorial以及A Closer Look at Platform Invoke on MSDN。 They walk through the main issues, as well as showing many examples of how to import and use functions from a C++ DLL.他们介绍了主要问题,并展示了许多如何从 C++ DLL 导入和使用函数的示例。

The best and easiest way of using a c++ dll file in c# :-在 c# 中使用 c++ dll 文件的最佳和最简单的方法:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace demo1
{
    class Program
    {
        [DllImport("shi.dll", EntryPoint = "?HelloWorld@@YAXXZ")]
       public static extern int HelloWorld();
      public  static void Main(string[] args)
        {
            //Console.WriteLine(StringUtilities.HelloWorld());
            Console.WriteLine(HelloWorld());
            // public static extern void HelloWorld();
           //  HelloWorld();
            //  Console.ReadKey();
        }
    }
}

我在使用不同的 .dll 文件时遇到了同样的问题,解决方案是将目标更改为 x64 而不是 x86

If you are sure of the exports (use dependancy walker to check) and that you have correctly mapped them using the correct PInvoke calls, then your issue might be 32/64 bit related especially if you are on a 64bit OS with a .NET application set to Any CPU.如果您确定导出(使用依赖遍历器检查)并且您已使用正确的 PInvoke 调用正确映射它们,那么您的问题可能与 32/64 位相关,特别是如果您在带有 .NET 应用程序的 64 位操作系统上设置为任何 CPU。

A 32 bit native DLL can only be loaded by a 32 bit .NET process when using PInvoke (the same applies to 64 bit native DLLs).使用 PInvoke 时,32 位本机 DLL 只能由 32 位 .NET 进程加载(这同样适用于 64 位本机 DLL)。

You can change the platform target using Properties->Build->Platform target or you can use the CorFlags utility.您可以使用 Properties->Build->Platform target 更改平台目标,也可以使用CorFlags实用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM