[in,out]如何与dllimport一起工作? [英] How does [in, out] work with dllimport?

查看:114
本文介绍了[in,out]如何与dllimport一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例中的[In,Out]是什么?



[DllImport(DllName.dll)]

public static extern int MethodFromDll([In,Out] char [] session_file,[In,Out] char []短语,[In] char [] indexbuild,[In,Out] char [] result);



我尝试过:



在线搜索但找不到答案

What is [In,Out] exactly doing in the following example?

[DllImport("DllName.dll")]
public static extern int MethodFromDll([In, Out] char[] session_file, [In, Out] char[] phrase, [In] char[] indexbuild, [In, Out] char[] result);

What I have tried:

Searched online but could not find answer

推荐答案

这些属性用于告诉编译器调用期间需要哪些数据封送,从托管代码到非托管代码和/或返回(或跨COM调用)。 />
[In] - InAttribute类 [ ^ ]表示数据应该从调用者封送到被调用者。

[Out] - OutAttribute类 [ ^ ]表示应该将数据从被叫方封送回来电。

[ In,Out] 做你期望的事情;它表示数据应该在两个方向上编组。

参见上面链接中的备注
These attributes are used to tell the compiler what data marshaling is required across the call, from the Managed to Unmanaged code and/or back (or across COM calls).
[In] -- The InAttribute Class[^] Indicates that data should be marshaled from the caller to the callee.
[Out] -- The OutAttribute Class[^] Indicates that data should be marshaled from callee back to caller.
and [In, Out] does what you would expect; it indicates that data should be marshaled in both directions.
See the Remarks in the above links.


这些属性告诉操作系统,属性后面的参数是输入以及输出参数。在某些情况下,您只需要其中一个;在其他一些情况下,你需要两者。要确定您是否需要这些属性,您应该检查MSDN以查找您正在使用的特定功能。



例如,考虑

[ GetWindowPlacement ]功能,其中声明为:

These are attributes telling the operating system that the parameter following the attribute is an input as well as an output parameter. In some cases, you need only one of them; and in some other cases you need both. To decide whether you need either of these attributes, you should check MSDN for the particular function you are using.

For example, consider the
[GetWindowPlacement] function, which is declared as:
BOOL WINAPI GetWindowPlacement(
  _In_    HWND            hWnd,
  _Inout_ WINDOWPLACEMENT *lpwndpl
);





因此很容易看到 lpwndpl 参数需要在代码中声明这两个参数。



So it is easy to see that the lpwndpl parameter needs to be declared in your code with both of these parameters.


这篇关于[in,out]如何与dllimport一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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