呼叫非受控 DLL 時拋出:無法載入 DLL "###.dll",找不到指定的模組 (Exception from HRESULT: 0x8007007E)。
using System.Runtime.InteropServices;
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, string funcname);
C++ 函數需符合:
extern 宣告中對應。C++ 改用 char** 或 LPCTSTR,C# 用 ref string 或 StringBuilder。
例:C++ int Set(char** str, int n) → C# int Set(ref string str, int n)。
不能直接呼叫類別成員。建議在 C++ 另寫全域封裝函數:
int DllGetCh(int a) {
Solution st = new Solution(); // 實例化類別
return st.getch(a);
}
再對 DllGetCh 做 DllImport。
VC++ BOOL → C# System.Int32(或 bool 需注意大小),建議統一用 int。
bin/Debug/ 或 bin/Release/C:/Windows/System32若路徑正確仍失敗,90% 以上是該 DLL 依賴其他 DLL 但未放置在可尋找路徑。需檢查依賴項(用 Dependency Walker 等工具)並確保所有相依 DLL 皆在搜尋路徑中。