推 ian90911: 很少看到用到__arglist 這有文件嗎? 10/15 14:26
沒有找到相關的文件,
// 專案 A
// 目的要在專案 A 中引用 C/C++ 的標準函式,
// 之後的其他專案只要參考專案 A 就可以使用這些函式,
// 不須 [DllImport("")]
// 但如同 A-1 的寫法,在單元測試中總是會出現
// System.BadImageFormatException: 找不到索引
// 但若用 B-1 的寫法(在同專案中),就能夠正常工作
// 而 A-3 foo2 也能夠正常被其他專案呼叫,
// 所以才想說在 A-3 foo2 中再去呼叫 A-1
public static class ExDLL
{
// 程式 A-1
[DllImport("msvcrt40.dll")]
public static extern int sscanf(string str, string format, __arglist);
// 程式 A-2
[DllImport("msvcrt40.dll")]
public static extern int sscanf(string str, string format, out int arg);
// 程式 A-3
public static int foo2(string str, string format, __arglist)
{
}
}
==================================================================
// 專案 B (參考專案 A)
// 程式 B-1
[DllImport("msvcrt40.dll")]
public static extern int sscanf(string str, string format, __arglist);
public void Main()
{
// 分別使用 A-1, A-3, A-2, B-1
// A-1 會產生 System.BadImageFormatException: 找不到索引
// A-2, A-3, B-1 都可以正常工作
// A-1:呼叫其他專案中的 DllImport + __arglist (失敗)
// A-2:呼叫其他專案中的 DllImport (成功)
// A-3:呼叫其他專案中的 __arglist (成功)
// B-1:呼叫本地專案中的 DllImport + __arglist (成功)
}
※ 編輯: noodleT (36.237.120.118), 10/15/2017 16:34:54
推 yeo1987: BadImageFormat, 指定 Encoding, CallingConvention 看看 10/15 23:45
→ yeo1987: 你原本的需求可以參考 MSDN 上 ArgIterator 的範例 10/15 23:52
→ noodleT: 寫在同一個專案會過,但寫在另一個專案來參考會失敗 10/16 07:10
→ yeo1987: 你有檢查 32/64 位元的設定嗎? 10/16 08:11
→ noodleT: 改 32 一樣失敗 10/16 08:18
推 yeo1987: 我測試跨組件不會跳例外, 但 return value 會變 0 10/18 20:58
→ yeo1987: 如果不用 __arglist, 用固定的參數不會有這種情形 10/18 20:59
→ yeo1987: 另外, printf 也會有一樣的情形 10/18 20:59