WinMain中的hPrevInstance的目的是什么

这个问题在这里已经有一个答案:> Why does prevInstance exist in WinMain and wWinMain if it is always NULL2
definition of WinMain是:
int CALLBACK WinMain(
    _In_ HINSTANCE hInstance,_In_ HINSTANCE hPrevInstance,_In_ LPSTR     lpCmdLine,_In_ int       nCmdshow
);

我所了解的是:

> hInstance是应用程序实例的句柄,可以,when not a DLL,be retrieved with GetModuleHandle(NULL)
> szCmdLine是命令行参数,可以用GetCommandLine()重试
> nCmdshow通常是SW_SHOW

然而,从来没有我遇到过任何使用hPrevInstance,即使在20世纪90年代后期的书籍.那么,如果有的话,使用hPrevInstance,究竟是什么呢?

解决方法

这是一件遗产.陈冯富珍在 The Old New Thing(2004年6月15日)提供了一个很好的解释.这是(与纠正的链接):

Once your average GUI program picks itself up off the ground,control begins at your 07001. The second parameter,hPrevInstance,is always zero in Win32 programs. Certainly it had a meaning at some point?

Of course it did.

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 07002,with the restriction that the second and third parameters had to be the same.)

(Since 16-bit Windows had a common address space,the GetInstanceData function was really nothing more than a hmemcpy,and many programs relied on this and just used raw hmemcpy instead of using the documented API. Win16 was actually designed with the possibility of imposing separate address spaces in a future version – observe flags like GMEM_SHARED – but the prevalence of tricks like hmemcpy’ing your prevIoUs instance reduced this potential to an unrealized dream.)

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.

And amazingly,it actually worked.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...