我必须调用 win32 dll 函数
int func1( int arg1, unsigned char **arg2, int *arg3);
我需要用 C# 包装为
public extern int fuc1(int arg1, out IntPtr arg2, out IntPtr arg3);
我从 C# 应用程序中将其称为
int arg1;
IntPtr arg2 = IntPtr.Zero;
IntPtr arg3 = IntPtr.Zero;
func1(arg1,out arg2,out arg3);
在 C# 包装器中声明的函数以及在 C# 测试应用程序中调用的函数是否正确? 现在我需要将 arg2 存储在文本文件中。如何做到这一点。
从汉斯那里得到答案,我使用
将其写入文件中System.IO.StreamWriter(@Application.StartupPath + "\\Filename.txt");
file.WriteLine(arg2);
file.Close();
最佳答案
i have a free function in the dll to clear the memory
然后你就有机会完成这项工作。函数声明应该如下所示:
[DllImport("foo.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int func1(int arg1, out IntPtr arg2, ref int arg3);
你可以这样调用它:
IntPtr ptr = IntPtr.Zero;
int dunno = 99;
string result = null;
int retval = func1(42, out ptr, ref dunno);
if (retval == success) {
result = Marshal.PtrToStringAnsi(ptr);
// etc...
}
if (ptr != IntPtr.Zero) func1free(ptr);
其中“func1free”是释放字符串的未记录的函数。
关于c# - unsigned char ** 相当于 c# 中的值,并且必须将返回值写入文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991060/