c# - unsigned char ** 相当于 c# 中的值,并且必须将返回值写入文件中

标签 c# wrapper intptr unsigned-char

我必须调用 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/

相关文章:

c++ - 在 C# 的 WPF 应用程序中单击按钮时如何调用 C++ 代码?

c# - 获取IntPtr指向的数组的大小

linux - 为任意应用程序编写 I/O 包装器 (linux)

C#.NET IntPtr 无效类型

c# - 使用 SharpDX 显示 D3DImage

c# - 如何使用客户端证书在 Web API 中进行身份验证和授权

c# - 将 XML 片段反序列化为 C# 类时无法获取某些元素和属性的值

c# - 生成低位深度的图像文件?

javascript - 将 js 翻译为 C#

C++ 类包装器和放置 new