Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a nil in place of a const record?

I am trying to call the WIC (Windows Imaging Component) factory method CreateEncoder:

HRESULT CreateEncoder(
  [in]            REFGUID guidContainerFormat,
  [in, optional]  const GUID *pguidVendor,
  [out, retval]   IWICBitmapEncoder **ppIEncoder
);

The general idea would be:

var
   encoder: IWICBitmapEncoder;

CreateEncoder(
      GUID_ContainerFormatIco,  //The GUID for the desired container format
      nil, //No preferred codec vendor
      {out}encoder);

For the second parameter, i want to pass nil; to indicate that i have no encoder preference

But Delphi's translation of the WinCodec.pas is:

function CreateEncoder(const guidContainerFormat: TGuid; const pguidVendor: TGUID;
      out ppIEncoder: IWICBitmapEncoder): HRESULT; stdcall;          

You'll notice that the second parameter, the optional TGUID, is declared as const.

So how do i pass it nil?

How can i pass a nil where a const record is required?

I tried the naïve solution:

const
   NULL_GUID: TGUID = ();

factory.CreateEncoder(GUID_ContainerFormatIco, NULL_GUID, {out}encoder);

and while that happens to not fail, it isn't really following the correct rules. It's passing a non-NULL address, that points to a GUID that the API doesn't know about.

I tried the naïve solution:

factory.CreateEncoder(GUID_ContainerFormatIco, TGUID(nil), {out}encoder);

but that's an Invalid typecast.

like image 623
Ian Boyd Avatar asked May 31 '14 01:05

Ian Boyd


1 Answers

You can use something like this:

const
  NULL_PGUID: PGUID = nil;
begin
  factory.CreateEncoder(GUID_ContainerFormatIco, NULL_PGUID^, {out}encoder);
end;

Or as Sertac suggested, the shorter and sweeter version:

factory.CreateEncoder(GUID_ContainerFormatIco, PGUID(nil)^, {out}encoder);

I did a test and David's suggestion of:

factory.CreateEncoder(GUID_ContainerFormatIco, TGUID(nil^), {out}encoder);    

Also works.

like image 191
Graymatter Avatar answered Sep 25 '22 23:09

Graymatter