RepeatUntil
RepeatUntil

Reputation: 2330

Delphi - Calling Win API

What is the difference between calling an Win API in following codes

Code #1:

uses
  Winapi.ActiveX;

procedure Foo();
var
  pv :Pointer;
begin
  CoTaskMemFree(pv);
end;

Code #2:

procedure CoTaskMemFree(
    pv: Pointer
    ); stdcall; external 'ole32.dll';

procedure Foo();
var
  pv :Pointer;
begin
  CoTaskMemFree(pv);
end;

I noticed the executable file size of Code 1 (161,792 bytes) is bigger than executable file of code 2 (23,552 bytes). i think that because of Code 1 will also compile the following units.

unit Winapi.ActiveX;

uses Winapi.Messages, System.Types, Winapi.Windows;

Upvotes: 9

Views: 2678

Answers (1)

David Heffernan
David Heffernan

Reputation: 613572

The difference in size is for the reasons that you outline. When you use a unit, your executable will contain code from that unit, and any dependent units. There are various options that you can use to reduce the impact, but invariably your executable size will increase when you use a unit that was not previously used.

procedure CoTaskMemFree(pv: Pointer); stdcall; external 'ole32.dll';

It is perfectly reasonable for you to define this yourself, in this manner, and so avoid using Winapi.ActiveX. In fact, it would be far better if the Delphi RTL was more granular to support such uses. It's quite natural to wish to access the COM heap allocation routines, but nothing more.

Upvotes: 10

Related Questions