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

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

问题描述

我正在尝试做类似这个 在 C# 中.我发现了如何使用 P/Invoke 从 C# 调用 Win32 方法 从此链接.然而,我在实现 P/Invoke 时遇到了一些困难.

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.

例如,我想访问的方法之一是 PdhOpenQuery,签名:

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
);

我认为相应的 C# 声明应该是这样的

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);

我的问题:

什么是 LPCTSTR,它在 C# 中映射到什么数据类型?
如何映射指针类型 DWORD_PTR?pinvoke 文章说 DWORD 映射到 UInt32,但是指针呢?
我认为 PDH_STATUS 和 PDH_HQUERY 是库的特定结构(我还不确定).我如何映射这些?

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?

推荐答案

什么是 LPCTSTR,数据类型是什么它是否在 C# 中映射?

What is LPCTSTR, and to what data type does it map in C#?

LPCTSTRconst TCHAR* 的 typedef.

LPCTSTR is a typedef for const TCHAR*.

TCHAR 试图抽象出 Windows API 存在于ANSI"(特定于语言环境的编码中的 char 字符串)和Unicode"中的事实(UTF-16) 版本.没有实际的 PdhOpenQuery 函数;有一个 PdhOpenQueryA 函数接受一个 ANSI 字符串,一个 PdhOpenQueryW 函数接受一个 UTF-16 字符串.

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# 使用 UTF-16 字符串,因此您会更喜欢这些函数的W"版本.使用 PdhOpenQueryW.然后第一个参数具有 C++ 类型 const wchar_t*.C# 类型是 [MarshalAs(UnmanagedType.LPWStr)] 字符串.

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.

如何映射指针类型 DWORD_PTR?pinvoke 文章说 DWORD 映射到UInt32,但是指针呢?

How to map a pointer type DWORD_PTR? The pinvoke article says DWORD maps to UInt32, but how about pointers?

DWORD_PTR 不是指针.它是一个无符号的整数,足以保存一个指针.等效的 C# 类型是 System.UIntPtr.

DWORD_PTR isn't a pointer. It's an unsigned integer big enough to hold a pointer. The equivalent C# type is System.UIntPtr.

我认为 PDH_STATUS 和 PDH_HQUERY 是库的特定结构(我是尚未确定).我如何映射这些?

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 似乎只是一个 int.

PDH_HQUERY 是一个指向句柄(另一个指针)的指针,但你可以假装它是一个整数并使用 IntPtr.

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/Invoke 时如何将 Win32 类型映射到 C# 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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