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

check if network connection is permanent

Posted by jpluimers on 2017/11/22

Thanks Uwe for the below code!

I was in a situation of a batch file running from a Task Scheduler that got broken because the user suddenly turned on persistent network connections.

So I needed to check if a shared drive-letter was indeed persistent or not and act accordingly.

The below code helped me merging the batch file with the Delphi application it would run in the background anyway.

It’s way better than checking of the global “persistence” flag for new connections has been set in the registry: [WayBackSaveConnections flag at HKCU\Software\Microsoft\Windows NT\CurrentVersion\Network\Persistent Connections (which is reflected both in the Windows UI and settable via net use /Persistent).

Source: check if network connection is permanent

Via: [WayBack] Anyone who can share how one can query if a drive letter mapping to a share is persistent across logon sessions? (yes, this is Windows, I know) – Jeroen Wiert Pluimers – Google+

–jeroen

Read the rest of this entry »

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

Delphi history: No, dynamic arrays do not support a non-zero lower bound, but what if you want them? – via Stack Overflow

Posted by jpluimers on 2017/11/21

A post of some older Delphi stuff I did in the past just in case need it again.

David Heffernan found the documentation for this: [WayBackStructured Types (Delphi): Dynamic Arrays – RAD Studio

Since I needed a dynamic array structure supporting a non-zero lower bound, I was glad he also provided an answer with a data structure that does provide a non-zero lower bound.

For my own reference I’ve put his answers and questions below (as it’s way easier to search my blog than the complete internet) and my own implementation:

Read the rest of this entry »

Posted in Delphi, Development, History, Software Development | 10 Comments »

Hi! how to hide a property in object inspector? i have my custom components …

Posted by jpluimers on 2017/11/16

For my link archive:

Some quotes

–jeroen

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

FastMM4 + Delphi – Recompiling application with 10.2.1 causes memory leaks?

Posted by jpluimers on 2017/11/15

This is why you should always test your Delphi apps with FastMM4 and FullDebugMode enabled: [WayBack] Delphi – Recompiling application with 10.2.1 causes memory leaks?

A workaround is to add the below code in either of these places:

  • end of your .dpr file right before the end.
  • in the finalization section of a new unit

CheckSynchronize();

It calls the [WayBackSystem.Classes.CheckSynchronize method to ensure the background thread performs the needed cleanup.

Thanks Stefan Glienke for solving this.

–jeroen

Read the rest of this entry »

Posted in Delphi, Delphi 10.2 Tokyo (Godzilla), Development, Software Development | Leave a Comment »

Delphi: ^TNodeData incompatible with PNodeData – {$T+} versus {$T-}

Posted by jpluimers on 2017/11/14

In my Turbo Pascal days, I was fan of the {$T+} directive (now a.k.a TypedAddress on) as it would make the @ operator return typed pointers and the compiler would be more strict in forcing checks on pointer types to be compatible.

Not so much in Delphi any more, see the below comment on in a G+ thread on Delphi pointer type compatibility [WayBack] by David Millington from Embarcadero.

Basically we’re stuck with {$T-} which is a double edged sword:

  • on the one hand it forces you to ensure your typed pointers are always the same actual type (so you need to define a type like PNodeData once and use it everywhere)
  • on the other hand it still allows the generic Pointer type to be compatible with typed pointers which means people use that and will not have the benefit of typed pointer checking

David Millington:

So the question is why is PNodeData different to ^TNodeData? These two pages have the answer:

  • “Two distinct types are compatible if they satisfy at least one of the following conditions:
    … (snip)
  • One type is Pointer (an untyped pointer) and the other is any pointer type.
  • Both types are (typed) pointers to the same type and the {$T+} compiler directive is in effect.”

[WayBackhttp://docwiki.embarcadero.com/RADStudio/Berlin/en/Type_Compatibility_and_Identity_(Delphi)#Type_Compatibility

That line about $T+ is key; it’s the “Typed @ operator” compiler option which is off by default. “In the {$T-} state, distinct pointer types other than Pointer are incompatible (even if they are pointers to the same type)”. So with that off, PNodeData and ^TNodeData are different.

[WayBackhttp://docwiki.embarcadero.com/RADStudio/en/Type-checked_pointers_(Delphi)

The best solution is to define PNodeData, as you’ve done, and use it everywhere.

What David fails to mention there, and I added as a commen later is this:

The problem $T is that it’s a two-edged sword as it makes code that looks valid fail to compile because the compiler in the $T+ state (a.k.a. $TypedAddress on state) never got updated to:

  • handle @ to an array creates a pointer to the element of that array
  • handle @ to resourcestrings to become PResStringRec (and likely more examples)

Which means the below programs fail to compile.

The alternative (adding way more {$T+} compatible overloads to the RTL/VCL/FMX) was never done either, so now we’re stuck with {$T-}

Array of char example:

program TypedAddressDirectiveWithCharArrays;

{$APPTYPE CONSOLE}

{$TypedAddress on}

uses
  Winapi.Windows;

var
  TimeZone: string;
  TZ: TTimeZoneInformation;

begin
  TimeZone := 'Coordinated Universal Time';
  StringToWideChar(TimeZone, @(TZ.StandardName), SizeOf(TZ.StandardName) div SizeOf(WideChar));
end.

(*

[dcc32 Error] TypedAddressDirectiveWithCharArrays.dpr(16): E2010 Incompatible types: 'PWideChar' and 'Pointer'

In the System unit:

implicit types:

  PWideChar = WideChar;
  PChar = PWideChar

explicit:

function StringToWideChar(const Source: UnicodeString; Dest: PWideChar; DestSize: Integer): PWideChar;



In the Winapi.Windows unit:

type
  WCHAR = WideChar;

  PTimeZoneInformation = ^TTimeZoneInformation;
  _TIME_ZONE_INFORMATION = record
    Bias: Longint;
    StandardName: array[0..31] of WCHAR;
    StandardDate: TSystemTime;
    StandardBias: Longint;
    DaylightName: array[0..31] of WCHAR;
    DaylightDate: TSystemTime;
    DaylightBias: Longint;
  end;
  TTimeZoneInformation = _TIME_ZONE_INFORMATION;
  TIME_ZONE_INFORMATION = _TIME_ZONE_INFORMATION;

*)

ResourceString example:

program TypedAddressDirectiveWithResourceStrings;

{$APPTYPE CONSOLE}

{$TypedAddress on}

uses
  System.SysConst,
  System.SysUtils;

procedure RangeError;
begin
  raise ERangeError.CreateRes(@SRangeError);
end;

begin
end.

(*
[dcc32 Error] TypedAddressDirectiveWithResourceStrings.dpr(13): E2250 There is no overloaded version of 'CreateRes' that can be called with these arguments


System unit:

type
  PResStringRec = ^TResStringRec;
  {$IF defined(EXTERNALLINKER)}
  TResStringRec = record
    Key: MarshaledAString;
  end;
  {$ELSE}
  TResStringRec = packed record
    // 32bit = 8 bytes
    // 64bit = 16 bytes
    Module: ^HMODULE;
    Identifier: NativeUint;
  end;
  {$ENDIF}


System.SysConst unit:

resourcestring
  SRangeError = 'Range check error';


SysUtils unit:

constructor Exception.CreateRes(ResStringRec: PResStringRec);
begin
  FMessage := LoadResString(ResStringRec);
end;


*)

Source: [WayBackOn Friday I had a brain fade moment (which I don’t seems to have recovered…

–jeroen

Posted in Delphi, Development, Software Development | 2 Comments »

Session plan for all of CodeRage XII (starts 20171107)

Posted by jpluimers on 2017/11/07

Schedule in European time zone:

Schedule in USA time zones:

–jeroen

via: [WayBack] If you’re not sure what the sessions are at this year’s CodeRage – here’s a full list and timetable. CodeRage is a free multi-day online conference wit… – David Millington – Google+

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

Delphi history: on why was TypInfo.pas is not named TypeInfo…

Posted by jpluimers on 2017/11/02

[WayBack] A colleague and I was just wondering, why was TypInfo.pas not named TypeInfo.pas? The original theory of 8.3 naming convention doesn’t hold as TypInfo i… – Willo “qbitza” van der Merwe – Google+

Because of collision: though hardly documented in the early Delphi days the standard function TypeInfo has been there forever so System.TypeInfo already takes TypeInfo in the global namespace as the System unit is used by default and doesn’t have a namespace prefix.

One more reason to suffix unit names with a word like Unit.

And one more reason to always get a Delphi version with source code for RTL/VCL/…: it’s an invaluable source for not so well documented things.

The TypInfo name was also from an era when some people hadn’t learned yet dat using abbrvtns is a sin. Ken Thompson has learned [WayBackspell it with an “e” · golang/go@c90d392 but we’re suck with StrPas, StrUtils, IntToStr and others forever like Unix is stuck with creat(2): [WayBackcreat(2): open/possibly create file/device – Linux man page and [WayBackhistory – What did Ken Thompson mean when he said, “I’d spell creat with an ‘e’.” – Unix & Linux Stack Exchange.

–jeroen

References:

Posted in Delphi, Development, Software Development | 3 Comments »

{$DUPLICATEENABLE ON} against name collisions in Delphi…

Posted by jpluimers on 2017/11/01

Need to research {$DUPLICATEENABLE ON} further one day, but surely looks useful but is not really documented, not even at the Delphi 2009 documentation where it was introduced: [WayBackSysUtils.pfIgnoreDupUnits Constant.

–jeroen

Via: [WayBackMonkeyMixer has just been updated!- I’ve fixed a unit naming collision with Castlia- I’ve added an XE7 project group (which includes both the BPL and… – Simon Stuart – Google+

PS:

A cryptic G+ comment [WayBack] by Uwe Schuster:

DUPLICATEENABLE is old, but since it is something for IDE packages to avoid intersections with user/3rd party packages there is no need for a documentation. I do use it in an IDE package that did use the JCL/JVCL units as well and does another unit with a common name. IIRC I did mention it related to MonkeyMixer.

I think he argues that documentation is both not needed, but the usage of the directive sometimes is. Which somehow feels like a contradiction to me.

PS2: via Stefan Glienke, an error message that is related to this:

---------------------------
Error
---------------------------
Cannot load package 'Package2.'  It contains unit 'Whatever', which is also contained in package 'Package1'.
---------------------------
OK
---------------------------

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

Especially important for Delphi users on Windows 10 Creators Update: IDE Fix Pack 6.1 released | Andy’s Blog and Tools

Posted by jpluimers on 2017/10/31

The new [Archive.isIDE Fix Pack 6.1 released | Andy’s Blog and Tools is huge, especially for Windows 10 targets:

Windows 10 Creators Update 1703 caused issues with all Delphi programs, libraries and packages because it changed how Windows loads imported DLLs in such a way that it causes performance issues and can crash the debugger.

Delphi 10.2 Tokyo Update 2 fixed this by not producing multiple dll import sections for one DLL anymore.

IDE Fix Pack 6.1 implements that “feature” for all previous Delphi versions (2009-10.1 Berlin) and extends it to not only eliminate duplicate dll imports but also duplicate delay dll imports.

There are also (optional) updates that you can enable with new compiler switches:

  • Eliminating fwait instructions (but be careful, as they can confuse the debugger)
  • Optimising calls to virtual methods through interfaces
  • Optimising certain function prolog code

There are some smaller changes too.

I’m going to try this soon.

–jeroen

Posted in Delphi, Development, Software Development | 2 Comments »

Until someone writes proper string visualisers for the Delphi debugger…

Posted by jpluimers on 2017/10/31

A few tricks to write long strings to files when the Delphi debugger cuts them off (just because they like using 4k buffers internally);

  • TStringStream.Create(lRequestMessage).SaveToFile('c:\temp\temp.txt')
  • TIniFile.Create('c:\a.txt').WriteString('a','a',BigStringVar)
  • TFileStream.Create('c:\a.txt', fmCreate or fmShareDenyNone).WriteBuffer(Pointer(TEncoding.UTF8.GetBytes(BigStringVar))^,Length(TEncoding.UTF8.GetBytes(BigStringVar)))

They all work form the debug inspector, but they do leak memory. See comments below.

Via:

–jeroen

Read the rest of this entry »

Posted in About, Conference Topics, Conferences, Delphi, Development, Encoding, Event, Software Development | 6 Comments »