简体   繁体   English

将Windows数据类型映射到.NET

[英]Mapping Windows Data Types to .NET

I have just been looking through the list of Windows Data Types as I am trying to work out how each type maps to it's equivalent .NET type. 我一直在浏览Windows数据类型列表,以尝试找出每种类型如何映射到等效的.NET类型。

I noticed that some of the type definitions are surrounded by #if tags, which changes their definition based upon the platform. 我注意到某些类型定义被#if标记包围,该标记会根据平台更改其定义。

For example, here is the definition for INT_PTR 例如,这是INT_PTR的定义

#if defined(_WIN64) 
 typedef __int64 INT_PTR; 
#else 
 typedef int INT_PTR;
#endif

My understanding is that this creates a 64bit INT_PTR on 64bit machines, and a 32bit INT_PTR on 32bit machines. 我的理解是,这将创建一个64位INT_PTR在64位计算机和32位INT_PTR在32位机器。 Okay....NET does the same thing in this respect as IntPtr and UIntPtr are platform specific, and therefore adapt between 32bit and 64bit machines. 好的UIntPtr在这方面做了同样的事情,因为IntPtrUIntPtr是特定于平台的,因此可以在32位和64位计算机之间进行调整。

Now, lets consider LONGLONG 现在,让我们考虑一下LONGLONG

#if !defined(_M_IX86)
 typedef __int64 LONGLONG; 
#else
 typedef double LONGLONG;
#endif

So in .NET my assumption is that this maps to Int64 ( long ) ? 因此,在.NET中,我的假设是这映射到Int64long )吗?

Also, consider TCHAR 另外,考虑一下TCHAR

#ifdef UNICODE
 typedef WCHAR TCHAR;
#else
 typedef char TCHAR;
#endif

My assumption here is that this maps to char (since .NET's char is unicode anyway)? 我的假设是这映射到char(因为.NET的char无论如何都是unicode的)?

Questions: 问题:

  1. If a native type is platform dependent, should it's .NET equivalent be the larger of the two definitions? 如果本机类型依赖于平台,那么它的.NET等效项是否应为两个定义中的较大者?
  2. Are there any known pitfalls when mapping data types? 映射数据类型时是否存在任何已知的陷阱?

Check out Platform Invoke Data Types . 检出平台调用数据类型

There are many pitfalls when mapping data types. 映射数据类型时有很多陷阱。 For example, the difference between the Windows data types BOOL and Boolean . 例如,Windows数据类型BOOLBoolean之间的区别。 The default marshaling for the .NET Boolean data type (C# bool ) will marshal it as a 32-bit integer to match the Windows BOOL type. .NET Boolean数据类型(C# bool )的默认封送处理会将其封送为32位整数,以匹配Windows BOOL类型。 The Windows Boolean data type is one byte. Windows Boolean数据类型为1个字节。 If you want to pass a C# bool to a Windows Boolean , you have to specify custom marshaling. 如果要将C# bool传递给Windows Boolean ,则必须指定自定义封送处理。

Structure packing can trip you up, and marshaling arrays can be very tricky, especially arrays inside structures. 结构打包可能会使您绊倒,而编组数组可能非常棘手,尤其是结构内部的数组。

You definitely want to read Marshaling Data with Platform Invoke very carefully. 您绝对想非常仔细地阅读使用Platform Invoke编组数据

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

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