无法理解LARGE_INTEGER结构 [英] can't make sense of LARGE_INTEGER struct

查看:143
本文介绍了无法理解LARGE_INTEGER结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++和一些Winapi东西,我遇到了这个家伙:

With C++ and some Winapi things, I encountered this guy:

#if defined(MIDL_PASS)
typedef struct _LARGE_INTEGER {
#else // MIDL_PASS
typedef union _LARGE_INTEGER {
    struct {
        DWORD LowPart;
        LONG HighPart;
    };
    struct {
        DWORD LowPart;
        LONG HighPart;
    } u;
#endif //MIDL_PASS
    LONGLONG QuadPart;
} LARGE_INTEGER;

所以,我的观察方式取决于是否设置了MIDL_PASS,这要么是

So, the way I see it, depending on MIDL_PASS being set or not, this is either a very compact struct with only a LONGLONG in it, or the much more interesting case, this becomes a union.

如果这是一个联合,对我来说还是有意义的,具有两个访问权限,一个是LONGLONG块,另一个是具有Low和Highpart的结构。
到目前为止一切都很好。

In case this is a union, it still makes sense to me, to have two possibilites of access, once the LONGLONG in one chunk, and once the struct with Low and Highpart. So far so good.

但是,由于该结构被两次声明,所以我没有任何道理。似乎它们都是匿名的,但后者可以通过 u获得。

But I cannot make any sense out of the fact that the struct is declared twice, identically. It seems they are both anonymous, but the latter one is available via "u".

现在我的问题是:

为什么要定义两个结构(冗余?),第一个结构的目的是什么,如果由于没有绑定到任何类型/变量名而无法访问它的话。

Why are the two structs defined (redundantly?), what is the purpose of the first one, if I cannot even access it, due to not being bound to any type / variable name.

推荐答案

Microsoft提供匿名结构作为扩展(他们的示例显示了另一个结构内部的一个结构,但联合结构中的一个结构与此类似)。如果您不介意基于其扩展名的非便携式代码,则可以使用类似的东西:

Microsoft provides anonymous structs as an extension (their example shows one struct inside another struct, but a struct in a union is similar). If you don't mind non-portable code based on their extension, you can use things like:

LARGE_INTEGER a;
a.LowPart = 1;

但是如果您想要可移植的代码,则需要:

but if you want portable code, you need:

a.u.LowPart = 1;

工会允许您使用其中任何一个。

The union lets you use either.

这篇关于无法理解LARGE_INTEGER结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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