{"id":38873,"date":"2004-06-15T07:00:00","date_gmt":"2004-06-15T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/06\/15\/what-was-the-purpose-of-the-hprevinstance-parameter-to-winmain\/"},"modified":"2004-06-15T07:00:00","modified_gmt":"2004-06-15T07:00:00","slug":"what-was-the-purpose-of-the-hprevinstance-parameter-to-winmain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040615-00\/?p=38873","title":{"rendered":"What was the purpose of the hPrevInstance parameter to WinMain?"},"content":{"rendered":"<p>Once your average GUI program picks itself up off the ground,  control begins at your  <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/windows\/windowreference\/windowfunctions\/winmain.asp\">  WinMain function<\/a>.  The second parameter, hPrevInstance, is always zero in Win32 programs.  Certainly it had a meaning at some point?\n  Of course it did.\n  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&#8217;s sort of the 16-bit equivalent to  <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/debug\/base\/readprocessmemory.asp\">  ReadProcessMemory<\/a>, with the restriction that the second and  third parameters had to be the same.)\n  (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  &#8211; observe flags like GMEM_SHARED &#8211; but the prevalence  of tricks like hmemcpy&#8217;ing your previous instance reduced this potential  to an unrealized dream.)\n  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.\n  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.\n  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&#8217;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.<\/p>\n<p>  And amazingly, it actually worked.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once your average GUI program picks itself up off the ground, control begins at your WinMain function. 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, [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-38873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Once your average GUI program picks itself up off the ground, control begins at your WinMain function. 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, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=38873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38873\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=38873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=38873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=38873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}