{"id":2199,"date":"2017-02-06T15:23:16","date_gmt":"2017-02-06T14:23:16","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=2199"},"modified":"2017-02-06T15:23:16","modified_gmt":"2017-02-06T14:23:16","slug":"microsoft-visual-c-how-to-call-winmain-from-a-console-apps-main-method-wie-man-die-winmain-methode-von-einer-konsolenanwendung-aus-aufruft","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=2199","title":{"rendered":"Microsoft Visual C++ : How to call WinMain() from a console apps main()-method \/ Wie man die WinMain-Methode von einer Konsolenanwendung aus aufruft"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Eine C++ Anwendung hat als Konsolenanwendung den Einstiegspunkt<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nint main(void) {...}\r\n<\/pre>\n<p>aber als Win32-Anwendung den Einstiegspunkt<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)  {...}\r\n<\/pre>\n<p>Es wird nach einer M\u00f6glichkeit gesucht, die WinMain-Methode von einer Konsolen-Anwendung aus aufzurufen, um eine Hauptfenster in der Konsolenanwendung zu generieren.<\/p>\n<h1>Ansatz<\/h1>\n<p>MSDN definiert die WinMain()-Methode folgenderma\u00dfen:<\/p>\n<ul>\n<li><em>hInstance<\/em> ist ein Element, das als \u201eHandle f\u00fcr eine Instanz\u201c oder \u201eHandle f\u00fcr ein Modul\u201c bezeichnet wird. Das Betriebssystem identifiziert mithilfe dieses Werts die ausf\u00fchrbare Datei (EXE), wenn diese in den Arbeitsspeicher geladen wird. Das Instanzhandle wird f\u00fcr bestimmte Windows-Funktionen ben\u00f6tigt\u2014z.\u00a0B. um Symbole oder Bitmaps zu laden.<\/li>\n<li><em>hPrevInstance<\/em> hat keine Bedeutung. Es wurde in 16-Bit-Windows verwendet, es ist aber jetzt stets 0 (null).<\/li>\n<li><em>pCmdLine<\/em> enth\u00e4lt die Befehlszeilenargumente als Unicode-Zeichenfolge.<\/li>\n<li><em>nCmdShow<\/em> ist ein Flag, das angibt, ob das Hauptanwendungsfenster minimiert, maximiert oder in Normalgr\u00f6\u00dfe angezeigt wird.<\/li>\n<\/ul>\n<p>Die WinMain()-Methode kann von der main()-Anwendung aufgerufen werden, wenn man ein hInstance Handle bekommt. Dies geht folgerma\u00dfen:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nGetModuleHandle(NULL)\r\n<\/pre>\n<h1>L\u00f6sung &#8211; Solution<\/h1>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ HelloWorld.cpp : Definiert den Einstiegspunkt f\u00fcr die Konsolenanwendung.\r\n\/\/\r\n\r\n#include &quot;stdafx.h&quot;\r\n#include &lt;windows.h&gt; \r\n\r\n\/\/ Das hier braucht man nur wenn man die test.wav abspielen will\r\n\/\/#pragma comment(lib,&quot;winmm.lib&quot;)  \/\/f\u00fcr MSV C++   \r\n\r\nconst char g_szClassName&#x5B;] = &quot;myWindowClass&quot;;\r\n\r\n\/\/ Step 4: the Window Procedure\r\nLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\r\n{\r\n\tswitch (msg)\r\n\t{\r\n\tcase WM_CLOSE:\r\n\t\tDestroyWindow(hwnd);\r\n\t\tbreak;\r\n\tcase WM_DESTROY:\r\n\t\tPostQuitMessage(0);\r\n\t\tbreak;\r\n\tdefault:\r\n\t\treturn DefWindowProc(hwnd, msg, wParam, lParam);\r\n\t}\r\n\treturn 0;\r\n}\r\n\r\nint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)\r\n{\r\n\tWNDCLASSEX wc;\r\n\tHWND hwnd;\r\n\tMSG Msg;\r\n\r\n\t\/\/Step 1: Registering the Window Class\r\n\twc.cbSize = sizeof(WNDCLASSEX);\r\n\twc.style = 0;\r\n\twc.lpfnWndProc = WndProc;\r\n\twc.cbClsExtra = 0;\r\n\twc.cbWndExtra = 0;\r\n\twc.hInstance = hInstance;\r\n\twc.hIcon = LoadIcon(NULL, IDI_APPLICATION);\r\n\twc.hCursor = LoadCursor(NULL, IDC_ARROW);\r\n\twc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);\r\n\twc.lpszMenuName = NULL;\r\n\twc.lpszClassName = (LPCWSTR) g_szClassName;\r\n\twc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);\r\n\r\n\tif (!RegisterClassEx(&amp;wc))\r\n\t{\r\n\t\tMessageBox(NULL, L&quot;Window Registration Failed!&quot;, L&quot;Error!&quot;,\r\n\t\t\tMB_ICONEXCLAMATION | MB_OK);\r\n\t\treturn 0;\r\n\t}\r\n\r\n\t\/\/ Step 2: Creating the Window\r\n\thwnd = CreateWindowEx(\r\n\t\tWS_EX_CLIENTEDGE,\r\n\t\t(LPCWSTR) g_szClassName,\r\n\t\tL&quot;Eine Mischung aus Console und Window App&quot;,\r\n\t\tWS_OVERLAPPEDWINDOW,\r\n\t\tCW_USEDEFAULT, CW_USEDEFAULT, 240, 120,\r\n\t\tNULL, NULL, hInstance, NULL);\r\n\r\n\tif (hwnd == NULL)\r\n\t{\r\n\t\tMessageBox(NULL, L&quot;Window Creation Failed!&quot;, L&quot;Error!&quot;,\r\n\t\t\tMB_ICONEXCLAMATION | MB_OK);\r\n\t\treturn 0;\r\n\t}\r\n\r\n\tShowWindow(hwnd, nCmdShow);\r\n\tUpdateWindow(hwnd);\r\n\r\n\t\/\/ Step 3: The Message Loop\r\n\twhile (GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0)\r\n\t{\r\n\t\tTranslateMessage(&amp;Msg);\r\n\t\tDispatchMessage(&amp;Msg);\r\n\t}\r\n\treturn Msg.wParam;\r\n}\r\n\r\n\r\nint main()\r\n{\r\n\t\/\/ Das hier spielt eine Datei test.wav im gleichen Verzeichnis\r\n\t\/\/PlaySound(L&quot;test.wav&quot;, NULL, SND_ASYNC);\r\n\t\r\n\tWinMain(GetModuleHandle(NULL), 0, 0, 1);\r\n\r\n\tprintf(&quot;Se hams Fenster geschlossen!&quot;);\r\n\tgetchar();\r\n\r\n\treturn 0;\r\n}\r\n\r\n<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D2199&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Problem Eine C++ Anwendung hat als Konsolenanwendung den Einstiegspunkt int main(void) {&#8230;} aber als Win32-Anwendung den Einstiegspunkt int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {&#8230;} Es wird nach einer M\u00f6glichkeit gesucht, die WinMain-Methode von einer Konsolen-Anwendung aus aufzurufen, um eine Hauptfenster in der Konsolenanwendung zu generieren. Ansatz MSDN definiert die WinMain()-Methode folgenderma\u00dfen: &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=2199\" class=\"more-link\"><span class=\"screen-reader-text\">Microsoft Visual C++ : How to call WinMain() from a console apps main()-method \/ Wie man die WinMain-Methode von einer Konsolenanwendung aus aufruft<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[35,3],"tags":[],"class_list":["post-2199","post","type-post","status-publish","format-standard","hentry","category-c-programmierung","category-programmierung"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4yGeN-zt","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2199"}],"version-history":[{"count":1,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2199\/revisions"}],"predecessor-version":[{"id":2200,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2199\/revisions\/2200"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}