文件时间到 __int64
将 FILETIME 结构转换为 __int64 的正确方法是什么?你能告诉我吗?
What is the proper way to convert a FILETIME
structure into __int64
? Can you please tell me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为您不应该:“不要将指向
FILETIME
结构的指针转换为ULARGE_INTEGER*
或__int64*
值,因为它可能会导致 64 位 Windows 上的对齐错误。”来源。
如果你真的想要的话,那就是类似:
但类似:
很糟糕。
I don't think you're suppose to: "Do not cast a pointer to a
FILETIME
structure to either aULARGE_INTEGER*
or__int64*
value because it can cause alignment faults on 64-bit Windows."Source.
If you really wanted it would be something like:
But something like:
Is bad.
无需使用按位或来恢复神秘的结构。 Windows API 拥有执行此操作所需的一切。
或者如果你想直接转到 __int64 。
There is no need to revert to arcane constructs using bitwise OR's. The Windows API has got everything you need to do this.
Or if you want to go to __int64 directly.
尝试
Try
当然,您可以只传递一个 __int64 转换为文件时间,如下所示 *(FILETIME*)&int64Val。这在 Visual C++ 下可以正常工作。
IE
Of course you could just pass in an __int64 casted to a filetime as follows *(FILETIME*)&int64Val. This will work fine under Visual C++.
ie
你可以尝试下面的代码。该代码来自 chromium 项目
you can try the code follow. the code is from chromium project
我遇到了完全相同的问题,用谷歌搜索,然后来到这里。但我还发现了一个有用的 Microsoft 支持页面:
https://support.microsoft. com/en-gb/help/188768/info-working-with-the-filetime-struct
它说:
用文件时间执行算术
通常需要对文件时间。例如,您可能需要知道文件的有效期为 30 天。要对文件时间执行算术,您需要将 FILETIME 转换为四字(64 位整数),执行算术,然后将结果转换回 FILETIME。
假设 ft 是一个包含文件创建时间的 FILETIME 结构,以下示例代码将时间添加 30 天:
编辑:我意识到这只是证实了其他一些答案,但我认为值得添加以进行澄清。
I had exactly the same issue, googled for it, and came here. But I also found a useful Microsoft support page at
https://support.microsoft.com/en-gb/help/188768/info-working-with-the-filetime-structure
It says:
Performing Arithmetic with File Times
It is often necessary to perform a simple arithmetic on file times. For example, you might need to know when a file is 30 days old. To perform an arithmetic on a file time, you need to convert the FILETIME to a quadword (a 64-bit integer), perform the arithmetic, and then convert the result back to a FILETIME.
Assuming ft is a FILETIME structure containing the creation time of a file, the following sample code adds 30 days to the time:
Edit: I realise this merely confirms some of the other answers, but I thought it was worth adding for clarification.
具有最佳性能的三种方法(一种汇编指令)。仅在 Visual C++ 2022 x64 中测试。
最好的办法。
缺点:
_UNALIGNED
宏(和__unaligned
修饰符)是非标准的。但几乎每个编译器都有某种形式的这个修饰符。缺点:只有打开“Enable Intrinsic Functions”C++ 编译器选项,此代码才会得到很好的优化。
其他答案有一些开销。
生成按位运算。
生成两个 32 位操作,而不是一个 64 位操作。
Three ways that have best performance (one assembler instruction). Tested only in Visual C++ 2022 x64.
The best way.
Disadvantage:
_UNALIGNED
macro (and__unaligned
modifier) is non-standard. But almost every compiler has this modifier in some form.Disadvantage: this code will be well optimized only if you turn on “Enable Intrinsic Functions” C++ compiler option.
Other answers have some overheads.
Generates bitwise operations.
Generates two 32-bit operations instead of one 64-bit one.