无法理解LARGE_INTEGER结构 [英] cant make sense of LARGE_INTEGER struct

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

问题描述

我主要是出于好奇问道。我不是一个C(++)编码器,但为了拓宽我的视野,我试图在其他领域每时每刻都潜水。当用C ++和一些Winapi的东西做这件事时,我遇到了这个家伙:

  #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被设置或不是,这是一个非常紧凑的结构只有一个长龙,或者更有趣的情况,这成为一个联盟。



如果这是一个联盟,对我来说依然是有道理的,有两个可能的访问,一次是长龙在一个块,一次结构与低和高。
到目前为止这么好。



但是我不能意识到结构体被声明为两次,相同。似乎他们都是匿名的,但后者可以通过u获得。



现在我的问题:



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



期待您的回答:)



问候,



Tom

解决方案

Microsoft提供匿名结构作为 extension (它们的例子在另一个结构体中显示一个结构体,但是联合体中的一个结构体是类似的)。如果您不介意基于其扩展名的非便携式代码,则可以使用以下内容:

  LARGE_INTEGER a; 
a.LowPart = 1;

但如果您想要便携式代码,则需要:

  auLowPart = 1; 

联盟允许您使用。


I'm asking this mainly out of curiousity. I am not really a C(++) Coder but to widen my horizon I try to dive in other areas every now and then. When doing this 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;

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.

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.

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".

Now to my question:

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.

Looking forward to your answers :)

greetings,

Tom

解决方案

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天全站免登陆