积极答复者
C#可否调用CRT中的scanf函数?

问题
答案
-
问题已经由我自己解决,很简单:
[DllImport("msvcrt.dll")] public static extern int scanf(string format, __arglist); [DllImport("msvcrt.dll")] public static extern int printf(string format, __arglist);
注意C#中有那个几个不为人所知的关键字,也让我纳闷很久,__arglist是对应C语言中的va_list,使用的时候应该同样也使用__arglist,下面是我自己写的一个demo,注意&a和&b可以用ref a、ref b来表示:
using System.Runtime.InteropServices; namespace TestConsoleCS { class Program { [DllImport("msvcrt.dll")] public static extern int scanf(string format, __arglist); [DllImport("msvcrt.dll")] public static extern int printf(string format, __arglist); static void Main(string[] args) { int a = 0, b = 0; printf("Please enter a and b:\n", __arglist()); scanf("%d%d", __arglist(ref a, ref b)); printf("a + b = %d\n", __arglist(a + b)); } } }
感觉又回到了C语言的时代……- 已标记为答案 Flysha 2011年6月8日 4:11
全部回复
-
问题已经由我自己解决,很简单:
[DllImport("msvcrt.dll")] public static extern int scanf(string format, __arglist); [DllImport("msvcrt.dll")] public static extern int printf(string format, __arglist);
注意C#中有那个几个不为人所知的关键字,也让我纳闷很久,__arglist是对应C语言中的va_list,使用的时候应该同样也使用__arglist,下面是我自己写的一个demo,注意&a和&b可以用ref a、ref b来表示:
using System.Runtime.InteropServices; namespace TestConsoleCS { class Program { [DllImport("msvcrt.dll")] public static extern int scanf(string format, __arglist); [DllImport("msvcrt.dll")] public static extern int printf(string format, __arglist); static void Main(string[] args) { int a = 0, b = 0; printf("Please enter a and b:\n", __arglist()); scanf("%d%d", __arglist(ref a, ref b)); printf("a + b = %d\n", __arglist(a + b)); } } }
感觉又回到了C语言的时代……- 已标记为答案 Flysha 2011年6月8日 4:11