delphidllpass-by-referencecalling-conventionstdcall

Is passing a DLL function argument with "const" an equivalent to pointer?


Data block:

PMyDataBlock = ^MyDataBlock;
MyDataBlock = record
  // .............
end;

Is a following definition:

function MyFunction(const pstSettings: MyDataBlock): HRESULT; stdcall; external 'MyLib.dll' name 'MyFunction';

a full equivalent of this?:

function MyFunction(pstSettings: PMyDataBlock): HRESULT; stdcall; external 'MyLib.dll' name 'MyFunction';

Solution

  • The short answer is "No, it is not"

    In your case, your record may or may not be passed by reference. The size of the record is 1 of the factor I know of that affect that behavior. If your record is 4 bytes or less, I believe it will be passed by value, otherwise, it will be passed by reference. I don't believe this behavior is contractual (In other word, Embarcadero is free to change it at any time in the future). In other words, it's a bad idea to use const parameter to call an external function.

    If you want to pass your record by reference, the proper way to do would be to declare it var

    function MyFunction(var pstSettings: MyDataBlock): HRESULT; stdcall; external 'MyLib.dll' name 'MyFunction';
    

    or pass it as a pointer.