{"id":186,"date":"2019-06-20T15:46:53","date_gmt":"2019-06-20T15:46:53","guid":{"rendered":"http:\/\/basicwebsitesolutions.com\/blog\/?p=186"},"modified":"2019-06-20T15:46:53","modified_gmt":"2019-06-20T15:46:53","slug":"delphi-create-a-single-instance-of-your-app","status":"publish","type":"post","link":"https:\/\/basicwebsitesolutions.com\/blog\/2019\/06\/20\/delphi-create-a-single-instance-of-your-app\/","title":{"rendered":"Delphi &#8211; Create a single instance of your app"},"content":{"rendered":"<p>Hi guys. After searching for a while on how to lock and limit the app clicking. And this is now possible.<\/p>\n<p>Here is the code for the main project file<\/p>\n<blockquote><p>program Project2;<\/p>\n<p>&nbsp;<\/p>\n<p>uses<\/p>\n<p>Forms, Windows, Messages, Dialogs,<\/p>\n<p>Unit1 in &#8216;Unit1.pas&#8217; {Form1};<\/p>\n<p>&nbsp;<\/p>\n<p>{$R *.res}<\/p>\n<p>&nbsp;<\/p>\n<p>function CreateSingleInstance(const InstanceName: string): boolean;<\/p>\n<p>var<\/p>\n<p>MutexHandle: THandle;<\/p>\n<p>begin<\/p>\n<p>MutexHandle := CreateMutex(nil, false, PChar(InstanceName));<\/p>\n<p>\/\/ if MutexHandle created check if already exists<\/p>\n<p>if (MutexHandle &lt;&gt; 0) then<\/p>\n<p>begin<\/p>\n<p>if GetLastError = ERROR_ALREADY_EXISTS then<\/p>\n<p>begin<\/p>\n<p>Result := false;<\/p>\n<p>CloseHandle(MutexHandle);<\/p>\n<p>end<\/p>\n<p>else Result := true;<\/p>\n<p>end<\/p>\n<p>else Result := false;<\/p>\n<p>end;<\/p>\n<p>&nbsp;<\/p>\n<p>var<\/p>\n<p>MyInstanceName: string;<\/p>\n<p>begin<\/p>\n<p>MyInstanceName := &#8216;Project2&#8217;;<\/p>\n<p>Application.Initialize;<\/p>\n<p>\/\/ Initialize MyInstanceName here<\/p>\n<p>if CreateSingleInstance(MyInstanceName) then<\/p>\n<p>begin<\/p>\n<p>\/\/ Form creation<\/p>\n<p>Application.CreateForm(TForm1, Form1);<\/p>\n<p>Application.Run;<\/p>\n<p>end<\/p>\n<p>else Application.Terminate;<\/p>\n<p>end.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Hi guys. After searching for a while on how to lock and limit the app clicking. And this is now possible. Here is the code for the main project file program Project2; &nbsp; uses Forms, Windows, Messages, Dialogs, Unit1 in &#8216;Unit1.pas&#8217; {Form1}; &nbsp; {$R *.res} &nbsp; function CreateSingleInstance(const InstanceName: string): boolean; var MutexHandle: THandle; begin &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/basicwebsitesolutions.com\/blog\/2019\/06\/20\/delphi-create-a-single-instance-of-your-app\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Delphi &#8211; Create a single instance of your app&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1],"tags":[108,36,107,109,106],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-delphi-and-interbase","category-life-as-a-programmer","tag-app","tag-delphi","tag-instance","tag-mutex","tag-single"],"_links":{"self":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":3,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/posts\/186\/revisions\/190"}],"wp:attachment":[{"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/basicwebsitesolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}