{"id":10573,"date":"2011-05-25T07:00:00","date_gmt":"2011-05-25T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/05\/25\/winmain-is-just-the-conventional-name-for-the-win32-process-entry-point\/"},"modified":"2011-05-25T07:00:00","modified_gmt":"2011-05-25T07:00:00","slug":"winmain-is-just-the-conventional-name-for-the-win32-process-entry-point","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110525-00\/?p=10573","title":{"rendered":"WinMain is just the conventional name for the Win32 process entry point"},"content":{"rendered":"<p>\n<code>WinMain<\/code> is\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms633559.aspx\">\nthe conventional name for the user-provided entry point<\/a>\nin a Win32 program.\nJust like in 16-bit Windows, where the\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/12\/03\/6644060.aspx\">\ncomplicated entry point requirements were converted by language-provided\nstartup code into a call to the the user&#8217;s <code>WinMain<\/code> function<\/a>,\nthe language startup code for 32-bit programs also does the work of\nconverting the raw entry point into something that calls\n<code>WinMain<\/code>\n(or <code>wWinMain<\/cODE> or <code>main<\/code> or <code>_wmain<\/code>).\n<\/p>\n<p>\nThe raw entry point for 32-bit Windows applications has a much simpler\ninterface than the crazy 16-bit entry point:\n<\/p>\n<pre>\nDWORD CALLBACK RawEntryPoint(void);\n<\/pre>\n<p>\nThe operating system calls the function with no parameters,\nand the return value (if the function ever returns) is passed\nto the <code>ExitThread<\/code> function.\nIn other words, the operating system calls your entry point like this:\n<\/p>\n<pre>\n...\n  ExitThread(RawEntryPoint());\n  \/*NOTREACHED*\/\n<\/pre>\n<p>\nWhere do the parameters to <code>WinMain<\/code> come from,\nif they aren't passed to the raw entry point?\n<\/p>\n<p>\nThe language startup code gets them by asking the operating system.\nThe instance handle for the executable comes from\n<code>GetModuleHandle(NULL)<\/code>,\nthe command line comes from <code>GetCommandLine<\/code>,\nand the <code>nCmdShow<\/code> comes from\n<code>GetStartupInfo<\/code>.\n(As we saw before,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/06\/15\/156022.aspx\">\nthe <code>hPrevInstance<\/code> is always <code>NULL<\/code><\/a>.)\n<\/p>\n<p>\nIf you want to be hard-core, you can program to the raw entry point.\nMind you, other parts of your program may rely upon the work that the\nlanguage startup code did before calling your <code>WinMain<\/code>.\nFor example, the C++ language startup code will run global constructors\nbefore calling into <code>WinMain<\/code>,\nand both C and C++ will initialze the so-called <i>security cookie<\/i>\nused as part of\n<a HREF=\"http:\/\/blogs.msdn.com\/michael_howard\/archive\/2007\/04\/03\/hardening-stack-based-buffer-overrun-detection-in-vc-2005-sp1.aspx\">\nstack buffer overrun detection<\/a>.\nBypass the language startup code at your peril.\n<\/p>\n<p>\n<b>Bonus chatter<\/b>:\nNotice that if you choose to return from your entry point function,\nthe operating system passes the return value to <code>ExitThread<\/code>\nand not <code>ExitProcess<\/code>.\nFor this reason, you typically don't want to return from your raw\nentry point but instead want to call <code>ExitProcess<\/code> directly.\nOtherwise, if there are background threads hanging around, they will\nprevent your process from exiting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WinMain is the conventional name for the user-provided entry point in a Win32 program. Just like in 16-bit Windows, where the complicated entry point requirements were converted by language-provided startup code into a call to the the user&#8217;s WinMain function, the language startup code for 32-bit programs also does the work of converting the raw [&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":[25],"class_list":["post-10573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>WinMain is the conventional name for the user-provided entry point in a Win32 program. Just like in 16-bit Windows, where the complicated entry point requirements were converted by language-provided startup code into a call to the the user&#8217;s WinMain function, the language startup code for 32-bit programs also does the work of converting the raw [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10573","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=10573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10573\/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=10573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}