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,854 other subscribers

Archive for the ‘Delphi’ Category

The Delphi Open Tools API Book 1.1 – Dave’s Development Blog

Posted by jpluimers on 2021/10/07

[WayBack] The Delphi Open Tools API Book 1.1 – Dave’s Development Blog

–jeroen

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

Howto create C-Header for Delphi/Free Pascal/Lazarus DLL – data types – Stack Overflow

Posted by jpluimers on 2021/09/23

From a long time ago, but got a request for it a while ago is [WayBack/Archive.is] Howto create C-Header for Delphi/Free Pascal/Lazarus DLL – data types – Stack Overflow:

I have used the below construct to generate header files compatible with the C-mode compiler of Visual C++ 6 from Delphi 5 code when Delphi had the -JPH switch (see notes below).
Note that I have not used this since Delphi 5, but the switch has since then been expanded:
Somewhere along the line, the [WayBackJPHNE switch has been added to the dcc32 command-line compiler:
  -JPHNE = Generate C++ .obj file, .hpp file, in namespace, export all
It certainly does not handle all types, and you will need quite a bit of [Archive.isHPPEMIT and [Archive.isEXTERNALSYM directives.
It generates the .hpp files to import the DLL that was written in Delphi.
Notes from the Delphi 5 era:
{ Visual C++ 6 does not like nested structs/unions the way that BC++ can handle them.
  Visual C++ 6 requires the "typedef" to be present AND the typename AFTER the struct definition.
  You will see this when defining TConversationId, TReturnKey and other types that depend on nested structs/unions

  The trick is to perform these steps each and every time this unit changes:
    - Generate this unit once with all the EXTERNALSYM disabled.
      - Then the DCC32 -JPH will generate the basic structs for them.
    - Copy all the nested struct and union definitions to this file
    - Embed the definitions with (*$HPPEMIT '' *)
    - Append the typename at the end of the struct definition
    - Enable all the EXTERNALSYM again
    - Regenerate the HPP files by using DCC32 -JPH
  To make this process easier, we have introduced two new conditional defines:
    - BCB - disable EXTERNALSYM, disable HPPEMIT
    - VC6 - enable EXTERNALSYM, enable HPPEMIT

  A similar thing is with these constructions that VC6 does not like those either:
    - short strings (BCB defines them as "SmallString<##>" which VC6 does not like).
    - short integers (BCB defines them as "Shortint" so we have included an HPPEMIT for that)
}

{$ifdef win32}
  { important! Makes sure that the all enumerated types fit in exactly one byte each! }
  {$Z1}

  { force the C/C++ HPP header files to have the C/C++ compiler pack structure elements on byte boundaries }
  {$ifdef BCB}
    {$HPPEMIT '#pragma option push -a1' }
  {$endif BCB}
{$endif win32}

And the download (hopefully I have converted it to git by then):

The link to the converter is now [Archive.is] bitbucket.org/jeroenp/wiert.me/src/default/Native/… – Delphi Coder

–jeroen

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

Some links that might help use SonarQube with Delphi

Posted by jpluimers on 2021/08/31

For my link archive:

--jeroen

Posted in Delphi, Development, Software Development, SonarQube, Static Code Analysis | 2 Comments »

Some links on Flux outside React.js

Posted by jpluimers on 2021/08/24

The Flux architecture is often used in ReactJS, but there are also implementations outside that realm.

So here are some links for my archive:

–jeroen

Posted in .NET, C#, Delphi, Development, JavaScript/ECMAScript, ReactJS, Scripting, Software Development | Leave a Comment »

delphi – How to embed the MIDAS.DLL in the client executable – Stack Overflow

Posted by jpluimers on 2021/08/17

I hardly use the [WayBack] TClientDataSet Class any more, so every once in a while I bump into a legacy project that does.

Often that forgets provide a deployment list, so usually with deployment of the underlying DLL is forgotten.

Those moments, it slips my mind that the original name for the underlying DataSnap technology is called MIDAS, and the DLL is MIDAS.DLL. Sometimes even a wrong version of that DLL is distributed (unicode versus non-unicode still can still be a thing).

You can avoid loading it (often at a premium of reduced speed) my adding MidasLib to the uses list of any unit in your project:

When you use BPLs instead of full units, then missing this can lead to a nasty access violation like this one where the ClientDataSet cannot be created because the wrong Midas.dll (non-unicode) was loaded, see the screenshot and stack overflow below.

Lessons re-learned:

  1. Use a minimal deployment footprint
  2. Ensure your test systems are clean, so no older versions of dependencies can be loaded at any time.

OS EAccessViolation at adress 5110E007: Access violation at address 5110E007 in module 'dsnap250.bpl'. Read of address 00000000
{occured in main thread}

Stack:
[50060B13]{rtl250.bpl  } System.@HandleAnyException
[00404815]{GVS.exe     } FastMM4.CalculateHeaderCheckSum$qqrp29Fastmm4.TFullDebugBlockHeader (Line 9098, "FastMM4.pas" + 1)
[50060039]{rtl250.bpl  } System.@AfterConstruction
[00404815]{GVS.exe     } FastMM4.CalculateHeaderCheckSum$qqrp29Fastmm4.TFullDebugBlockHeader (Line 9098, "FastMM4.pas" + 1)
[00404824]{GVS.exe     } FastMM4.UpdateHeaderAndFooterCheckSums$qqrp29Fastmm4.TFullDebugBlockHeader (Line 9108, "FastMM4.pas" + 1)
[004057C6]{GVS.exe     } FastMM4.DebugGetMem$qqri (Line 9649, "FastMM4.pas" + 42)
[00405817]{GVS.exe     } FastMM4.DebugGetMem$qqri (Line 9686, "FastMM4.pas" + 79)
[5005A247]{rtl250.bpl  } System.@ReallocMem
[50065674]{rtl250.bpl  } System.DynArraySetLength
[50065715]{rtl250.bpl  } System.@DynArraySetLength
[5110D001]{dsnap250.bpl} Datasnap.Dbclient.TCustomClientDataSet.CreateDSBase
[51111571]{dsnap250.bpl} Datasnap.Dbclient.TCustomClientDataSet.CreateDataSet
[500E920D]{rtl250.bpl  } System.Rtti.TValue._op_Implicit
[00559D29]{CDSDemo.exe } MainDataModuleUnit.TMainDataModule.DataModuleCreate (Line 50, "MainDataModuleUnit.pas" + 1)
[5016E7EF]{rtl250.bpl  } System.Classes.TDataModule.DoCreate
[5016E69C]{rtl250.bpl  } System.Classes.TDataModule.AfterConstruction
[50060039]{rtl250.bpl  } System.@AfterConstruction
[5016E67A]{rtl250.bpl  } System.Classes.TDataModule
[00544941]{CDSDemo.exe } MainFormUnit.TMainForm.PageControlChange (Line 30, "MainFormUnit.pas" + 19)
[0054598B]{CDSDemo.exe } MainFormUnit.TMainForm.FormShow (Line 40, "MainFormUnit.pas" + 47)
[50C075DD]{vcl250.bpl  } Vcl.Forms.TCustomForm.DoShow
[50C0BF7D]{vcl250.bpl  } Vcl.Forms.TCustomForm.CMShowingChanged
[5005FD9F]{rtl250.bpl  } System.TObject.Dispatch
[50AC4C16]{vcl250.bpl  } Vcl.Controls.TControl.WndProc
[50AC97FB]{vcl250.bpl  } Vcl.Controls.TWinControl.WndProc
[50C080B5]{vcl250.bpl  } Vcl.Forms.TCustomForm.WndProc
[50AC4850]{vcl250.bpl  } Vcl.Controls.TControl.Perform
[50AC8B71]{vcl250.bpl  } Vcl.Controls.TWinControl.UpdateShowing
[50AC8C80]{vcl250.bpl  } Vcl.Controls.TWinControl.UpdateControlState
[50ACB9D6]{vcl250.bpl  } Vcl.Controls.TWinControl.CMVisibleChanged
[50AC4C16]{vcl250.bpl  } Vcl.Controls.TControl.WndProc
[50AC97FB]{vcl250.bpl  } Vcl.Controls.TWinControl.WndProc
[50C080B5]{vcl250.bpl  } Vcl.Forms.TCustomForm.WndProc
[50C0E597]{vcl250.bpl  } Vcl.Forms.TScreen.GetMonitor
[50C094C5]{vcl250.bpl  } Vcl.Forms.TCustomForm.SetWindowToMonitor
[50AC4850]{vcl250.bpl  } Vcl.Controls.TControl.Perform
[50AC31E6]{vcl250.bpl  } Vcl.Controls.TControl.SetVisible
[50C078C1]{vcl250.bpl  } Vcl.Forms.TCustomForm.SetVisible
[50C11DCF]{vcl250.bpl  } Vcl.Forms.TApplication.Run
[004B3B28]{CDSDemo.exe } VclApplication.TApplication.Run (Line 113, "VclApplication.pas" + 1)
(00161097){CDSDemo.exe } [00562097]

–jeroen

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

Some links on the Delphi VolatileAttribute

Posted by jpluimers on 2021/08/12

Some links, because I had a hard time finding good documentation on VolatileAttribute (which is the name of the type; you use it as [Volatile] on variables, parameters and fields.

TL;DR

Volatile

The volatile attribute is used to mark fields that are subject to change by different threads, so that code generation does not optimize copying the value in a register or another temporary memory location.

You can use the volatile attribute to mark the following declarations:

You cannot use the volatile attribute to mark the following declarations:

type
    TMyClass = class
    private
        [volatile] FMyVariable: TMyType;
    end;

Searches

Read the rest of this entry »

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

System.TypInfo.SizeOfSet – RAD Studio API Documentation

Posted by jpluimers on 2021/08/11

This seems to have been introduced into Delphi Berlin: [WayBack] System.TypInfo.SizeOfSet – RAD Studio API Documentation. Maybe slightly earlier, but at least it was not documented in XE8 [WayBack] (via [WayBack] Delphi “SizeOfSet”.).

This is a useful function, for instance to get the Cardinality of a set in [WayBack] delphi – How can I get the number of elements of any variable of type set? – Stack Overflow.

From what I can see, the undocumented TLargestSet type also got introduced in Delphi Berlin.

Few hits by Google on [WayBack] Delphi “TLargestSet”, including [WayBack] Free Automated Malware Analysis Service – powered by Falcon Sandbox – Viewing online file analysis results for ‘Coperativa.exe’.

Not much more is documented than this in Tokyo [WayBack]:

System.TypInfo.SizeOfSet

Delphi

function SizeOfSet(TypeInfo: PTypeInfo): Integer;

C++

extern DELPHI_PACKAGE int __fastcall SizeOfSet(PTypeInfo TypeInfo);

Properties

Type Visibility Source Unit Parent
function public
System.TypInfo.pas
System.TypInfo.hpp
System.TypInfo System.TypInfo

Description

Embarcadero Technologies does not currently have any additional information. Please help us document this topic by using the Discussion page!

–jeroen

Posted in Delphi, Delphi 10.1 Berlin (BigBen), Delphi 10.2 Tokyo (Godzilla), Delphi 10.3 Rio (Carnival), Delphi XE8, Development, Software Development | Leave a Comment »

Delphi 10.2 Tokyo and up compiler issue: incompatible with Delphi 1.10.1 Berlin behaviour on unit path resolution

Posted by jpluimers on 2021/08/10

There is a very tricky compiler and IDE issue in Delphi 10.2 and up that on the compiler site behaves differently from Delphi 1.10.1 Berlin, and on the IDE side stays the same from Delphi 2005 on.

The issue baffled me, as lot’s features were not added in the Delphi compiler because of backward compatibility reasons.

The problem is that the compiler now favours the unit search path over the paths specified in the .dpr. This breaks compatibility with earlier versions and the IDE: the IDE still thinks pathnames in the .dpr files are the ones to follow.

There are some permutations on this problem, of which a few ones below.

Nonexisting unit in the project, but existing on the search path

  • .dpr file in a directory X
  • a unit inside a different directory A specified the .dpr file, but not on disk, and directory A not in the unit search path
  • a disk version of the unit in yet another directory B where directory B is in the unit search path

This results in Delphi 1..10.1 Berlin to error on compiling the project (because the unit is not found), but 10.2 and up succeeding (because the unit is found).

Existing unit in the project, but directory not on the search a path

Another permutation is this one:

  • .dpr file in a directory X
  • a unit inside a different directory A specified the .dpr file, and on disk, and directory A not in the unit search path

Two same named units, one on search path, other in project but not on search path

Yet another permutation:

  • .dpr file in a directory X
  • a unit inside a different directory A specified the .dpr file, on disk, and directory A not in the unit search path
  • a disk version of the unit in yet another directory B where directory B is in the unit search path
  • unit A and unit B have different content

This works the same in all versions of Delphi: the unit file referenced in the .dpr is compiled.

Via: [WayBack] Jeroen Pluimers on Twitter: “Delphi 10.2 Tokyo and 10.3 Rio compilers break Delphi 1…10.1 Berlin behaviour. Still reproducing and researching the consequences. It for instance means stuff suddenly compiles when it should not, or breaks when it should not. Preliminary reproduction at “

[WayBack] wiert.me / public / delphi / DelphiConsoleProjectWithMissingProjectUnitThatIsOnTheSearchPath · GitLab

–jeroen

Read the rest of this entry »

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

DUnit with FastMM: Detecting memory leaks in individual tests.

Posted by jpluimers on 2021/08/05

Steps:

  1. Add the conditional define FASTMM to your project
  2. Ensure you have $(BDS)\source\DUnit\src  in your search path in your project (as otherwise Delphi will pick the pre-built TestFramework.dcu file which was compiled without the FASTMM conditional define)
  3. Inside a test method, or the SetUp method of a class, set FailsOnMemoryLeak to True
  4. If the SetUp method of the class allocates memory, ensure the TearDown de-allocates it. Otherwise you will have leaks:
    • DUnit will check memory differences from the start of the SetUp until the end of the TearDown
    • DUnit will not take into account what is freed in the destructor or by automatic finalization after the destructor!
  5. Re-build your application (otherwise the DUnit TestFramework unit will not take into account the FASTMM conditional define)

Depending in your test framework, FailsOnMemoryLeak might be by default be False or True:

  • TestInsight by default has FailsIfMemoryLeaked set to True for the root test suite (which is then applied to FailsOnMemoryLeak of any test method).
    procedure RunRegisteredTests(const baseUrl: string);
    var
      suite: ITestSuite;
      result: TTestResult;
      listener: ITestListener;
    begin
      suite := RegisteredTests;
      if not Assigned(suite) then Exit;
      result := TTestResult.Create;
      result.FailsIfNoChecksExecuted := True;
      result.FailsIfMemoryLeaked := True;
      listener := TTestInsightListener.Create(baseUrl, suite.CountEnabledTestCases);
      result.AddListener(listener);
      try
        suite.Run(result);
      finally
        result.Free;
      end;
    end;
  • Console DUnit runners (Text, or XML) by default have FailsIfMemoryLeaked set to False.
  • GUI DUnit runner has FailsIfMemoryLeaked depending on the options:

DUnit source differences

Note that recent Delphi versions (I think XE and up) ship with almost the same sources as https://sourceforge.net/code-snapshots/svn/d/du/dunit/svn/dunit-svn-r44-trunk.zip, with these Embarcadero changes:

  • all SVN timestamps are based on time zone -0400 instead of +0000:
    • $Date: 2008-04-24 07:59:47 -0400 (Thu, 24 Apr 2008) $
    • $Date: 2008-04-24 11:59:47 +0000 (Thu, 24 Apr 2008) $
  • Embarcadero removed:
    • all comment lines having TODO in them
    • all files of types .dof, .cfg, and.wmz
    • the files NGUITestRunner.nfm and NGUITestRunner.pas
    • the file /etc/usermap
    • the directory trees /private and /projects
  • Embarcadero changed
    • file /src/versioninfo.res from 9.3.0 to 9.2.1 (which is odd, as all files are from 9.3.0)
    • unit TextTestRunner to support:
      • CLR (used last in Delphi 2007)
      • FailureCount
      • ErrorCount
      • IndentLevel
      • PrefixChars
      • conditional defines ADDITIONAL_INFO, BASIC_INFO
      • output of UnitName
    • unit TestExtensions to support:
      • CLR (used last in Delphi 2007)
      • conditional defines ANDROID_FIXME and LINUX
      • compiler directive LEGACYIFEND
    • GUITestRunner.dfm to have ResultsView: TListView property Height value 45 instead of 39
    • the below methods to use ReturnAddress in stead of CallerAddr:
      • TGUITestCase.FindControl
      • TGUITestCase.Click
      • TGUITestCase.EnterKeyInto
      • TGUITestCase.EnterTextInto
      • TGUITestCase.Show
      • TGUITestCase.CheckTabTo overloads
      • TGUITestCase.CheckFocused overloads
      • TGUITestCase.CheckEnabled overloads
      • TGUITestCase.SetFocus overloads
      • TGUITestCase.CheckVisible overloads
    • method TGUITestRunner.RunTest
      • from
        class procedure TGUITestRunner.RunTest(test: ITest);
        begin
          with TGUITestRunner.Create(nil) do
          begin
           try
            suite := test;
            ShowModal;
           finally
            Free;
           end;
          end;
        end;
      • to
        class procedure TGUITestRunner.RunTest(test: ITest);
        var
          GUI :TGUITestRunner;
        begin
          Application.CreateForm(TGUITestRunner, GUI);
          with GUI do
          begin
           try
            suite := test;
            ShowModal;
           finally
            Free;
           end;
          end;
        end;
    • unit GUITestRunner:
      • from
        procedure RunTest(test: ITest);
        begin
          with TGUITestRunner.Create(nil) do
          begin
            try
              Suite := test;
              ShowModal;
            finally
              Free;
            end;
          end;
        end;
      • to
        procedure RunTest(test: ITest);
        var
          GUI :TGUITestRunner;
        begin
          Application.CreateForm(TGUITestRunner, GUI);
          with GUI do
          begin
            try
              Suite := test;
              if Suite <> nil then
                ShowModal;
            finally
              Free;
            end;
          end;
        end;
      • method TGUITestRunner.SetUp not to set SubItems[0] := ''; when there is no test suite.
      • method TGUITestRunner.FormCreate to use FormatSettings.TimeSeparator instead of TimeSeparator.
  • unit TestFramework to support:
    • HPPEMIT
    • LEGACYIFEND
    • CLR and _USE_SYSDEBUG_
    • AUTOREFCOUNT
    • NEXTGEN
    • ANDROID
    • the RunCountAttribute and RunCount support
    • a CheckEquals overload for uint64
    • a CheckNotEquals overload for TCharArray
    • CheckCircularRef
    • CompareFloatRelative
    • NotSameErrorMessage with WideString arguments instead of string
    • a TestDataDir function
    • ReturnAddress for older compilers
  • a new unit DUnitTestRunner
  • a new Makefile file
  • a new UnitTests.log file

–jeroen

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

TestInsight: when you think the math does not add up…

Posted by jpluimers on 2021/08/04

You’d think the math does not add up:

But it does: both “Success” and “Warnings” are counted as “passed”.

 

TL;DR: ensure the yellow “Warning” triangle is enabled

–jeroen

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