<bdo id='3rSaS'></bdo><ul id='3rSaS'></ul>

    <small id='3rSaS'></small><noframes id='3rSaS'>

      1. <i id='3rSaS'><tr id='3rSaS'><dt id='3rSaS'><q id='3rSaS'><span id='3rSaS'><b id='3rSaS'><form id='3rSaS'><ins id='3rSaS'></ins><ul id='3rSaS'></ul><sub id='3rSaS'></sub></form><legend id='3rSaS'></legend><bdo id='3rSaS'><pre id='3rSaS'><center id='3rSaS'></center></pre></bdo></b><th id='3rSaS'></th></span></q></dt></tr></i><div id='3rSaS'><tfoot id='3rSaS'></tfoot><dl id='3rSaS'><fieldset id='3rSaS'></fieldset></dl></div>
      2. <legend id='3rSaS'><style id='3rSaS'><dir id='3rSaS'><q id='3rSaS'></q></dir></style></legend>

        <tfoot id='3rSaS'></tfoot>

      3. P/Invoke [In, Out] 属性对于编组数组是可选的吗?

        Are P/Invoke [In, Out] attributes optional for marshaling arrays?(P/Invoke [In, Out] 属性对于编组数组是可选的吗?)

          <small id='ok5PD'></small><noframes id='ok5PD'>

          1. <i id='ok5PD'><tr id='ok5PD'><dt id='ok5PD'><q id='ok5PD'><span id='ok5PD'><b id='ok5PD'><form id='ok5PD'><ins id='ok5PD'></ins><ul id='ok5PD'></ul><sub id='ok5PD'></sub></form><legend id='ok5PD'></legend><bdo id='ok5PD'><pre id='ok5PD'><center id='ok5PD'></center></pre></bdo></b><th id='ok5PD'></th></span></q></dt></tr></i><div id='ok5PD'><tfoot id='ok5PD'></tfoot><dl id='ok5PD'><fieldset id='ok5PD'></fieldset></dl></div>
                <tbody id='ok5PD'></tbody>
              <tfoot id='ok5PD'></tfoot>
                <bdo id='ok5PD'></bdo><ul id='ok5PD'></ul>
                <legend id='ok5PD'><style id='ok5PD'><dir id='ok5PD'><q id='ok5PD'></q></dir></style></legend>
                  本文介绍了P/Invoke [In, Out] 属性对于编组数组是可选的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Assume that there is a native function with a pure-C interface like the following one, exported from a native DLL:

                  // NativeDll.cpp
                  
                  extern "C" void __stdcall FillArray(
                      int fillValue, 
                      int count, 
                      int* data)
                  {
                      // Assume parameters are OK...
                  
                      // Fill the array
                      for (int i = 0; i < count; i++)
                      {
                          data[i] = fillValue;
                      }
                  }
                  

                  The following P/Invoke works fine (tested with VS2010 SP1):

                  [DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
                  public static extern void FillArray(
                      int fillValue,
                      int count,
                      [In, Out] int[] data
                  );
                  

                  as well as this P/Invoke, same as above, but without the [In, Out] attributes:

                  [DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
                  public static extern void FillArray(
                      int fillValue,
                      int count,
                      int[] data
                  );
                  

                  So, are those [In, Out] attributes optional for marshaling arrays? What is their purpose, if any? Is it OK to omit them in our P/Invoke declarations?

                  解决方案

                  No, they are not exactly optional. It just happens to work by accident. It is however a very common accident. It works because the array doesn't actually gets marshaled. The pinvoke marshaller sees that the C# array is already compatible with the native array so skips the step to create a copy of it. It merely pins the array and passes the pointer to the native code.

                  This is of course very efficient and you will inevitably get the results back because the native code is directly writing the array elements. So neither the [In] nor the [Out] attributes matter.

                  It gets much murkier if the array element type isn't that simple. It isn't that easy to identify an element type that's a struct or class type that isn't blittable or whose layout doesn't match after marshaling so the pinvoke marshaller has to make a copy of the array. Especially the layout incompatibility can be very hard to identify because the managed layout is undiscoverable. And can change depending on the jitter that is used. It may work in x86 but not x64 for example, pretty nasty when AnyCPU is selected. Getting it to copy the modified copy back to the C# array does require [Out].

                  Not sure what to advice, other than that nobody ever got fired for being explicit in their declarations. Perhaps you should always be explicit when the array element type isn't simple so you'll never have an accident.

                  这篇关于P/Invoke [In, Out] 属性对于编组数组是可选的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                  1. <small id='oDCeU'></small><noframes id='oDCeU'>

                      <legend id='oDCeU'><style id='oDCeU'><dir id='oDCeU'><q id='oDCeU'></q></dir></style></legend>

                          <tbody id='oDCeU'></tbody>
                        <i id='oDCeU'><tr id='oDCeU'><dt id='oDCeU'><q id='oDCeU'><span id='oDCeU'><b id='oDCeU'><form id='oDCeU'><ins id='oDCeU'></ins><ul id='oDCeU'></ul><sub id='oDCeU'></sub></form><legend id='oDCeU'></legend><bdo id='oDCeU'><pre id='oDCeU'><center id='oDCeU'></center></pre></bdo></b><th id='oDCeU'></th></span></q></dt></tr></i><div id='oDCeU'><tfoot id='oDCeU'></tfoot><dl id='oDCeU'><fieldset id='oDCeU'></fieldset></dl></div>
                          <bdo id='oDCeU'></bdo><ul id='oDCeU'></ul>
                          • <tfoot id='oDCeU'></tfoot>