如何,使用P时,Win32的映射类型和C#类/调用? [英] How to map Win32 types to C# types when using P/Invoke?

查看:219
本文介绍了如何,使用P时,Win32的映射类型和C#类/调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这样的事情在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#中映射?


LPCTSTR 常量TCHAR *

的typedef

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 for const 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 actual PdhOpenQuery function; there is a PdhOpenQueryA function that takes an ANSI string and a PdhOpenQueryW 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++ type const 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 is System.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 an int.

PDH_HQUERY is a pointer to a handle (another pointer), but you can just pretend it's an integer and use IntPtr.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆