[英]How to use a c++ dll in c# project
我有一個 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)'
謝謝你指導我。 我不知道我哪里出錯了。
最可能的問題是試圖從 64 位程序調用 32 位 DLL(相反的方法也不起作用)。
其他可能性是您的 DLL 路徑錯誤,或者 DLL 依賴於您的系統上缺少的某個其他 DLL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.