How to pass a string array to an external DLL?

I have an external function:

extern "C" __declspec(dllexport) int __cdecl Identify(BSTR* bstrTemplates, __int64 lCount, __int64* lIndex, __int64* lRetCode) 

BstrTemplates must be a string array.

What my function in D7 should look like and how to pass a string array to an external function. I can’t lower my head now.

+3
source share
3 answers

Finally, the problem is resolved. It was a dynamic array. It looks like it cannot be used as a C-style array. It seems the length prefix is ​​confused with dll. For the entries here is the prototype and use:

A type

type
  TArrayOfWideString= array[0..999] of WideString;

Statement

function Identify(var ATemplates: TArrayOfWideString; ATemplatesCount: int64; var ATemplateIndex: int64; var ARetCode: int64): Integer; cdecl; external 'Identify.dll';

Using

var
  templateIndex, retCode: int64;
  templates: TArrayOfWideString;
  retval: integer;

//TODO: range checking for TArrayOfWideString needed

templates[0] := 'template1';
templates[1] := 'template2';

retVal := Identify(templates, 2, scanIndex, retCode);
+1
source

A BSTR - WideString Delphi, BSTR WideString Delphi, C- , , . , , , , .

, WideString Delphi null nil Delphi:

var
  templates : array of WideString;
begin
  SetLength(templates, 3); // 2 template names + 1 nil
  templates[0] := 'template1';
  templates[1] := 'template2';
  templates[2] := nil;

  Identify(@templates[0], ....); // pass it as a pointer to the first element

, . , ( C ), . .

+3

BSTR * BSTR ( Delphi BSTR WideString).

: ( :-)):

:

function Identify(bstrTemplates: PWideString; lCount: int64; lIndex: PInt64; lRetCode: PInt64): Integer; cdecl external 'mydll.dll';

Delphi:

function Identify(bstrTemplates: PWideString; lCount: int64; var lIndex: Int64; var lRetCode: Int64): Integer; cdecl external 'mydll.dll';

( , bstrTemplates nil):

function Identify(var bstrTemplates: WideString; lCount: int64; var lIndex: Int64; var lRetCode: Int64): Integer; cdecl external 'mydll.dll';

Use the first element in the array when passing bstrTemplates (for example, @MyArray [0])

+1
source

Source: https://habr.com/ru/post/1756719/


All Articles