【发布时间】: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,但你不知道这些 -
来自您发布的二进制转储,但不是您编写的源代码