作者noodleT (面)
看板C_Sharp
标题[问题] 如何传递 __arglist 给其他 function
时间Sun Oct 15 13:37:46 2017
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://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1508045868.A.D58.html
1F:推 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
2F:推 yeo1987: BadImageFormat, 指定 Encoding, CallingConvention 看看 10/15 23:45
4F:→ yeo1987: 你原本的需求可以参考 MSDN 上 ArgIterator 的范例 10/15 23:52
5F:→ noodleT: 写在同一个专案会过,但写在另一个专案来参考会失败 10/16 07:10
6F:→ yeo1987: 你有检查 32/64 位元的设定吗? 10/16 08:11
7F:→ noodleT: 改 32 一样失败 10/16 08:18
8F:推 yeo1987: 我测试跨组件不会跳例外, 但 return value 会变 0 10/18 20:58
9F:→ yeo1987: 如果不用 __arglist, 用固定的参数不会有这种情形 10/18 20:59
10F:→ yeo1987: 另外, printf 也会有一样的情形 10/18 20:59