如果有必要/适合使用InAttribute和OutAttribute为COM Interop [英] When is it necessary/appropriate to use InAttribute and OutAttribute for COM Interop

查看:153
本文介绍了如果有必要/适合使用InAttribute和OutAttribute为COM Interop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想经过我们已经分散在不同项目COM互操作定义的混乱和它们收集到一个已知良好的地理位置,整个开发团队可以受益。这一努力的一部分涉及清理已多年积累的定义。

I am trying to go through the mess of COM interop definitions we have scattered across various projects and collect them into a single, known-good location from which the whole dev team can benefit. Part of this effort involves cleaning up the definitions that have been accumulated over the years.

有些是从其他源代码中借来的,有些是从pinvoke.net逐字复制,有的看从SDK头直接转换。有一件事我注意到的是,有没有关于何时使用各种编组的属性,(即使在pinvoke.net的例子,这是非常击中或错过)的一致性。问题的部分原因是,我不认为任何人在这里(包括我自己)在需要时的各种属性或没有,或他们实际上做充分了解。到了这一点,让这些权似乎是猜测和随机变化组合,直到COMExceptions停止发生,但我宁愿翻译是正确的,因为有人居然看着他们,并宣布他们。

Some of these are borrowed from other source code, some were copied verbatim from pinvoke.net, and some look to be directly translated from the SDK headers. One thing I notice is that there is no consistency about when to use the various marshalling attributes, (even among the pinvoke.net examples this is pretty hit-or-miss). Part of the problem is, I don't think anyone here (myself included) fully understands when the various attributes are needed or not, or what they actually do. Up to this point, getting these right seems to be a combination of guesswork and random changes until the COMExceptions stop happening, but I'd much rather the translations be correct because someone actually looked at them and declared them so.

所以,我开始用 [IN] [OUT] 。我知道这两个属性做概念:他们通知哪个方向的数据已去编组。我的假设的编组,例如,不会打扰复制 [IN] 数据返回给调用者,或者知道 [OUT] 数据可能需要在被叫方方被释放,等等。我不知道的事情是:

So, I'm starting with [In] and [Out]. I know what those two attributes do conceptually: they inform the marshaller which direction the data has to go. I assume the marshaller, for example, won't bother copying [In] data back to the caller, or knows that [Out] data might need to be freed on the callee side, etc. The things I don't know are:


  1. 什么时候必要以使用这些属性?也就是说,当是默认编组行为错了,这样的属性,使其正确吗?

  2. 什么时候安全以使用这些属性?也就是说,将指​​定什么应该已经是默认的行为方式的转变编组的作品?

  3. 什么时候危险以使用这些属性?我认为明确地标记输出参数 [IN] 是坏的,但标记输入参数 [输入,输出] 其实要打破什么

  1. When is it necessary to use these attributes? That is, when is the default marshalling behavior wrong such that the attributes make it correct?
  2. When is it safe to use these attributes? That is, will specifying what should already be the default behavior change the way the marshaller works?
  3. When is it dangerous to use these attributes? I assume that explicitly marking an output parameter [In] is bad, but is marking an input parameter [In, Out] actually going to break anything?

所以,对于一个假想的COM接口方法的IDL看起来像这样:

So, given a hypothetical COM Interface method whose IDL looks like this:

HRESULT Foo(
    [in] ULONG a,
    [out] ULONG * b
    [in, out] ULONG * c);



我可能会看到这个翻译成任何如下:

I might see this translated as any of the following:

void Foo(
  uint cb,
  out uint b,
  ref uint c);

void Foo(
  uint cb,
  [Out] out uint b,
  [In, Out] ref uint c);

void Foo(
  [In] uint cb,
  [Out] out uint b,
  [In, Out] ref uint c);

有这三个之间的功能差异?因为除了技术正确性原因,是任何人认为是更好比其他人呢?

Is there any functional difference between those three? Is any of them considered "better" than the others for reasons besides technical correctness?

推荐答案

不,这不是绝对必要的申请任何在自己的代码,这些属性。 。他们都是可选的,并自动应用到根据参数的类型代码

No, it's not strictly necessary to apply any of these attributes in your own code. They're all optional, and automatically applied to your code depending on the type of the parameter.

从本质上讲,它们具有以下的C#关键字等同于:

Essentially, they have the following C# keyword equivalents:


  • 您获得 [IN] 默认情况下。

  • REF 关键字可以让你 [输入,输出]

  • 退出关键字可以让你 [OUT]

  • You get [In] by default.
  • The ref keyword gets you [In, Out].
  • The out keyword gets you [Out].

所有在一个不错的表这里记录

您只需要在要改变这些隐含的语义或改变编组的默认行为使用它们。例如,您可以标记声明的参数 REF [IN] 属性只能抑制出的一部分的编组。

You only need to use them when you want to alter those implied semantics or change the default behavior of the marshaler. For example, you can mark a parameter declared ref with the [In] attribute only to suppress the "out" part of marshaling.

这是说,我发现自己使用他们,因为他们正在做的代码的自我记录的一个非常简单的方法。

That said, I find myself using them because they're a very easy way of making code self-documenting.

这篇关于如果有必要/适合使用InAttribute和OutAttribute为COM Interop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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