【问题标题】:How to use a c++ dll in c# project如何在 c# 项目中使用 c++ dll
【发布时间】:2021-12-22 04:48:37
【问题描述】:

我有一个 C++ 源代码,我想在我的 C# 项目中使用它。我从中创建了一个 DLL 文件。 C++源码有几十个.h.cpp文件,但我只需要4个方法。所以我以这种方式定义了我的方法。

void _SC1200_H_ voc_init_decode(short vocrate);
void _SC1200_H_ voc_init_encode(short vocrate);
void _SC1200_H_ voc_encode(Shortword sp_in[], unsigned char out[], short npp_flag);
void _SC1200_H_ voc_docode(unsigned char input[], Shortword sp_out[]);

当我们反汇编 Dll 文件时,我们可以看到方法。

Dump of file d:\Debug\Melpe.dll

File Type: DLL

  Section contains the following exports for Melpe.dll

    00000000 characteristics
    618A1F5A time date stamp Mon Nov  8 23:12:26 2021
        0.00 version
           1 ordinal base
           4 number of functions
           4 number of names

    ordinal hint RVA      name

          1    0 0001C28F voc_docode = @ILT+650(_voc_docode)
          2    1 0001C28A voc_encode = @ILT+645(_voc_encode)
          3    2 0001C1F9 voc_init_decode = @ILT+500(_voc_init_decode)
          4    3 0001C1FE voc_init_encode = @ILT+505(_voc_init_encode)

  Summary

       1C000 .data
        1000 .idata
        A000 .rdata
        2000 .reloc
        1000 .rsrc
       36000 .text
       1B000 .textbss

在 C# 项目中,我们以这种方式调用方法。

[DllImport("D:\\Debug\\Melpe.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void voc_init_decode(short vocrate);
[DllImport("D:\\Debug\\Melpe.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void voc_init_encode(short vocrate);
[DllImport("D:\\Debug\\Melpe.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void voc_encode(byte[] sp_in, byte[] output, bool npp_flag);
[DllImport("D:\\Debug\\Melpe.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void voc_docode(byte[] input, byte[] sp_out);

而我使用的方法是这样的。

short voc_rate = 2400;
voc_init_decode(voc_rate);

但我遇到了这个错误。

System.DllNotFoundException: 'Unable to load DLL 'D:\Debug\Melpe.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'

谢谢你指导我。我不知道我哪里出错了。

【问题讨论】:

  • 这能回答你的问题吗? How to call C++ DLL in C#
  • 什么是 C++ dll?我知道 C 的。 C++ .dll 表示 COM 或 MFC,但你不知道这些
  • 来自您发布的二进制转储,但不是您编写的源代码

标签: c# c++ dll


【解决方案1】:

最可能的问题是尝试从 64 位程序调用 32 位 DLL(相反的也不起作用)。

其他可能是您的 DLL 路径错误,或者 DLL 依赖于您的系统上缺少的其他 DLL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多