Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - Calling Win API

Tags:

winapi

delphi

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;
  • Is there any other advantage of using the method used on #Code2 ?
  • Is there any risk of doing that ?
like image 514
RepeatUntil Avatar asked Oct 27 '15 16:10

RepeatUntil


1 Answers

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.

like image 70
David Heffernan Avatar answered Oct 10 '22 06:10

David Heffernan