[問題] 如何傳遞 __arglist 給其他 function

看板C_Sharp (C#)作者 (麵)時間7年前 (2017/10/15 13:37), 7年前編輯推噓3(307)
留言10則, 3人參與, 7年前最新討論串1/1
internal void foo (string str, __arglist) { xxx } public void foo2(string str, __arglist) { // 請問這邊要如何將其他程式傳進來 // foo2 的 arglist 再傳入 foo ?? foo(str,arglist?); } public void Main() { int a,b,c; foo2("ABC",__arglist(out a,out b,out c)); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.237.120.118 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1508045868.A.D58.html

10/15 14:26, 7年前 , 1F
很少看到用到__arglist 這有文件嗎?
10/15 14:26, 1F
沒有找到相關的文件, // 專案 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

10/15 23:45, 7年前 , 2F
BadImageFormat, 指定 Encoding, CallingConvention 看看
10/15 23:45, 2F

10/15 23:51, 7年前 , 3F
https://git.io/vdMts 這樣呼叫應該不會有問題
10/15 23:51, 3F

10/15 23:52, 7年前 , 4F
你原本的需求可以參考 MSDN 上 ArgIterator 的範例
10/15 23:52, 4F

10/16 07:10, 7年前 , 5F
寫在同一個專案會過,但寫在另一個專案來參考會失敗
10/16 07:10, 5F

10/16 08:11, 7年前 , 6F
你有檢查 32/64 位元的設定嗎?
10/16 08:11, 6F

10/16 08:18, 7年前 , 7F
改 32 一樣失敗
10/16 08:18, 7F

10/18 20:58, 7年前 , 8F
我測試跨組件不會跳例外, 但 return value 會變 0
10/18 20:58, 8F

10/18 20:59, 7年前 , 9F
如果不用 __arglist, 用固定的參數不會有這種情形
10/18 20:59, 9F

10/18 20:59, 7年前 , 10F
另外, printf 也會有一樣的情形
10/18 20:59, 10F
文章代碼(AID): #1PulGirO (C_Sharp)
文章代碼(AID): #1PulGirO (C_Sharp)