-->

无符号字符**等同于C#和必须写在文件中的返回值(unsigned char ** equivale

2019-07-17 12:00发布

我打电话给一个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();

Answer 1:

我有一个免费的功能在DLL清理内存

然后,你必须在使这项工作了一枪。 该函数声明应该是这样的:

[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”是释放字符串否则无证功能。



Answer 2:

您可能需要使用MarshalAs属性,例如:

public static extern int func1(int arg1, [MarshalAs(UnmanagedType.LPStr)] string arg2, IntPtr arg3);

检查这里的文件:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx



文章来源: unsigned char ** equivalent in c# and have to write the return value in a file