{"id":3423,"date":"2011-02-18T06:52:00","date_gmt":"2011-02-18T06:52:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2011\/02\/18\/mfc-application-instance-control\/"},"modified":"2019-02-18T18:45:13","modified_gmt":"2019-02-18T18:45:13","slug":"mfc-application-instance-control","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/mfc-application-instance-control\/","title":{"rendered":"MFC Application Instance Control"},"content":{"rendered":"<p>An interesting question, asked days ago in one of <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/category\/visualc\">our C++ forums<\/a>, was the following:<\/p>\n<ul>\n<li><a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/vcmfcatl\/thread\/8c035e77-3fed-4584-ad08-8c8da14bcfc1\">How to Run only one instance of MFC application??<\/a><\/li>\n<\/ul>\n<p>A possible approach, based in <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms684266(VS.85).aspx\">mutex (mutual exclusion) objects<\/a>, was posted a few hours later. In the proposed schema you declare a mutex object inside the MFC application class (i.e. the class header):<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1761bbbf-5035-46dc-9c38-c1d7df906cc2\" style=\"padding-bottom: 0px;margin: 0px;padding-left: 0px;padding-right: 0px;float: none;padding-top: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #ddd;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"color:#008000\">\/\/ MFC application class declaration<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">class<\/span> CYourAppClass::CWinApp<\/li>\n<li>{<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;HANDLE m_Mutex_h;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;UINT m_WinMsg_ui; <\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<\/li>\n<li style=\"background: #f3f3f3\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>When you define the MFC application <span style=\"font-family: Courier New\">InitInstance()<\/span> method, you attempt to create the mutex -whose creation will succeed the first time, fail afterward.-<\/p>\n<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:629f4c53-1529-4aae-92da-90a74c25111d\" style=\"padding-bottom: 0px;margin: 0px;padding-left: 0px;padding-right: 0px;float: none;padding-top: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #ddd;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li>BOOL CYourAppclass::InitInstance()<\/li>\n<li style=\"background: #f3f3f3\">{<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;m_WinMsg_ui=RegisterWindowMessage(_T(<span style=\"color:#a31515\">&#8220;ONNMESSAGE&#8221;<\/span>));<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;m_Mutex_h=::CreateMutex(NULL, FALSE, STR_VIEWUTILITYMUTEX);<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">if<\/span> ((m_Mutex_h!=NULL)&amp;&amp;(GetLastError()!=ERROR_ALREADY_EXISTS))<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;{<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#008000\">\/\/ App is NOT running twice<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CYourDlg dlg;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pMainWnd = &amp;dlg;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INT_PTR nResponse = dlg.DoModal();<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">else<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;{<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#008000\">\/\/ App is running twice send message to running app, <\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#008000\">\/\/ the app will now know a 2nd instance was started<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;::SendMessage(HWND_BROADCAST, m_WinMsg_ui, 0, 0); <\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#008000\">\/\/ more initialization follows<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<\/li>\n<li style=\"background: #f3f3f3\">}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>About the <span style=\"font-family: Courier New\">else<\/span>, <a href=\"http:\/\/social.msdn.microsoft.com\/profile\/bordon\/?type=forum&amp;referrer=http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/vcmfcatl\/thread\/8c035e77-3fed-4584-ad08-8c8da14bcfc1\">the solution proponent (Bordon)<\/a> said the following<\/p>\n<blockquote>\n<p><em>The &#8220;<\/em><span style=\"font-family: Courier New\">SendMessage<\/span><em>&#8221; is only needed if you want that the running application knows a 2nd instance was started. You send a registered windows message to your running application, and your running application will receive this message and knows that a 2nd instance was started. You can create i.e. code in your message handler to bring your app to the front.<\/em><\/p>\n<\/blockquote>\n<p>Interesting. Was marked as solution by the original guy so it seems it worked. Would you have recommended another approach?<\/p>\n<p><span style=\"background-color: #ffffff\"><span style=\"color: #ff0000\"><strong>UPDATE:<\/strong> Please check in the comments section an entry posted by MS MFC expert Pat Brenner about this issue.<\/span><\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An interesting question, asked days ago in one of our C++ forums, was the following: How to Run only one instance of MFC application?? A possible approach, based in mutex (mutual exclusion) objects, was posted a few hours later. In the proposed schema you declare a mutex object inside the MFC application class (i.e. the [&hellip;]<\/p>\n","protected":false},"author":293,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[10,98,99],"class_list":["post-3423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-mfc","tag-mutex-objects","tag-synchronization"],"acf":[],"blog_post_summary":"<p>An interesting question, asked days ago in one of our C++ forums, was the following: How to Run only one instance of MFC application?? A possible approach, based in mutex (mutual exclusion) objects, was posted a few hours later. In the proposed schema you declare a mutex object inside the MFC application class (i.e. the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/293"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=3423"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=3423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=3423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=3423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}