问题描述
由于我是初学者,所以这可能是一个非常基本的问题.我正在启动DirectX 11,在创建我的第一个应用程序时,使用了wWinMain,并在寻找WinMain和wWinMain之间的差异时,遇到了此参数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始终为null,并且由于始终为null,所以为什么存在它(因为逻辑上认为创建者不会给出无用的参数是合理的).还有(引用书中的内容)
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),
什么是互斥锁以及如何使用它(一个好的链接就足够了).看起来需要一种方法来查找应用程序的另一个实例是否存在,prevInstance应该具有指向它的指针或引用,显然不是这种情况,因为它为null.为什么会这样?prevInstance的作用是什么?
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?
推荐答案
Raymond Chen的博客几乎完全致力于讨论当今Windows API的奇特之处".幸运的是,他有一个博客帖子,它回答了这个确切的问题:
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:
...
这是将hPrevInstance参数设置为WinMain的原因.如果 hPrevInstance为非NULL,则它是副本的实例句柄 已经运行的程序.您可以使用GetInstanceData来 从中复制数据,让自己更快地动起来.例如, 您可能要从前一个窗口中复制主窗口句柄 实例,以便您可以与之通信.
This was the reason for the hPrevInstance parameter to WinMain. If hPrevInstance was non-NULL, then it was the instance handle of a copy of the program that is already running. You can use GetInstanceData to copy data from it, get yourself up off the ground faster. For example, you might want to copy the main window handle out of the previous instance so you could communicate with it.
hPrevInstance是否为NULL不会告诉您您是否是 该程序的第一个副本.在16位Windows下,只有第一个 程序实例注册其类;第二和随后 实例继续使用由 第一个例子. (实际上,如果他们尝试过,注册将会失败 (因为该类已经存在.)因此,所有16位Windows 如果hPrevInstance是 非NULL.
Whether hPrevInstance was NULL or not told you whether you were the first copy of the program. Under 16-bit Windows, only the first instance of a program registered its classes; second and subsequent instances continued to use the classes that were registered by the first instance. (Indeed, if they tried, the registration would fail since the class already existed.) Therefore, all 16-bit Windows programs skipped over class registration if hPrevInstance was non-NULL.
设计Win32的人发现自己在修复时遇到了一些麻烦 是时候移植WinMain了:hPrevInstance需要传递什么?这 毕竟,Win32中不存在整个模块/实例的内容,并且 单独的地址空间意味着跳过的程序 在第二个实例中重新初始化将不再起作用.所以Win32 总是传递NULL,使所有程序都认为它们是 第一个.
The people who designed Win32 found themselves in a bit of a fix when it came time to port WinMain: What to pass for hPrevInstance? The whole module/instance thing didn't exist in Win32, after all, and separate address spaces meant that programs that skipped over reinitialization in the second instance would no longer work. So Win32 always passes NULL, making all programs believe that they are the first one.
当然,由于兼容性原因,现在hPrevInstance
与当今的Windows API无关,MSDN建议您使用互斥锁来检测应用程序的先前实例.
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.
互斥锁代表互斥".您可以参考 MSDN文档中的CreateMutex()
.有很多使用互斥对象检测应用程序以前实例的示例,例如 .基本思想是用您想出的唯一名称创建一个互斥锁,然后尝试创建一个名为互斥锁的互斥锁.如果CreateMutex()
因ERROR_ALREADY_EXISTS
而失败,则说明您的应用程序实例已经启动.
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. If CreateMutex()
failed with ERROR_ALREADY_EXISTS
, you know that an instance of your application was already launched.
这篇关于如果prevInstance始终为NULL,为什么在WinMain和wWinMain中存在prevInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!