<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] 属性对于编组数组是可选的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  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>