The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for the ‘Delphi’ Category

web services – Accessing the original TWebRequest object in a Delphi SOAP Server – Stack Overflow

Posted by jpluimers on 2019/05/22

Thanks Ondrej Kelle for answering this:

uses
  System.SysUtils,
  Web.HTTPApp,
  Soap.WebBrokerSOAP;

function TTest.CallMe: string;
var
  WebDispatcher: IWebDispatcherAccess;
begin
  Result := '';
  if Supports(GetSOAPWebModule, IWebDispatcherAccess, WebDispatcher) then
    Result := Format('You are calling me from: %s', [WebDispatcher.Request.RemoteIP]);
end;

Source: [WayBack] web services – Accessing the original TWebRequest object in a Delphi SOAP Server – Stack Overflow

–jeroen

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development | Leave a Comment »

Delphi XE8 introduced the dependency directive, but it’s not for Windows

Posted by jpluimers on 2019/05/21

Interesting thread that shows Delphi XE8 introduced a dependency directive: [WayBack] Just curious, in which version was the dependency directive introduced? As in procedure X; external ‘somelib’ dependecy ‘otherlib’; S… – Johan Bontes – Google+

It’s documented as of XE8: [WayBack] Procedures and Functions (Delphi) – RAD Studio: Specifying Dependencies of the Library

The earliest use I could find is in XE4 source C:\Program Files (x86)\Embarcadero\RAD Studio\11.0\source\IBX\IBIntf.pas

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

The Initialization-Block of a unit that is part of a package. Is it run as part of DLLMain?

Posted by jpluimers on 2019/05/16

The interesting question a while back [WayBack] The Initialization-Block of a unit that is part of a package. Is it run as part of DLLMain? – Alexander Benikowski – Google+ has a simple TL;DR answer: “it depends” on the actual usage of those units.

Way more elaborate, as I dislike language stuff that you need to track down by trial and error what is actually used:

Well, seems so(in our case). Rootcall which triggers the initialization is:
ntdll.ldrLoadDLL
I did not mention, that the BPL is used by a DLL(DLL links against package) which means the package is loaded/initialized by the Os when the DLLMain runs. Odd combination but that seems to be the culprit here.

David Heffernan
Yes, it is triggered from DllMain. And yes, this has massive consequences for what can and cannot be done in initialization sections.

Alexander Benikowski
+David Heffernan in my case. When a packages unit is already initialized by being used from an Exe(which links to the Package), it is not from within a DLLMain.
In my case, both the Application and the dll it loads, both link to the same package. But the unit in question is unused until the DLL is loaded.

Uwe Raabe
The docs for InitializePackage say it calls the initialization parts of all contained units in the package – not only the used ones.

David Heffernan
+Alexander Benikowski​ In that scenario we have load time linking and I guess the package framework handles it differently.

David Millington
Related: don’t forget that class constructors and destructors effectively run in the initialization and finalization sections too. Restrictions or side effects apply there too. docwiki.embarcadero.com – Methods (Delphi) – RAD Studio

Stefan Glienke
+Uwe Raabe Which is not being done when you use it as runtime package. Only initialization sections from unit being used are being run. This caused us several problems in the past where 2 modules (A being the host application exe and B being some DLL that gets loaded at a later point via LoadLibrary) were using a runtime package but only B used a particular unit from the package which caused the initialization code for that unit being executed when B was loaded and hence being executed in the context of the dllmain of B.

The usual solution is then to put those units into some dummy unit forcing the initialization of that unit to be run in A.

Another solution according to your statement could be to call InitializePackage on all the used runtime packages – and there the question is: couldn’t the RTL do that somehow?

+Stefan Glienke I am not sure if that is desirable when done unconditionally. Even when compiled with packages I wouldn’t expect units to be initialized which aren’t actually used. That would perhaps change the behavior of the application depending on whether it is compile with runtime packages or without.
The case is different with dynamic loaded packages. The units in there are obviously not directly used in the first place. As no one can know which units of such a package are used or not, initializing all of them seems like a viable decision.
Of course there will be situations where your proposed behavior might come in handy, but I doubt that this will be a fit for all.

Source: The Initialization-Block of a unit that is part of a package. Is it run as pa…

Related:

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

How to Design Early Returns in C++ (Based on Procedural Programming) – Fluent C++

Posted by jpluimers on 2019/05/15

One more thing to take away from Procedural Programming: It’s Back? It Never Went Away – Kevlin Henney [ACCU 2018] – YouTube was explained in [WayBack] How to Design Early Returns in C++ (Based on Procedural Programming) – Fluent C++.

Though in C++, it applies to all programming languages that stem from a procedural background (Pascal, C#, Java, golang, to name just a few).

The article is about keeping an if/else-if/else tree, even when they can be removed becomes some of their bodies perform an early return, as

In C++, as well as in other languages, the return keyword has two responsibilities:

  • interrupting control flow,
  • yielding a value.

It basically comes down to this argument:

Essentially, the argument for Code is that you need to know less to understand the structure of the code.

Indeed, if we fold away the contents of the if statements, Code becomes this:

The structure of the code is very clear. There are 4 different paths based on the year, they’re independent from each other, and each path will determine the boolean result of the function (if it doesn’t throw an exception).

Now let’s see how Code looks like when we fold away the if statements:

And now we know much less. Do the if statements contain a return? Maybe.

Do they depend on each other? Potentially.

Do some of them rely on the last return false of the function? Can’t tell.

With Code you need to look inside of the if statement to understand the structure of the function. For that reason, Code requires a reader to know less to understand the structure. It gives away information more easily than Code .

–jeroen

via [WayBack] Kevlin Henney – Google+: How to Design Early Returns in C++ (Based on Procedural Programming) – Fluent C++

Posted in .NET, C, C#, C++, Conference Topics, Conferences, Delphi, Development, Event, Software Development | Leave a Comment »

Need to try this: overloaded default properties

Posted by jpluimers on 2019/05/15

[Archive.is] Need to try this: … multiple default index properties having the same name …getters can be overloads … resolve …by type signature … – Thomas Mueller (dummzeuch) – Google+, thanks to marck for this brilliantly simple example:

private
  function GetColumnValue(const ColumnName: string): string; overload;
  function GetColumnValue(Index: Integer): string; overload;
  procedure SetColumnValue(Index: Integer; const Value: string);
public
  property Values[const ColumnName: string]: string read GetColumnValue; default;
  property Values[ColumnIndex: Integer]: string read GetColumnValue write SetColumnValue; default;
end;

This means:

  • you can have multiple default indexor properties
  • the multiple indexor properties can have the same name e.g., Values
  • the properties getters can be overloads (i.e. have the same name) e.g., GetColumnValue
  • Delphi will resolve the overloads by type signature

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

More Delphi debug visualizers

Posted by jpluimers on 2019/05/07

I hope that by now they are available for more Delphi versions:

Some other posts around the Debug Visualiser topic:

–jeroen

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development | Leave a Comment »

Registering/unregseting Windows file associations with Delphi

Posted by jpluimers on 2019/05/02

As I need this one day: [WayBack] Just in case I need this again… >Free library DSiWin32 … now implements this as DSiRegisterUserFileAssoc and DSiUnregisterUserFileAssoc <… – Thomas Mueller (dummzeuch) – Google+

Note file has moved from WayBack: gpdelphiunits.googlecode.com/svn/trunk/src/DSiWin32.pas to [WayBack] OmniThreadLibrary/DSiWin32.pas at master · gabr42/OmniThreadLibrary · GitHub because Google code has shut down.

There still is [WayBack] Google Code Archive – Long-term storage for Google Code Project Hosting: gpdelphiunits, but maintenance is now part of [WayBack] GitHub – gabr42/OmniThreadLibrary: A simple and powerful multithreading library for Delphi

Related: [WayBack] delphi – How to associate a program with a file type, but only for the current user? – Stack Overflow

–jeroen

Posted in Delphi, Development, Software Development | 1 Comment »

GExperts: searching for case-insensitive “T*List.Create” but not “TStringList.Create”

Posted by jpluimers on 2019/05/02

Just learned that partial exclusion can be done with the case-insensitive GExperts Grep Search like this:

T[^s][^t][^r][^i][^n][^g].*List.*\.Create

This will skip TStringList.Create, but matches TMyList.Create.

I’d rather have done something like this, but the Delphi RegEx does not support negative lookbehind:

^ *[a-zA-Z0-9_]* *: *T(<!string)[a-zA-Z0-9_]*ListO? *;$

So the alternative is to search for this:

^ *[a-zA-Z0-9_]* *: *T[a-zA-Z0-9_]*ListO? *;$

then exclude all the case insensitive TStringList entries from it, however GExperts did not support that at the time of writing.

This is an intermediate that works for some of the times:

^ *[a-zA-Z0-9_]* *: *T[^s][^t][^r][^i][^n][^g][a-zA-Z0-9_]*ListO? *;$

–jeroen

^ *[a-zA-Z0-9_]* *: *T[^s][^t][^r][^i][^n][^g][a-zA-Z0-9_]*ListO? *$;
^ *[a-zA-Z0-9_]*: .T[a-zA-Z0-9_]*ListO? *;$;
^ *[a-zA-Z0-9_]* *: *T(?!string)[a-zA-Z0-9_]*ListO? *;$

Posted in Delphi, Development, GExperts, RegEx, Software Development | Leave a Comment »

Note that the Delphi superobject library has changed to “not maintained” in december 2018, has problems with large address aware

Posted by jpluimers on 2019/05/02

A while ago I found out the [WayBack] not maintained status · hgourvest/superobject@f1c42db · GitHub.

This means you should not use the [WayBack] superobject JSON library in Delphi any more: there won’t be any fixes.

Many people use it, especially because it used to be much more stable than the built-in JSON support of Delphi.

One breaking issue in superobject is the lack of large address space support: due to the pointer calculations in various places, it does not support pointers above the 2 gibibyte boundary as filed in the 2016 [WayBack] Issues with {$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE} · Issue #22 · hgourvest/superobject · GitHub

This gives problems in at least this case:

  • enabling {$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE} (in older Delphi 7 through 2006 also versions this was {$SetPEFlags $20})
  • using top-down memory allocation, for instance by:
    • a user setting HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Memory Management value AllocationPreference to hex value 00100000
    • using FastMM4 with the (default) {$define AlwaysAllocateTopDown} setting

Example registry file and batch file to enable top-down memory (reboot afterwards):

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management]
"AllocationPreference"=dword:00100000

Command to view:

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" | findstr "AllocationPreference"

Command to enable:

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v AllocationPreference /t REG_DWORD /d 00100000 /f

Command to disable:

reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v AllocationPreference /f

Large Address Aware is a nightmare

Be very very very careful with this, and by enabling Large Address Aware to your executables, as many times they can load 3rd party libraries that often are beyond your control.

Even if there is a slight chance that your code is being used with Large Address Aware enabled, then follow guidelines line in [WayBack] windows – Unit Testing for x86 LargeAddressAware compatibility – Stack Overflow

Summary of [WayBack] memory – Drawbacks of using /LARGEADDRESSAWARE for 32 bit Windows executables? – Stack Overflow:

blindly applying the LargeAddressAware flag to your 32bit executable deploys a ticking time bomb!

by setting this flag you are testifying to the OS:

yes, my application (and all DLLs being loaded during runtime) can cope with memory addresses up to 4 GB.
so don’t restrict the VAS for the process to 2 GB but unlock the full range (of 4 GB)”.

but can you really guarantee?
do you take responsibility for all the system DLLs, microsoft redistributables and 3rd-party modules your process may use?

Edit 20240628

Earlier this year, the SuperObject Delphi library got archived on GitHub. Definitely unmaintained: [Wayback/Archive] GitHub – hgourvest/superobject: This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

The XSuperObject library mentioned below in a comment has not been maintained for 4 years either ( [Wayback/Archive] GitHub – onryldz/x-superobject: Delphi Cross Platform Rapid JSON: “vkrapotkin Now ParseFromFile can read UTF8-BOM files (#136) 2d3ec01 · 2020-12-09”), so I wonder what alternatives are still available.

--jeroen

Posted in Conference Topics, Conferences, Delphi, Development, EKON, Event, Software Development | 4 Comments »

Delphi function result assignments before the function returns…

Posted by jpluimers on 2019/05/01

Eric Grange:

Actually it is not that assignments of function return values can happen “when the function raises an exception” but rather than they can happen before the function returns.

Note that this is not limited to large return types, it can also happen on reference counted types (string, dynamic array, variant and interface), though this is contextual as well…

Got bit by the interface thing a few months ago, an interface release was triggering an exception when the result was assigned, the call stack looked way out of synch with the code, so various compilation and and map file mismatch issues got investigated, before I dropped in asm view in the debugger, which made it all obvious.

I’ve quoted it in full as I’ve been bitten by this a few times as well, but never got to making a proper blog post on it.

Thanks Eric for phrasing this and David for bringing it up.

It actually has been the case since somewhere toward the end of the Turbo Pascal era.

Source: [WayBackThis program: {$APPTYPE CONSOLE} uses System.SysUtils; type TRec1 = r…

–jeroen

Posted in Delphi, Development, History, Pascal, Software Development, Turbo Pascal | Leave a Comment »