Quantcast
Channel: Delphi – The Wiert Corner – irregular stream of stuff
Viewing all 1440 articles
Browse latest View live

Windows Manifest Files and Delphi

$
0
0

An interesting read on Windows Manifest Files in Delphi [WayBack].

TL;DR:

Do not let Delphi manage your manifest. All versions are either limited, or buggy, or both.

Basically it’s the same as doing VersionInfo in Delphi: do not let the IDE do it, but do it yourself. In this case:

Write your own manifest in XML, then load it as a resource.

Via: [WayBack] Blogged : Windows Manifest Files – In this post I look at Windows Manifest Files, what they do, why we need them and how to use them in Delphi and Finalbuilder… – Vincent Parrett – Google+

–jeroen


Not sure why, but sometimes the Delphi IDE does not allow you to toggle the Console Application; it looks off, but is in fact on: IsConsole returns true

$
0
0

On my research list: Not sure why, but sometimes the Delphi options display a regular application, but the IsConsole returns true because of AppType Console in the main PropertyGroup.

  1. console applications are turned off in the linker
  2. the debugger still shows it as a console application when stepping through the initialization section of the System unit

One of the problems is that unlike the {$APPTYPE directive (which has not changed much over time), the AppType element is undocumented (hardly anything in the .dproj file is documented).

The $APPTYPE lists two values: Console and GUI, defaulting to GUI:

Empirically, the AppType element can have these values:

  • Application
  • Console
  • Package

This is the topmost part of the .dproj file does not matter, without AppType element valued Console, or with it or even having it valued Application: either way a console application is being built.

There is no $APPTYPE in the .dpr or any of the .pas files.

The IsConsole variable is always false and a console window always appears when debugging.

That variable is not always conclusive, as there are situations where code is inside a BPL or DLL started by an EXE, so this works better: [WayBack] How to determine if I’m running as a console app? (Delphi on Win32) – Stack Overflow:

function IAmAConsoleApp: Boolean;
var
  Stdout: THandle;
begin
  Stdout := GetStdHandle(Std_Output_Handle);
  Win32Check(Stdout <> Invalid_Handle_Value);
  Result := Stdout <> 0;
end;

The .dproj files:

https://gist.github.com/jpluimers/a2839f02857e2a9aaf719098a3630a0c

–jeroen

Interface methods are not assignment compatible with method references or methods of object.

$
0
0

Boy I wish that QC was still up and QualityPortal was publicly indexable as that would have saved me quite a bit of time tracking this down. Luckily I got help from Stefan Glienke (who maintains the awesome Spring4D library based on modern Delphi compiler support) when I mentioned

How good are you with reference to function?
I’ve an odd compiler thing throwing errors when using interfaces but not with classes.

So, for posterity:

Unlike C#, in Delphi interface methods are not compatible with method references or methods of object.

This has many manifestations, which means you can get a variety of compiler errors. I’ve listed the ones I could find below, but presume there are more and if I find more will update this post.

These are the errors you can get:

  • E2010 Incompatible types: ‘T’ and ‘Procedure’
  • E2035 Not enough actual parameters
  • E2250 There is no overloaded version of ‘FirstOrDefault’ that can be called with these arguments

These are the (now defunct, but used to be publicly accessible) QC and QualityPortal (needs sign on) entries (thanks Stefan Glienke and Blaise Thorn for reporting these):

The really frustrating part is that the RSP is marked as “new feature” whereas clearly it isn’t, so it probably never will be fixed.

A workaround for now is to wrap the interface method references with:

  • either anonymous methods (when you have just a few classes to cover, but maybe more than a few methods on the interface)
  • or instance methods on a class (when there are many classes to cover and preferably few methods on the interface)

Examples are in the code below that also shows this works fine and dandy in C#.

–jeroen

Delphi, attributes, RTTI and the IDE

$
0
0

Reminder to self: [WayBack] It took me a while to address an awful IDE crash issue. If you install a design time package which uses RTTI to populate custom attributes declared in… – Baoquan Zuo – Google+

TL;DR: be very careful using the built-in RTTI support objects as when they refer to custom defined attributes in packages, and the packages get unloaded, the cache does not get flushed.

Bug: RSP-11620: IDE crashes when rebuilding a project group that contains a component with customattribute

–jeroen

Delphi: do NOT use duplicate GUIDs on interfaces

$
0
0

One of the things when fixing bugs in an old codebase is wading through technical debt.

A quick win is to get rid of duplicate GUIDs when interface portions have been copy-paste re-used:

  1. interfaces with the same GUID are treated the same with as casts even if they are different.
  2. the compiler does not warn about duplicate GUID values**

These searches help big time: it shows the duplicate GUIDs if they have been indented all the same way. Good enough for most situations.

grep -Sh "\[\'{" *.pas | sort

grep -rh "\[\'{" *.pas | sort

It depends which flavour of grep you use (gnu or regular) to specify recursion.

Note that findstr doesn’t cut it as it always shows the filename: [WayBackList files recursively showing only full path and file size from Windows Command Prompt – Super User

** this compiles without warning:

program NoWarningOnDuplicateInterfaceGUID;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  IInterface1 = interface
    ['{ECF26C39-CBFF-488E-A3AB-2629726F1005}']
  end;

  IInterface2 = interface
    ['{ECF26C39-CBFF-488E-A3AB-2629726F1005}']
  end;

var
  Subject: IInterface;
begin
  try
    (Subject as IInterface1)._Release;
    (Subject as IInterface2)._release;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

–jeroen

Firemonkey/Isometric at master · tothpaul/Firemonkey

Detecting if a debugger is present is different from detecting if an IDE is present.

$
0
0

I have seen this happen in various environments: people wanting to detect if their debugger is present, but trying to detect their IDE, or vice versa.

Similar reasoning is for detecting for being running on a console, or your project having been built to run on a console.

People use these decisions, for instance to determine if their application should behave as a service, or as a regular process.

Ways to determine the various permutation points:

  • Running inside a debugger: use the [WayBackIsDebuggerPresent function (which can be true, even if Delphi DebugHook stays zero).
  • Check for the IDE: tricky; as IDEs have different behaviour over time. For Delphi, check for any TAppBuilder Window class with the [WayBack] FindWindow function.
  • Compiled for console: for Delphi check IsConsole, for .NET I could not find good information.
  • Running on a console: check if you can allocate a handle to stdout
  • Running as a service: check the hosting assembly or hosting process

Related links:

–jeroen

When generics and VCL controls bite you: memory overwrites when you show the control usually ending up in access violations

$
0
0

Recently I got bitten by the 2013 reported http://qc.embarcadero.com/wc/qcmain.aspx?d=112101 (too bad the site is gone and the WayBack machine doesn’t have it archived) as a result of [WayBackdelphi – Why do I get access violations when a control’s class name is very, very long? – Stack Overflow.

It got reentered as [RSP-18399] Buffer overflow in TWinControl.CreateParams() – Embarcadero Technologies but since that requires logon, it’s not search machine indexed so it’s very hard to find unless you know where to look.

So I spent a quite some time to find out what was wrong:

Since Delphi 1, the [WayBackControls.TCreateParams Record has a 64-byte WinClassName field that’s blindingly copied by the TWinControl.CreateParams without range checking.

The structure is used by the [WayBackTWinControl.CreateWnd Method to call the Windows API [WayBackRegisterClass function that takes a [WayBackWNDCLASS structure with a lpszClassName field that supports up to 256 characters and it fails when it’s longer.

That overwrite cause spurious other errors depending on the memory that gets overwritten. It took almost a day to figure out the cause of the error was this field, then an hour or to track that down to the long class names created by generic code.

To my surprise, I found back [WayBack] This issue caused coworkers and me quite a few hours wasted:Long story short – refactor some forms/frames to class names longer than 64 chars and boom… – Stefan Glienke – Google+.

As of Delphi 8 (yes, that version that a lot of people want to forget, but did bring a few good things), the structure was defined as below, and the code intialising also got improved:

Params.WinClassName := ClassName;
...
Params.WinClassName := Format('%s.%d', [Params.WinClassName, AppDomain.CurrentDomain.GetHashCode]);

So there it’s a string that – if it is too long – will get rejected by the Windows API anyway just like the native Delphi VCL implementation should have done 20+ years ago.

The sad part for FMX users: that structure and code got blindingly copied to the FMX.Controls.Win unit.

{$IF DEFINED(CLR)}
  TCreateParams = record
    Caption: string;
    Style: DWORD;
    ExStyle: DWORD;
    X, Y: Integer;
    Width, Height: Integer;
    WndParent: HWND;
    Param: IntPtr;
    WindowClass: TWndClassInfo;
    WndProc: TFNWndProc;
    WinClassName: string;
  end;
{$ELSE}
  TCreateParams = record
    Caption: PChar;
    Style: DWORD;
    ExStyle: DWORD;
    X, Y: Integer;
    Width, Height: Integer;
    WndParent: HWnd;
    Param: Pointer;
    WindowClass: TWndClass;
    WinClassName: array[0..63] of Char;
  end;
{$ENDIF}

–jeroen


Detecting the Delphi edition that is installed

$
0
0

Via [WayBack] In what ways can you detect which edition of Delphi in installed from the installed files? I’m trying to distinguish between Standard, Professional, E… – Jeroen Wiert Pluimers – Google+

Different Delphi editions have different msbuild support files. For instance IDL is only available in Enterprise/Architect/Ultimate (which use the exact same binaries anyway).

IDL is old, see

So I updated my [WayBack] jeroenp / wiert.me / Run-Dependend-rsvars-From-Path.bat — Bitbucket (which besides running rsvars.bat, can start the various Delphi IDE personalities, or run msbuild with various configurations) with [WayBack] jeroenp / wiert.me / commit / 2c55fa1bf786 — Bitbucket:

Support 10.1 Berlin and 10.2 Berlin in Run-Dependend-rsvars-From-Path.bat, as well as Professional/Starter editions.

Thanks to Andrea Magni – Google+, Querying was simple: look for the value of the Edition entry under inside the *\X\BDS\Y.0 where Y is the BDS version, X is Borland, CodeGear or Embarcadero (depending on the BDS version) and * is the base key HKEY_CURRENT_USER\Software, HKEY_LOCAL_MACHINE\SOFTWARE , HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node.

–jeroen

ILockable/TLockable/Lockable. Similar to IManaged in Spring4D, but provides a Locked interface.

$
0
0

Had to use this to start solving threading issues in a project I inherited a while ago in a temporary way before doing a huge refactoring. Will likely need it in the future as well: ILockable/TLockable/Lockable. Similar to IManaged in Spring4D, but provides a Locked interface..

ILockable/TLockable/Lockable. Similar to IManaged(now IShared) in Spring4D, but provides a Locked interface.

Note this is very inefficient, but can be practical as a shotgun approach to start solving the mess when you inherit a project that has the “I know, I’ll use threads!” approach in it.

Replace the resource typed TNeedsLock that needs protection with a ILockable<TNeedsLock>, then route all references via a call to the Locked()() call to it.

If you need a simpler approach, then use [WayBack] Interfaced monitor lock — Bitbucket by Stefan Glienke.

–jeroen

Some notes on Testinsight Issues

$
0
0

So I won’t forget:

Note to self:

https://bitbucket.org/sglienke/testinsight gets you to the issues list filtering on new open issues.

If your screen is not wide enough, you will not see the “Wiki” link on the left side bar: click the hamburger menu to navigate to the Wiki at bitbucket.org/sglienke/testinsight/wiki

–jeroen

including enumerations and JPEG compression examples for wPDF 4 Manual: Compression related properties

$
0
0

Since I was tracking down an issue having to to with generating DIB in a compressed PDF: [Archive.is] wPDF 4 Manual: Compression related properties

Property CompressStreamMethod

By modifying this property you can let the PDF engine compress (deflate) text. By using compression the file will be reasonable smaller. On the other had compression will create binary data rather than ASCII data. While “deflate” produces the smallest files, “run-length” compression is compatible even to very old PDF reader programs.

Property JPEGQuality

wPDF can compress bitmaps using JPEG. This will work only for true color bitmaps (24 bits/pixel) and if you have set the desired quality in this property.

Property EncodeStreamMethod

If data in the PDF file is binary it can be encoded to be ASCII again. Binary data can be either compressed text or graphics. You can select HEX encoding or ASCII95 which is more effective then HEX.

Property ConvertJPEGData

Note: Only applies to TWPDFExport.

If this property is true JPEG data found in the TWPRichText editor will not be embedded as JPEG data. Instead the bitmap will be compressed using deflate or run length compression. It is necessary to set this property to TRUE if the PDF files must be compatible to older PDF reader programs which are incapable to read JPEG data.

Note that EncodeStreamMethod does not do compression, but it does belong here because the encodings result in different PDF sizes.

The settings are not documented in more detail, so here are the enumerations explaining them in a bit more depth:

–jeroen

delphi – IfThen(Assigned(Widget), Widget.Description, ‘No Widget’) doesn’t crash. Should it? – Stack Overflow

$
0
0

Very interesting question [WayBackdelphi – IfThen(Assigned(Widget), Widget.Description, ‘No Widget’) doesn’t crash. Should it? – Stack Overflow.

Three important things here:

  • depending on inlining and kind of arguments, function calls can evaluate their arguments one or multiple times
  • lacking formal language specification, you never know if a method will be inlined or not
  • function calls should not have side effects

Via another interesting discussion at [WayBack] Inline functions are not guaranteed to evaluate their arguments exactly once… – David Heffernan – Google+

–jeroen

Delphi annoyance: in debug mode, no breakpoints are being adhered to, no blue bullets

$
0
0

For all Delphi Galileo versions (the BDS based Delphi versions: 8 until now), I have bumped into this annoyance:

Over time, while working on an application, running it in DEBUG mode in the debugger, wil not fire any breakpoints and all blue bullets (meaning the lines have code generated) are gone.

There are no warnings or (error) dialogs leading to this situation.

The only remedy is to quit Delphi, start it again, then do a full rebuild of the application.

Of course this happens more often with large applications than with small ones.

Is there anyone who has a reliable method to:

  • signal this before it happens
  • workaround it in a better way

–jeroen

Debugging RTL/VCL Code with CodeSite – Dave’s Development Blog

$
0
0

This is so cool! [WayBackDebugging RTL/VCL Code with CodeSite – Dave’s Development Blog.

It comes down to performing CodeSite.Send(...) calls as evaluation expressions in non-breaking breakpoints.

Ensure you have the CodeSite.Logging unit in your uses lists and you’re good to go.

Thanks David for pointing me to this!

This is even more useful than the breakpoint Log Message itself (which is only a string) or plain Eval Expression (which puts just one item into the Delphi event log) despite them being there since Delphi <= 5:[WayBackDebugging code in Delphi XE – Stack Overflow.

–jeroen

via: [WayBack] Debugging RTL/VCL Code with CodeSite – David Hoyle – Google+

 


Just found out about the SysUtils.FindCmdLineSwitch Function

$
0
0

I learn new things every day. So today I learned about [WayBackSysUtils.FindCmdLineSwitch Function, which was introduced in Delphi 4, but I was still messing with ParamCount/ParamStr loops.

It as not changed over time. The above docs are Delphi 2007, and these are some of the newer:

–jeroen

Where do you place your unit uses?

$
0
0

Over the years, I have had the question of where to put uses list entries a lot.

Last year, there was one again from a very experienced developer: [WayBack] Where do you place your unit uses? Over the years, I’ve come to preferring to place my uses in the Interface section only, even if its types, constants… – Lars Fosdal – Google+

The answer is really simple, and comes down to this:

  • use only the units you need (Law of Demeter)
  • use the units as close as possible to where you need them (this helps Minimizing Scope which is related to Information Hiding and the Proximity Principle)

Besides these Clean Code and Code Complete arguments, there is another very important argument:

The larger the scope of a unit, the more resources it takes to compile your project.

This gets worse when you have cycles in your unit dependencies.

I think it gets more than progressively worse; I have seen ~5 million line projects use close to 2 gigabytes of RAM during compilation when they had deep/long cyclic dependencies, forcing a full project build with DDevExtensions configured correctly in order to avoid out-of-memory at all.

For the above question, the poll seems to indicate the public at large gets it right:

References

A few tips from the thread:

Just a note: I use the Uses Report in Peganza Pascal Analyzer to identify the candidates for moving to the interface section. The task itself is easily done with Ctrl-Alt-Shift-Up (or Down) from MMX Code Explorer, which moves the unit under the cursor to the other section.
By the way I highly recommend this brilliant expert:
github.com – rfrezino/RFindUnit
+Lars Fosdal Use a proper static analysis tool on a regular basis. The Unit Dependency Analyzer as part of MMX Code Explorer can tell you that pretty fast.

–jeroen

Getting rid of bugs: mark them as “new feature”– as cancelling a  THTTPClient.BeginGet() does not work as intended

$
0
0

I have seen this happen before, but it seems to be a habit on the Quality Portal: getting rid of bugs by marking them as “new feature”:

[WayBack] I’m using THTTPClient.BeginGet() to create an async get request to a webservice that holds a connection open until it has data to return (sort of like a… – Brian Ford – Google+. which basically was another person finding out about [RSP-20827] THTTPClient request can not be canceld – Embarcadero Technologies, which got marked (a month after submission!) as “Issue is reclassified as ‘New Feature'”.

I get why it happens (there was something exposed, but some of the functionality is missing a feature which needs to be added).

Marking it as such however sends the wrong signal to your users: we do not see bugs as bugs, so they get on the “new feature” pile with no estimate on when the feature will be done.

–jeroen

Does anyone have the source from the book: The Tomes of Delphi: Algorithms and Data Structures…

$
0
0

Since I need this one day: [WayBack] Does anyone have the source from the book: The Tomes of Delphi: Algorithms and Data Structures by Julian Bucknall? – John Kouraklis – Google+

–jeroen

PS: Back when writing this, the site had a 301 redirect problem usually when the referrer was a Google domain. See [WayBack] Jeroen Pluimers on Twitter: “@JMBucknall https://t.co/WGM77kDSQL in an anonymous Chrome window gets to the site; in a regular Chrome tab goes to https://t.co/5foBijMW4R Somehow that persisted as a 301 in the browser cache. Clearing the cache and retrying solved the issue.” which seems related to [WayBack] Lumenaris website hacked by scammers! | Leaving Earth | BoardGameGeek

I went to the Lumenaris website to see if you could still buy Leaving Earth & expansions, only to find it has been hacked by unscrupulous pharmaceutical scammers. I hope it gets sorted out soon!

Twitter thread:

 

 

 

Delphi function result assignments before the function returns…

$
0
0

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

Viewing all 1440 articles
Browse latest View live