如何,使用P时,Win32的映射类型和C#类/调用? [英] How to map Win32 types to C# types when using P/Invoke?
问题描述
我试图做这样的事情这在C#。我发现了如何利用P至调用从C#的Win32方法/从这个链接调用。不过,我在实施的P / Invoke遇到了一些困难。
例如,我想访问的方法之一是 PdhOpenQuery
签名:
PDH_STATUS PdhOpenQuery(
__in LPCTSTR szDataSource,
__in DWORD_PTR dwUserData,
__out PDH_HQUERY * phQuery
);
我的数字对应的C#声明应该是这样的。
函数[DllImport(的Pdh.dll)]
静态外部PDH_STATUS PdhOpenQuery(LPCTSTR szDataSource,
DWORD_PTR dwUserData,出PDH_HQUERY * phQuery);
我的问题:
什么是LPCTSTR,以及在何种类型的数据它映射在C#中?结果
如何绘制指针类型DWORD_PTR?该PInvoke的文章说DWORD映射到UInt32的,但如何指点?结果
我觉得PDH_STATUS和PDH_HQUERY特定结构图书馆(我还没有确定)。我怎么映射这些?
什么是正确的方法声明,你怎么正确地调用它?
什么是LPCTSTR,到什么类型的数据
它在C#中映射?
块引用>的typedef
LPCTSTR
是常量TCHAR *
。
TCHAR
是试图抽象掉了Windows API中存在的事实既ANSI(字符
在一个特定区域的编码字符串)和统一code(UTF-16)的版本。没有实际的PdhOpenQuery
功能;有一个PdhOpenQueryA
函数,它的ANSI字符串,并带有一个UTF-16字符串PdhOpenQueryW
功能。C#使用UTF-16字符串,所以你要preFER这些功能的W的版本。使用
PdhOpenQueryW
。那么第一个参数C ++类常量为wchar_t *
。 C#的类型是[的MarshalAs(UnmanagedType.LPWStr)字符串
。
如何绘制指针类型DWORD_PTR?
该PInvoke的文章说DWORD映射到
UInt32的,但如何指点?
块引用>
DWORD_PTR
不是一个指针。这是一个无符号的整数的大足的持有的一个指针。等价的C#类型是System.UIntPtr
。
我觉得PDH_STATUS和PDH_HQUERY是
具体结构图书馆(我
尚未确定)。我怎么映射这些?
块引用>
PDH_STATUS
似乎只是一个INT
。
PDH_HQUERY
是一个指向一个句柄(另一个指针),但你可以只pretend它是一个整数,并使用的IntPtr
。全部放在一起,你的声明应该是:
函数[DllImport(的Pdh.dll)]
静态外部INT PdhOpenQueryW(
[的MarshalAs(UnmanagedType.LPWStr)字符串szDataSource,
UIntPtr dwUserData,
出的IntPtr phQuery);I am trying to do something like this in C#. I found out how to call Win32 methods from C# using P/Invoke from this link. However I met some difficulties in implementing P/Invoke.
For example, one of the methods that I would like to access is PdhOpenQuery, signature:
PDH_STATUS PdhOpenQuery( __in LPCTSTR szDataSource, __in DWORD_PTR dwUserData, __out PDH_HQUERY *phQuery );
I figure the corresponding C# declaration should be something like this
[DllImport("Pdh.dll")] static extern PDH_STATUS PdhOpenQuery(LPCTSTR szDataSource, DWORD_PTR dwUserData, out PDH_HQUERY *phQuery);
My questions:
What is LPCTSTR, and to what data type does it map in C#?
How to map a pointer type DWORD_PTR? The pinvoke article says DWORD maps to UInt32, but how about pointers?
I think PDH_STATUS and PDH_HQUERY are specific struct to the library (I'm not sure yet). how do I map these?What is the correct method declaration, and how do you call it correctly?
解决方案What is LPCTSTR, and to what data type does it map in C#?
LPCTSTR
is a typedef forconst TCHAR*
.
TCHAR
is an attempt to abstract away the fact that the Windows API exists in both "ANSI" (char
strings in a locale-specific encoding) and "Unicode" (UTF-16) versions. There is no actualPdhOpenQuery
function; there is aPdhOpenQueryA
function that takes an ANSI string and aPdhOpenQueryW
function that takes a UTF-16 string.C# uses UTF-16 strings, so you'll want to prefer the "W" version of these functions. Use
PdhOpenQueryW
. Then the first parameter has C++ typeconst wchar_t*
. The C# type is[MarshalAs(UnmanagedType.LPWStr)] string
.How to map a pointer type DWORD_PTR? The pinvoke article says DWORD maps to UInt32, but how about pointers?
DWORD_PTR
isn't a pointer. It's an unsigned integer big enough to hold a pointer. The equivalent C# type isSystem.UIntPtr
.I think PDH_STATUS and PDH_HQUERY are specific struct to the library (I'm not sure yet). how do I map these?
PDH_STATUS
appears to be just anint
.
PDH_HQUERY
is a pointer to a handle (another pointer), but you can just pretend it's an integer and useIntPtr
.Putting it all together, your declaration should be:
[DllImport("Pdh.dll")] static extern int PdhOpenQueryW( [MarshalAs(UnmanagedType.LPWStr)] string szDataSource, UIntPtr dwUserData, out IntPtr phQuery);
这篇关于如何,使用P时,Win32的映射类型和C#类/调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!