unsigned char型**相当于C#和必须写在文件中的返回值 [英] unsigned char ** equivalent in c# and have to write the return value in a file

查看:494
本文介绍了unsigned char型**相当于C#和必须写在文件中的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用一个Win32 DLL函数

  INT FUNC1(INT ARG1,unsigned char型** ARG2,为int * ARG3 ); 

和我需要裹在C#作为

 公开的extern INT fuc1(INT ARG1,出的IntPtr ARG2,出的IntPtr ARG3); 

和我把它叫做从C#应用程序

  INT ARG1; 
IntPtr的ARG2 = IntPtr.Zero;
IntPtr的参数3 = IntPtr.Zero;
func1的(ARG1,ARG2出来,出来ARG3);



在C#包装声明,以及呼吁在C#中的测试程序正确的功能?
现在我需要的ARG2存储在一个文本文件中。如何做到这一点。



这是汉斯得到答案,我用



写在文件

  System.IO.StreamWriter(@ Application.StartupPath +\\Filename.txt); 
file.WriteLine(ARG2);
file.Close();


解决方案

我有一个免费的在DLL函数清除内存




然后,你必须在做这项工作的一个镜头。该函数声明应该是这样的:

 函数[DllImport(foo.dll,CallingConvention = CallingConvention.Cdecl)
私人静态外部INT FUNC1(INT ARG1,出的IntPtr ARG2,楼盘INT ARG3);

和你这样称呼它:

  IntPtr的PTR = IntPtr.Zero; 
INT说不上= 99;
字符串结果= NULL;

INT RETVAL = func1的(42,出PTR,参考说不上来);
如果(RETVAL ==成功){
结果= Marshal.PtrToStringAnsi(PTR);
//等等......
}
如果(PTR = IntPtr.Zero!)func1free(PTR);



在哪里func1free是释放字符串,否则未公开的函数。


I have to call a win32 dll function

int func1( int arg1, unsigned char **arg2, int *arg3);

and i need wrapped in c# as

public extern int fuc1(int arg1, out IntPtr arg2, out IntPtr arg3);

and i called it from a c# application as

int arg1;
IntPtr arg2 = IntPtr.Zero;
IntPtr arg3 = IntPtr.Zero;
func1(arg1,out arg2,out arg3);

Is the function declared in the c# wrapper as well as called in C# test app Correct ? Now i need to store the arg2 in a text file. How to do that.

Got answer from Hans and i wrote it in a file using

System.IO.StreamWriter(@Application.StartupPath + "\\Filename.txt");
file.WriteLine(arg2);
file.Close();

解决方案

i have a free function in the dll to clear the memory

Then you have a shot at making this work. The function declaration ought to look like this:

[DllImport("foo.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int func1(int arg1, out IntPtr arg2, ref int arg3);

And you'd call it like this:

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);

Where "func1free" is the otherwise undocumented function that releases the string.

这篇关于unsigned char型**相当于C#和必须写在文件中的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆