如果 prevInstance 始终为 NULL,为什么它存在于 WinMain 和 wWinMain 中
由于我是初学者,这可能是一个非常基本的问题。我正在启动 DirectX 11,在创建第一个应用程序时,使用了 wWinMain,在寻找 WinMain 和 wWinMain 之间的差异时,我遇到了这个参数 prevInstance。
根据MSDN,prevInstance总是为null,既然它总是为null,为什么它存在(因为认为创建者不会给出无用的参数是合乎逻辑的)。并且(引自书中),
如果您需要一种方法来确定以前的实例是否 应用程序已经在运行,文档建议创建 使用 CreateMutex 的唯一命名的互斥体。虽然互斥体将是 创建后,CreateMutex 函数将返回 ERROR_ALREADY_EXISTS。
什么是互斥体,以及如何使用它(一个好的链接就足够了)。看起来需要一个方法来查找应用程序的另一个实例是否存在, prevInstance 应该有一个指向它的指针或引用,但显然情况并非如此,因为它为 null。为什么会这样,prevInstance的作用是什么?
Since I am a beginner, it may be a very basic question. I am starting DirectX 11, and while creating my first application, wWinMain was used, and while searching for difference between WinMain and wWinMain, i came across this parameter prevInstance.
prevInstance is always null according to MSDN, and since it is always null, why does it exist (since it is logical to think that creators will not have given a useless parameter). And (quoting from the book),
if you need a way to determine whether a previous instance of the
application is already running, the documentation recommends creating
a uniquely named mutex using CreateMutex. Although the mutex will be
created, the CreateMutex function will return ERROR_ALREADY_EXISTS.
What is a mutex, and how to use it (a good link will be sufficient). And it looks like a method is needed to find if another instance of an application exists, prevInstance should have a pointer or reference to it, which is apparently not the case, since it is null. Why is it so, and what is the role of prevInstance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Raymond Chen 的博客几乎完全致力于讨论 Windows API 中“奇怪”的方面今天给我们。幸运的是,他有一篇博客文章回答了这个确切的问题:
当然,除了兼容性原因之外,现在
hPrevInstance
与当今的 Windows API 无关,MSDN 建议您使用互斥体来检测应用程序的先前实例。互斥体代表“互斥”。您可以参考MSDN文档了解
CreateMutex()
。有很多使用互斥体来检测应用程序以前实例的示例,例如这个< /a>.基本思想是创建一个具有您想出的唯一名称的互斥体,然后尝试创建该命名的互斥体。如果CreateMutex()
失败并出现ERROR_ALREADY_EXISTS
,则您知道应用程序的实例已启动。Raymond Chen's blog is almost entirely dedicated to discussing aspects of the Windows API that are "oddities" to us today. And fortunately, he has a blog post that answers this exact question:
Of course, now that
hPrevInstance
is irrelevant to the Windows API today except for compatibility reasons, MSDN recommends that you use a mutex to detect previous instances of an application.A mutex stands for "mutual exclusion". You can refer to the MSDN documentation for
CreateMutex()
. There are lots of examples of using mutexes to detect previous instances of applications, such as this one. The basic idea is to create a mutex with a unique name that you come up with, then attempt to create that named mutex. IfCreateMutex()
failed withERROR_ALREADY_EXISTS
, you know that an instance of your application was already launched.prev 实例参数用于 16 位 Windows 兼容性。我认为 WinMain 的 MSDN 参考文献中已经说明了这一点,至少以前是这样。
The prev instance parameter is there for 16-bit Windows compatibility. I think that was stated in the MSDN reference for WinMain, at least it used to be.