出于任何原因,我不应该将 GUID 验证添加到我的 Visual C 中;程序?

发布于 2024-10-08 18:11:44 字数 578 浏览 6 评论 0原文

为了使我的 Visual C++ 程序更加健壮,我很想插入检查 GUID 变量 包含有效版本4 个 GUID(并且不会未初始化):

GUID guid;
UuidCreate( &guid );
// many lines of code later...
// the following assert should not fire for valid version 4 GUIDs
int data3 = guid.Data3;
assert( ( data3 >> 12 ) == 4 );

我完全确定所有 GUID 通常都来自 UuidCreate() 函数内部,或者是未初始化的变量(后者是我所使用的)想通过这些检查进行诊断)。我唯一担心的是 Microsoft 可能会突然改变 Windows 未来版本中的 GUID 实现。

我还应该考虑哪些其他因素来决定此类检查是否不会造成伤害?另外,GUID 实现在未来版本的 Windows 上发生更改的可能性有多大?

In order to make my Visual C++ program more robust I'm tempted to insert checks that GUID variables contain valid version 4 GUIDs (and are not left uninitialized):

GUID guid;
UuidCreate( &guid );
// many lines of code later...
// the following assert should not fire for valid version 4 GUIDs
int data3 = guid.Data3;
assert( ( data3 >> 12 ) == 4 );

I'm completely sure that all GUIDs will either normally come from within UuidCreate() function or be uninitialized variables (and the latter is what I'd like to diagnose with those checks). My only concern is Microsoft could suddenly change GUID implementation in future versions of Windows.

What other factors should I consider to decide whether such checks won't hurt? Also how likely is it that GUID implementation changes on future versions of Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄离 2024-10-15 18:11:44

我唯一担心的是 Microsoft 可能会突然更改 Windows 未来版本中的 GUID 实现。

如果发生的话,处理它。不,说真的,您还可能担心 integer 的实现,Microsoft 可能决定切换到 .NET 数据类型,并使 integer 系统范围内的值成为 4 字节值。

担心其他事情,如果实现发生变化,您无论如何都必须重写/更改/修复您的程序。

编辑:如果我理解正确,那么您担心的是函数的返回值,而不是 GUID 结构本身。您的意思是,目前它可以返回 GUID 或未初始化的值,但在未来的实现中它可能会返回 0 填充的 GUID?

在这种情况下,我会坚持我的第一段,担心它是否会发生。目前该函数具有明确定义的行为,请使用它。检查是否有未初始化的值,仅此而已。你无法猜测微软未来会做什么(见鬼,有时连微软都猜不到)。

My only concern is Microsoft could suddenly change GUID implementation in future versions of Windows.

Deal with it, if it happens. No, seriously, you could also worry about the implementation of integer, Microsoft could decide to switch to the .NET datatypes and make the integer system-wide a 4-byte value.

Worry about other things, if implementations are changing, you'll have to rewrite/change/fix your program anyway.

Edit: If I understand you right, you're worrying about the return value of the function, not the GUID-Structure itself. And you mean that at the moment it can return either a GUID or the uninitialized value, but in future implementations it might return a 0-filled-GUID?

In that case I stick to my first paragraph, worry about it if it happens. At the moment the function has a clearly defined behavior, work with that. Check for uninitialized values and that's it. You can't guess what Microsoft will do in the future (hell, sometimes even Microsoft can't).

心碎无痕… 2024-10-15 18:11:44

好吧,让我们看看...您在调用 UuidCreate 时无法指定特定的 GUID 版本,并且您会期望返回特定的版本吗?这怎么是个好主意呢?

如果您想使您的软件“更加健壮”,那么初始化变量并检查错误怎么样?

GUID guid = {0};
if (SUCCEEDED(UuidCreate( &guid ) ) {
    // you can be _pretty_ sure guid has some valid stuff in it
}

Well, let's see... you're calling UuidCreate without being able to specify a particular GUID version, and you're going to expect a particular version in return? How is that a good idea?

If you're trying to make your software "more robust", how about initializing your variables and checking for errors?

GUID guid = {0};
if (SUCCEEDED(UuidCreate( &guid ) ) {
    // you can be _pretty_ sure guid has some valid stuff in it
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文