{"id":1954,"date":"2013-10-18T08:24:42","date_gmt":"2013-10-17T20:24:42","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1954"},"modified":"2013-10-18T11:17:00","modified_gmt":"2013-10-17T23:17:00","slug":"hands-off-my-application-another-dpr-trick","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1954\/","title":{"rendered":"Hands Off My Application! (Another DPR Trick)"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>In a comment on my previous post, <a href=\"https:\/\/www.deltics.co.nz\/blog\/posts\/1945\/comment-page-1#comment-15082\">David Heffernan noted that the IDE messes around with other parts of the application DPR<\/a> in addition to the uses list.  He is right.  Another area that the IDE likes to muck about with is the code in the DPR <strong>begin .. end<\/strong> itself !<\/p>\n<p>Fortunately there is a simple way to avoid that also.<\/p>\n<p><!--more--><\/p>\n<h2>First, Recognise the Problem<\/h2>\n<p>Let&#8217;s see just one of the things that IDE does in this area (version used for this exercise: Delphi 2010.  Steps may vary slightly in any different version but should be easy enough to follow).<\/p>\n<p>Create a new VCL Forms application and View Project Source (the DPR).  You should see something like this:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nprogram Project1;\r\n\r\nuses\r\n  Forms,\r\n  Unit1 in 'Unit1.pas' {Form1};\r\n\r\n{$R *.res}\r\n\r\nbegin\r\n  Application.Initialize;\r\n  Application.MainFormOnTaskbar := True;\r\n  Application.CreateForm(TForm1, Form1);\r\n  Application.Run;\r\nend.\r\n<\/pre>\n<p>Now open the <strong>Project Options<\/strong> dialog, select <strong>Application<\/strong> settings and enter something as the <strong>Application Title<\/strong>:<\/p>\n<figure id=\"attachment_1955\" aria-describedby=\"caption-attachment-1955\" style=\"width: 866px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?ssl=1\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?resize=640%2C204&#038;ssl=1\" alt=\"Setting the Application Title\" width=\"640\" height=\"204\" class=\"size-full wp-image-1955\" srcset=\"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?w=866&amp;ssl=1 866w, https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?resize=300%2C95&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-18-at-09.01.47.png?resize=500%2C159&amp;ssl=1 500w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/a><figcaption id=\"caption-attachment-1955\" class=\"wp-caption-text\">Setting the Application Title<\/figcaption><\/figure>\n<p>Press OK<\/p>\n<p>Before your very eyes the project DPR will change to:<\/p>\n<pre class=\"brush: delphi; highlight: [12]; title: ; notranslate\" title=\"\">\r\nprogram Project1;\r\n\r\nuses\r\n  Forms,\r\n  Unit1 in 'Unit1.pas' {Form1};\r\n\r\n{$R *.res}\r\n\r\nbegin\r\n  Application.Initialize;\r\n  Application.MainFormOnTaskbar := True;\r\n  Application.Title := 'Right Honourable Application';\r\n  Application.CreateForm(TForm1, Form1);\r\n  Application.Run;\r\nend.\r\n<\/pre>\n<p>This is just the first example of where the IDE re-writes your DPR code.  There are others (changing the Main Form and\/or the list of auto-created forms, for example), but many if not all of them can be defeated by one simple trick.  A clue to that trick can be found by removing the line from the DPR that sets <strong>Application.Title<\/strong> and going back to the <strong>Project Options<\/strong>.<\/p>\n<p><em>The <strong>Application Title<\/strong> is no longer set!<\/em><\/p>\n<p>Not only does the IDE apply these settings by re-writing your DPR code, it actually seems to get the current setting by reading the code too !<\/p>\n<p>For these features, the IDE relies on being able to recognise certain things in the code of your DPR, driven by the &#8220;boiler-plate&#8221; code created by the project wizards that it expects to find.  In this case, code that uses the <strong>Application<\/strong> variable (from the Forms unit).<\/p>\n<h2>If You Can&#8217;t Beat Them, Hide<\/h2>\n<p>Obviously you can put your own code in this <strong>begin\/end<\/strong> area.  Very often you have to, to perform initialisation or checks before launching into your UI.<\/p>\n<p>But precisely because you <em>can<\/em> add code the IDE does do it&#8217;s best to identify only those parts in that code which it feels it <em>should<\/em> be able to modify.  If the IDE cannot find the required &#8220;markers&#8221; that identify the pieces that the IDE wishes to modify then rather than risk doing damage it simply keeps itself to itself.<\/p>\n<p>So all we have to do is remove those markers.<\/p>\n<p>We can do this very simply by declaring our own variable to use in place of <strong>Application<\/strong>:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nvar\r\n  app: TApplication;\r\nbegin\r\n  app := Application;\r\n\r\n  app.Initialize;\r\n  app.MainFormOnTaskbar := True;\r\n  app.CreateForm(TForm1, Form1);\r\n  app.Run;\r\nend.\r\n<\/pre>\n<p>Once you&#8217;ve done this I don&#8217;t think the IDE will ever trouble your DPR again, at least not the parts between the <strong>begin<\/strong> and <strong>end<\/strong>.  Which is to say that in almost 20 years of Delphi, after making such a change to the DPR I don&#8217;t recall a single instance of the IDE ever trying to make unwelcome changes in this area.<\/p>\n<p>It does mean that you have to avoid using certain IDE features.<\/p>\n<p>They either simply won&#8217;t work or will complain.  Try changing the application title now and Delphi 2010 will complain that it cannot find a valid &#8220;<em>Application.CreateForm<\/em>&#8220;.  But more importantly, <em>it won&#8217;t try to make any changes in the DPR either<\/em>!<\/p>\n<p>As I mentioned, the other key area that this &#8220;breaks&#8221; is the <strong>Main Form<\/strong> and <strong>Auto-Create Forms list<\/strong> in the project options.<\/p>\n<h2>With Great Power Comes Great Responsibility<\/h2>\n<p>By denying the IDE the capability to manage your DPR code you have to take on the responsibility of managing things yourself.  But then, since the whole point is to avoid problems that the IDE management of this code can cause, you wouldn&#8217;t have wanted to continue using them anyway.  \ud83d\ude42<\/p>\n<p>In applications where you don&#8217;t need any bespoke changes in the DPR code you can of course just leave things as they are.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> In a comment on my previous post, David Heffernan noted that the IDE messes around with other parts of the application DPR in addition to the uses list. He is right. Another area that the IDE likes to muck about with is the code in the DPR begin .. end itself ! Fortunately there is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[292,246],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-vw","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1945,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1945\/","url_meta":{"origin":1954,"position":0},"title":"Unit Aliases and Build Configurations","date":"17 Oct 2013","format":false,"excerpt":"Over on stackoverflow, Ann Gossard had a question about using an $ifdef in the project (DPR) uses list to use one unit in preference over another, specifically in debug builds. In this situation, it immediately occurred to me that unit aliases might be creatively deployed. What follows is a slightly\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-17-at-15.25.51.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1985,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1985\/","url_meta":{"origin":1954,"position":1},"title":"Build Automation With Train","date":"29 Oct 2013","format":false,"excerpt":"A comment from Kevin P brought a build automation tool to my attention this evening, called Train. Train is a JavaScript based build automation tool from a little company called RemObjects. It is written in Oxygene but the API provides specific support for Delphi. It's an open source project and\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-29-at-23.36.22-.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1397,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1397\/","url_meta":{"origin":1954,"position":2},"title":"Qualified Enum Reference That Fails to Compile in XE4 (and rightly so)","date":"01 May 2013","format":false,"excerpt":"Running through some of my code last night, putting them through the new XE4 compiler, threw up a real oddity: Some code that used to compile just fine, which no longer compiles in XE4 and which should not have compiled before! It's an odd one, because the code previously compiled\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1372,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1372\/","url_meta":{"origin":1954,"position":3},"title":"DPROJ Woes","date":"23 Apr 2013","format":false,"excerpt":"I spent the best part of half a day last week trying to get to the bottom of a very strange issue on a project I was working on. I had just created a new unit in the project and had reached a point where I needed to add that\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2144,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2144\/","url_meta":{"origin":1954,"position":4},"title":"Smoketest 1.0 &#8211; Release and Be Damned!","date":"14 Nov 2013","format":false,"excerpt":"As I have been promising for some time (quite literally 5 years (!), I am ashamed to admit) I am finally unclenching and releasing the Smoketest framework into the wild, ready or not. The code is published and will continue to be updated in a github repository. Documentation is still\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-11-14-at-19.51.57-.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2878,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2878\/","url_meta":{"origin":1954,"position":5},"title":"Azure DevOps &#8211; Template for Builds + Running Tests and Capturing Results","date":"12 Sep 2019","format":false,"excerpt":"A more complete build script, re-usable in the form of a template, that caters for different Delphi versions, combined with a demonstration of running unit tests and capturing results for reporting and analysis in Azure DevOps Pipelines.","rel":"","context":"In &quot;automation&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2019-09-12-at-14.25.20.jpg?fit=386%2C386&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1954"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1954"}],"version-history":[{"count":7,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1954\/revisions"}],"predecessor-version":[{"id":1963,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1954\/revisions\/1963"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}