c#中的DWORD和VARTYPE等效

时间:2013-08-06 10:19:16

标签: c# c#-4.0 pinvoke marshalling

我使用的API包含以下方法:

BOOL GetItemPropertyDescription (HANDLE hConnect, int PropertyIndex, DWORD *pPropertyID, VARTYPE *pVT, BYTE *pDescr, int BufSize);
BOOL ReadPropertyValue (HANDLE hConnect, LPCSTR Itemname, DWORD PropertyID, VARIANT *pValue);

c#中的等价物是什么?

DWORD, VARTYPE, VARIANT数据类型是什么意思?

1 个答案:

答案 0 :(得分:3)

表1 有一个相当完整的表格here。试试看。

DWORD is uint
VARTYPE is an ushort (but you have a ref ushort there) 
        or much better a VarEnum (but you have a ref VarEnum there)
        (VarEnum is defined under System.Runtime.InteropServices)
VARIANT is object (but you have a ref object there)

这里有一篇关于VARIANT封送的文章:http://blogs.msdn.com/b/adam_nathan/archive/2003/04/24/56642.aspx

确切的PInvoke写入很复杂,它取决于参数的方向和它们的确切规范。 pPropertyID是指向单个DWORD的指针,还是指向“数组”的第一个DWORD的指针?谁“填写”指定的值,来电者或被叫者或两者?所有其他指针都一样。

从技术上讲,ref的全部/部分可能是out,如果它们被被叫方填充。

通过方法的名称,他们的pinvoke可能是:

[DllImport("YourDll.dll")]
//[return: MarshalAs(UnmanagedType.Bool)] // This line is optional, and it's implicit
bool GetItemPropertyDescription(IntPtr hConnect, 
                                int propertyIndex, 
                                out uint pPropertyID, 
                                out VarEnum pVT, 
                                out IntPtr pDescr, 
                                int bufSize);

[DllImport("YourDll.dll", CharSet = CharSet.Ansi)]
//[return: MarshalAs(UnmanagedType.Bool)] // This line is optional, and it's implicit
bool ReadPropertyValue(IntPtr hConnect, 
                       string itemName, 
                       uint propertyID, 
                       out object pValue);