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

Delphi spring collections

Posted by jpluimers on 2020/12/02

[WayBack] Spring Collections I have a list of elements, there are, for example, 100 of them. List : IList; I want to get 5 values greater than 10 and … – Jacek Laskowski – Google+

Q

I have a list of elements, there are, for example, 100 of them.

List : IList<Integer>;

I want to get 5 values greater than 10 and I do it like this:

result: = List.Where(ValueIsGreatThan10).Take(5);

Will the “work loop” be executed a minimum number of times and if, for example, the first 5 values in the list will be greater than 5, then only the five will be checked? Or maybe the Where() loop will scan 100 elements, and Take() will return the first 5 results?

A (by Stefan Glienke)

Where and Take are streaming operators and only execute as much as required.

Also the operations have deferred execution. So your statement does not materialize any collection yet. Only if you iterate it will.

They are designed after the operators in .NET so the table from [WayBack] Classification of Standard Query Operators by Manner of Execution (C#) | Microsoft Docs applies. If you find any difference please report it.

Example:

var
  nums: IEnumerable<Integer>;
  i: Integer;
begin
  nums := TEnumerable.Range(1, 100).Where(
    function(const i: Integer): Boolean
    begin
      Writeln('checking: ', i);
      Result := i > 10;
    end
  ).Take(5);
  Writeln('query created');
  for i in nums do
    Writeln('got number: ', i);
end.

This code will print:

query created
checking: 1
checking: 2
checking: 3
checking: 4
checking: 5
checking: 6
checking: 7
checking: 8
checking: 9
checking: 10
checking: 11
got number: 11
checking: 12
got number: 12
checking: 13
got number: 13
checking: 14
got number: 14
checking: 15
got number: 15

–jeroen

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

Delphi Compiler Intrinsics can help you collapse generated code for generics a lot

Posted by jpluimers on 2020/12/01

On my reading list [WayBack] Delphi Corner Weblog: The current state of generics in Delphi because it explains in more detail why Delphi compiler generic type based intrinsics introduced some 5 years ago, but never been (accurately) documented, but listed at [WayBack] delphi – Undocumented intrinsic routines – Stack Overflow, are so important to help collapse decreasing code bloat:

  • Default
  • IsManagedType
  • HasWeakRef
  • GetTypeKind

There is also the TypeInfo which is somewhat documented on-line starting in the Delphi 2007 documentation:

 

And some non-generic intrinsics that are still undocumented:

  • IsConstValue
  • ReturnAddress

Spring4D already has been making use of this for a few years, and you can too!

Via [WayBack] rvelthuis.blogspot.com/2018/10/the-current-state-of-generics-in-delphi.html – Jacek Laskowski – Google+ that has some more information on Spring4D too.

A tiny bit more information on collapsing: [WayBack] TURBU Tech » Blog Archive » Wish list: Generics collapsing

–jeroen

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

Delphi revelations #3 – kbmMW Smart client – DispInvoke leak – Components4Developers

Posted by jpluimers on 2020/11/26

For my archive, as one day I will run into this Variant/DispInvoke issue myself:[WayBack] Delphi revelations #3 – kbmMW Smart client – DispInvoke leak – Components4Developers:

Delphi revelations, now about investigating a pesky DispInvoke bug in all versions of Delphi supported by kbmMW, including 10.3 Rio, that results in leaks, and how kbmMW now ends up not being affected by it.

Via: [WayBack] Next Delphi revelations, now about investigating a pesky DispInvoke bug in all versions of Delphi supported by kbmMW, including 10.3 Rio, that results i… – Kim Madsen – Google+

–jeroen

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

Delphi: why breakpoints from time to time are not usable (green highlighted line on IDE)? – Stack Overflow

Posted by jpluimers on 2020/11/25

I still have not figured out the circumstances, but ever since either the Unicode Delphi versions (2009 and up) or BDS (8 and up), I have this every now and then: [WayBack] Delphi: why breakpoints from time to time are not usable (green highlighted line on IDE)? – Stack Overflow

So far this is consistent, limiting it to:

  • large projects (100+ kilo-lines-of-code)
  • for those projects, either as single .dproj file or as part of a .groupproj
  • debugging works the first time, but fails consistently with these symptoms:
    • break-points fail
    • blue dots in the gutter disappear; red-dots become green
    • exceptions cause a stack-trace with only hex-addresses
  • other projects in the same project group still debug fine

To get it working (one time per try) again:

  • restart the IDE
  • close the project, then re-open it in the same IDE instance

What does not help getting it to work:

  • clean build from the IDE
  • deleting DCU files

–jeroen

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

A bunch of Spring4D dependency injection container related questions

Posted by jpluimers on 2020/11/19

Since G+ is down, a lot of interesting questions have vanished.

Luckily I saved some by [WayBack] Jacek Laskowski – Google+ related to Spring4D and dependency injection:

[WayBack] Spring4D IoC and specific singleton… – Jacek Laskowski – Google+

First my Google Groups archival reminder:

  1. Rename from
  2. To
  3. Save that URL with Archive.is

Read the rest of this entry »

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

Do not make methods protected unless you want them to be visible as public

Posted by jpluimers on 2020/11/18

One of the protection levels in Delphi is protected. Originally meant for the class itself, that level is also visible to “friends”: anything in the same unit, for example:

unit BusinessLogicUnit;

interface

type
  TBusinessLogic = class(TObject)
  protected
     Procedure Foo();
     // ...
  public
     // ...
  end;

implementation

// ...

end.

You can even access them from outside that unit by using a trick like below.

Some people use the protected section so that unit tests can assess them using the below trick.

Do not do that!

It means anyone can use that trick, often doing more damage than good.

In this case, the trick was abused by a clever programmer that was relatively new to the code base. It resulted in unintended side effects.

unit HackUnit;

interface

implementation

uses
  BusinessLogicUnit;

type
  TBusinessLogicHack = class(TBusinessLogic);

procedure Hack;
var
  Instance: TBusinessLogicHack;
begin
  Instance := TBusinessLogicHack.Create();
  try
    Instance.Foo();
  finally
    Instance.Free();
  end;
end;

end.

Of course you can still access it like below.

It is slightly longer, but more importantly: much better shows the intent and how that intent is accomplished.

unit GoodUsageUnit;

interface

implementation

uses
  BusinessLogicUnit;

type
  TBusinessLogicDescendant = class(TBusinessLogic)
  public
    procedure Foo();
  end;

procedure TBusinessLogicDescendant.Foo();
begin
  inherited Foo();
  // ...
end;

procedure Usage;
var
  Instance: TBusinessLogicDescendant;
begin
  Instance := TBusinessLogicDescendant.Create();
  try
    Instance.Foo();
  finally
    Instance.Free();
  end;
end;

end.

–jeroen

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

The Delphi Geek: Using Generics to Manipulate Enumerated Types

Posted by jpluimers on 2020/11/18

Not that this is bad code, but there are no unit tests for them, and I have seen places in the wild that blindly use it without documenting where it came from and what tested alternatives might be: [WayBack] The Delphi Geek: Using Generics to Manipulate Enumerated Types

The unit itself is down (though there is still a copy on the WayBack machine).

The post itself mentions it is Spring4D-inspired, and since Spring4D already has quite an extensive [WayBack] TEnum<T> implementation covered by unit tests, so that is a logical place to do for.

I might actually document the migration table if I find time for it.

Here is a start so I will only have to insert the blanks

Function Replacement
class function Clip(const value: Integer): T;
class function Clip(const value: T): T;
class function Ensure(const value: Integer; const min, max: T): T;
class function Ensure(const value, min, max: T): T;
class function FromInt(const value: Integer): T;
class function Enum: RangeEnum; static;
class function GetValueOrDefault(const value: Integer): T;
class function IsValid(const value: Integer): Boolean;
class function IsValid(const value: T): Boolean;
class function Max: T; static;
class function Min: T; static;
class function ToInt(const value: T): Integer;
class function ToString(const value: T): string;

–jeroen

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

Delphi: `procedure RaiseAbstractError(const aClass: TClass; const aMethodName: string);`

Posted by jpluimers on 2020/11/17

Needs the System.SysConst unit:

procedure RaiseAbstractError(const aClass: TClass; const aMethodName: string);
begin
  // more explanatory than AbstractErrorProc();
  raise EAbstractError.CreateFmt('%s: method %s.%s', [SAbstractError, aClass.ClassName, aMethodName]);
end;

It uses a TClass typed parameter so you can call it from non-static class methods (using Self as parameter value) in addition to instance methods (using ClassType as parameter value).

–jeroen

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

delphi – What are the list of all possible values for DVCLAL? – Stack Overflow

Posted by jpluimers on 2020/11/17

From a while ago, from notes even longer ago – around 1994 on DVCLAL the Delphi VCL Access License code which is actually a checking logic for determining the SKU (stock keeping unit) or Delphi license: [WayBack] delphi – What are the list of all possible values for DVCLAL? – Stack Overflow

There is no official documentation on this, so here is something from my notes of 15+ years ago:

The DVCLAL is there to check which SKU of Delphi you are using and it varies per SKU.

There are only checks for the Professional (RPR) and Client/Server (RCS) SKUs:

procedure RCS;

procedure RPR;

If they fail, they call this method:

procedure ALV;
begin
  raise Exception.CreateRes(@SNL);
end;

where

resourcestring
  SNL = 'Application is not licensed to use this feature';

Depending on the feature matrix and Delphi version, various components call RPR and RCS in their Create constructors to guarantee a minimum SKU.

Underneath, these RPR and RCS functions call the function  GDAL . Their names are historic and got documented around Delphi 2007:

  • [WayBack] GDAL (Get Delphi Access License)
  • [WayBack] RPR will Restrict to PRofessional license and higher
  • [WayBack] RCS will Restrict to Client/Service license and higher

Historically you had these levels of Delphi editions that could be distinguished this way:

  1. Personal
  2. Professional
  3. Client/Server (or Enterprise)

This excludes Starter and Community (which are “just” Personal), Turbo (which was “just” Professional), Architect and Ultimate, which are “just” Client/Server with extra tools.

A few years ago, another answer got added to that question explaining more details:

I am just adding another answer to this question, for all the people who search the for actual DVCLAL (Delphi Visual Component Library Access License) values, as well as some other information for all people who are curious how stuff works.

1) Like Jeroen Wiert Pluimers said, if you want to check for “Professional or higher” or “Enterprise only” inside your Delphi application/library/package/component, you can use RPR (Require Professional) or RCS (“Require Client/Server”; Client/Server was the name for the Enterprise edition in early Delphi versions) respectively. If the requirement is not met, ALV (Access License Violation) will be called which will raise an Exception with the message defined in SysConst.SNL (S Not Licensed). In English:

Application is not licensed to use this feature

2) In case you want to check for one specific edition, you can use the output of the function GDAL (Get Delphi Access License), which is one of the following (AL1s array):

AL1s[0] = $FFFFFFF0; // Standard/Personal edition DVCLAL value
AL1s[1] = $FFFFEBF0; // Professional edition DVCLAL value
AL1s[2] = $00000000; // Enterprise/ClientServer edition DVCLAL value
AL1s[3] = $FFFFFFFF; // DVCLAL resource not existing

if the DVCLAL resource has an invalid value, GDAL will call ALVwhich will raise an Exception with message SysConst.SNL.

3) In case you want to check the DVCLAL value of a foreign EXE/DLL file (e.g. if you want to write a Resource Editor, decompiler etc), then you’ll have to query the DVCLAL resource directly.

There are only three official values:

Standard:      23 78 5D 23 B6 A5 F3 19 43 F3 40 02 26 D1 11 C7
Professional:  A2 8C DF 98 7B 3C 3A 79 26 71 3F 09 0F 2A 25 17
Enterprise:    26 3D 4F 38 C2 82 37 B8 F3 24 42 03 17 9B 3A 83

4) Just for fun: If you solve the formula 0 = (ROR(a,15) xor a) xor (ROR(b,10) xor b) xor (ROR(c,5) xor c) xor (AL1 xor AL2) you can define any DVCLAL value (tuple a, b, c, d) you want! (AL1 and AL2 are the values in the AL1s and AL2s arrays which describe the desired Delphi edition; ROR is rotate right through carry)

For example, here are alternative DVCLALs which work too:

Standard:      00 00 00 00 00 00 00 00 9B 70 0C 66 6B 8F F3 99
Professional:  00 00 00 00 00 00 00 00 9A DB 73 0F 6A 30 8C F0
Enterprise:    00 00 00 00 00 00 00 00 D8 B2 48 11 D8 B2 48 11

To validate a DVCLAL, you calculate

AL1 := DVCLAL[0] xor DVCLAL[1] xor DVCLAL[2] xor DVCLAL[3];
AL2 := ROR(DVCLAL[0],15) xor ROR(DVCLAL[1],10) xor ROR(DVCLAL[2],5) xor DVCLAL[3];

and look up AL1 and AL2 in the array AL1s and AL2s,

This way you can disguise the edition you have used a little.

5) In the meantime, an official documentation, at least for the functions GDALRPR and RCS, has been published.

6) Of course, everything works for C++ Builder, too.

In the mean time, new posts explaining bits of DVCLAL related resources (like PACKAGEINFO and CHARTABLE) and the TPF0 form/datamodule resource have appeared, of which this is a selection:

jeroen

miscprogs.zip

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

Tracking down Delphi “abstract error” occassions.

Posted by jpluimers on 2020/11/12

A few tips.

  1. Set breakpoints in these units:
    • System.pas:
      procedure _AbstractError;
      begin
        if Assigned(AbstractErrorProc) then
          AbstractErrorProc;
        RunErrorAt(210, ReturnAddress);
      end;
    • System.SysUtils.pas:
      procedure AbstractErrorHandler;
      begin
        raise EAbstractError.CreateRes(@SAbstractError);
      end;
  2. add a watch for ClassName that has “Allow side effects and function calls” enabled
  3. run your code until it breaks or stops (usually in the first method above)
  4. double click on each entry in the “Call Stack” pane, and for each entry:
    • set a break point on the call to the method above it in the stack trace
    • note that sometimes this is on the line you arrived, but sometimes one more more lines higher in your code
    • the cause is that the stack trace will show you where your code will RETURN to, not the place it was CALLED FROM.
  5. abort your program
  6. run your program again
  7. on each hit breakpoint, watch the value of ClassName:
    • if it becomes [WayBackE2003: Undeclared identifier: 'ClassName', then:
      • observe the method directives
      • if it includes static, but it is actually inside a class, then remove the static

Sometimes you cannot perform the last step: class property definitions have to be backed by static class methods.

This usually means you have a bad design anyway: you depend on global/singleton kind of behaviour that is almost impossible to properly test.

A better approach is to have regular object instances for that, then use constructor dependency injection (maybe combined with factory or dependency injection container) to setup the structures of dependencies.

TODO

Create a conference summary on the use of class versus instance methods, and static/regular.

Base materials:

–jeroen

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