简体   繁体   English

.NET COM互操作方法签名

[英].NET COM Interop Method Signature

What interop signature would you use for the following COM method? 您将使用什么互操作签名用于以下COM方法? I am interested particularly in the final two parameters, and whether to try to use MarshalAs with a SizeParamIndex or not. 我特别感兴趣的是最后两个参数,以及是否尝试使用MarshalAsSizeParamIndex

HRESULT GetOutputSetting(
  DWORD    dwOutputNum,
  LPCWSTR  pszName,
  WMT_ATTR_DATATYPE*  pType,
  BYTE*    pValue,
  WORD*    pcbLength
);

Documentation states: 文件说明:

pValue [out] Pointer to a byte buffer containing the value. pValue [out]指向包含该值的字节缓冲区的指针。 Pass NULL to retrieve the length of the buffer required. 传递NULL以检索所需缓冲区的长度。

pcbLength [in, out] On input, pointer to a variable containing the length of pValue. pcbLength [in,out]输入时,指向包含pValue长度的变量的指针。 On output, the variable contains the number of bytes in pValue used. 在输出时,变量包含使用的pValue中的字节数。

You could try the PInvoke Signature Toolkit . 您可以尝试PInvoke签名工具包 It's rather useful for getting marshaling right when performing platform interops. 在执行平台互操作时正确编组是非常有用的。 It quite possibly won't cover your particular problem, but you may find a comparable one that gives you the information you seek. 它很可能不会涵盖您的特定问题,但您可能会找到一个类似的问题,为您提供所寻求的信息。

I would use the SizeParamIndex, because your scenario is exactly the one this feature is for: To specify the length of a variable sized array. 我会使用SizeParamIndex,因为您的场景正是此功能的用途:指定变量大小的数组的长度。

So the last to parameters would be in C# signature: 所以参数的最后一个是C#签名:

byte[] pValue,
ref ushort pcbLength

The byte-Array is passed without ref , because the array corresponds to a pointer in native code. 在没有ref的情况下传递byte-Array,因为该数组对应于本机代码中的指针。 If you pass NULL (or null in C#) for pValue in order to retrieve the size of the buffer needed. 如果为pValue传递NULL(或在C#中为null),以便检索所需缓冲区的大小。 That means also that the caller has to allocate the byte-Array. 这也意味着调用者必须分配字节数组。 The parameter pcbLength is passed by ref , because it is used as an in/out-parameter. 参数pcbLength由ref传递,因为它用作输入/输出参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM