如果 prevInstance 始终为 NULL,为什么它存在于 WinMain 和 wWinMain 中

发布于 2024-12-05 09:41:53 字数 505 浏览 0 评论 0原文

由于我是初学者,这可能是一个非常基本的问题。我正在启动 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 技术交流群。

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

发布评论

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

评论(2

你列表最软的妹 2024-12-12 09:41:53

Raymond Chen 的博客几乎完全致力于讨论 Windows API 中“奇怪”的方面今天给我们。幸运的是,他有一篇博客文章回答了这个确切的问题:

在 16 位 Windows 中,有一个名为 GetInstanceData 的函数。这
函数接受一个 HINSTANCE、一个指针和一个长度,并复制内存
从该实例到您当前的实例。 (这有点像
16 位相当于 ReadProcessMemory,但限制是
第二个和第三个参数必须相同。)

...

这就是 WinMain 的 hPrevInstance 参数的原因。如果
hPrevInstance 为非 NULL,则它是副本的实例句柄
已经运行的程序。您可以使用 GetInstanceData 来
从中复制数据,让自己更快地起步。例如,
您可能想从以前的窗口句柄中复制主窗口句柄
实例,以便您可以与其通信。

hPrevInstance 是否为 NULL 告诉您是否是
程序的第一个副本。在16位Windows下,只有第一个
程序的实例注册了它的类;第二次及后续
实例继续使用由
一审。 (事实上​​,如果他们尝试的话,注册就会失败
因为该类已经存在。)因此,所有 16 位 Windows
如果 hPrevInstance 是,程序会跳过类注册
非空。

设计 Win32 的人们发现自己陷入了困境
是时候移植 WinMain 了:为 hPrevInstance 传递什么?这
毕竟,整个模块/实例在 Win32 中并不存在,并且
单独的地址空间意味着跳过的程序
第二次重新初始化将不再起作用。所以Win32
总是传递 NULL,使所有程序都相信它们是
第一个。

当然,除了兼容性原因之外,现在 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:

In 16-bit Windows there was a function called GetInstanceData. This
function took an HINSTANCE, a pointer, and a length, and copied memory
from that instance into your current instance. (It's sort of the
16-bit equivalent to ReadProcessMemory, with the restriction that the
second and third parameters had to be the same.)

...

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.

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.

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.

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. If CreateMutex() failed with ERROR_ALREADY_EXISTS, you know that an instance of your application was already launched.

清音悠歌 2024-12-12 09:41:53

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文