简体   繁体   English

如何在c#项目中使用c++ dll

[英]How to use a c++ dll in c# project

I have a C ++ source and I want to use it in my C# project.我有一个 C++ 源代码,我想在我的 C# 项目中使用它。 I created a DLL file from it.我从它创建了一个 DLL 文件。 C++ source has dozens of .h and .cpp files, but I only need 4 methods. C++ 源代码有几十个.h.cpp文件,但我只需要 4 个方法。 So I defined my methods this way.所以我以这种方式定义了我的方法。

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[]);

When we disassemble the Dll file we can see the methods.当我们反汇编 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

In the C # project, we called the methods this way.在 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);

And I used the method like this.我使用了这样的方法。

short voc_rate = 2400;
voc_init_decode(voc_rate);

But I faced this error.但是我遇到了这个错误。

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

Thank you if you guide me.谢谢你指导我。 I do not know where I went wrong.我不知道我哪里出错了。

The most likely problem is trying to call a 32-bit DLL from a 64-bit program (the opposite doesn't work either).最可能的问题是试图从 64 位程序调用 32 位 DLL(相反的方法也不起作用)。

Other possibilities are that you got the DLL path wrong, or the DLL depends on some other DLL that is missing on your system.其他可能性是您的 DLL 路径错误,或者 DLL 依赖于您的系统上缺少的某个其他 DLL。

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

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