Changing component class at run-time on demand

This is easier as thought (thanks to Hallvard’s Blog – Hack#14: Changing the class of an object at run-time): procedure PatchInstanceClass(Instance: TObject; NewClass: TClass); type PClass = ^TClass; begin if Assigned(Instance) and Assigned(NewClass) and NewClass.InheritsFrom(Instance.ClassType) and (NewClass.InstanceSize = Instance.InstanceSize) then begin PClass(Instance)^ := NewClass; end; end; type TMyButton = class(TButton) public procedure Click; override; end; … Read more

Call C# dll from Delphi

You may have more luck skipping the COM part by using my project template for unmanaged exports class MyDllClass { [DllExport] static int MyDllMethod(int i) { MessageBox.Show(“The number is ” + i.ToString()); return i + 2; } } In Delphi, you’d import it like so: function MyDllMethod(i : Integer) : Integer; stdcall; extern ‘YourAssembly.dll’; I … Read more

How can I make AllocateHwnd threadsafe?

This problem can be solved like so: Obtain or implement a threadsafe version of AllocateHwnd and DeallocateHwnd. Replace the VCL’s unsafe versions of these functions. For item 1 I use Primož Gabrijelcic’s code, as described on his blog article on the subject. For item 2 I simply use the very well-known trick of patching the … Read more

How to kill a thread in delphi?

You have to check for Terminate in the thread for this to work. For instance: procedure TMyThread.Execute; begin while not Terminated do begin //Here you do a chunk of your work. //It’s important to have chunks small enough so that “while not Terminated” //gets checked often enough. end; //Here you finalize everything before thread terminates … Read more