Friday 15 January 2010

Delphi-to-C dll: Passing Arrays -


I am using Delphi to load a DLL (which I have created in Delphi XE-3) Code my problem knows why my works are not being run - they are not the only one Delphi file (simplified) looks like this:

  Program calls the call to 'DLLUnitipes' Uses SUUSUtils, Windows, DLLUNI; // Header Conversion var DLLHandle: Cardinal; N: integer; A: TARRA & lt; Integer & gt; Results 1: Integer; Get started / start each array Set Lang (R, A); A [0] = ...; // Load the DLL (and confirm its filled) DLLhandle: = LoadLibrary ('dllname.dll'); If DLLhandle & lt; & Gt; 0 Then start the result 1: = dll_func1 (n, a); // A and B have not ended the Android (Free) library correctly; End.   

I have successfully added "trace in" dll_func1 to DLLUnit for the first time, in which:

  const nameofDLL = 'dllname'; Function dll_func1 (n: integer; a: tARRA & lt; integer & gt;): integer; Cdecl; DDLL of external name; Again, I access the C file, which is still the correct n and DLLdefs value, but A (under the "Local variable" heading) has become:   / P> 
  [-] A: (multi-numeric). [0] 0 (0x00000000)   

I know I can at least get DLL (hopefully) I'm reaching because other functions should call and I'm able to trace in dll_func1.c file without a problem I change the function to

  function dll_func1 (n: Integer; A: PInteger) Whistle Of: integer; Cdecl; DDLL of external name; ... result1: = dll_func1 (n, PInteger (A))   

or

  function dll_func1 (n: Integer; A: PInteger): Maximum Marks ; Cdecl; DDLL of external name; ... Result 1: = dll_func1 (n, @ a [0])   

(using integers or array of both arrays and arrays) but there is no change, Believing is related to a problem which I am not looking at compiling and running the whole thing, but the result is incorrect due to 1 TARRA failures. Any thoughts on what's going wrong?

Edit Function in C: int dll_func1 (int n, int a []) < / Div>

In your question there are two Delphi announcements for external functions. One of these is used by TArray & lt; T & gt; Do not do this as a parameter, you can not use the Delphi dynamic array as an interop type. The reason is that TArray & lt; T & gt; is a complex managed type that can only be created and consumed by delphi code.

You have to do this as I do below, and as I explained in the answer to my previous question, and declare element type as an array parameter declaration. For example, PInteger , PDouble , etc.

Here's a lot of confusion, and unnecessary complexity is the easiest example of what you need to see how to pass the array in your code from your Delphi code to C code.

This is it.

C code

  // testarray.c zero print double (double D); // Linker delphi code will solve it with zero test (double * arr, int count) {int i; For (i = 0; i   

Delphi code

  Program DelphitiOC; CRTL uses {$ APPTYPE CONSOLE}; Repeating process _print double (d: double); Cdecl; RightLen start (D); End; Process testing (AR: PDDB; count: integer); Cdecl; External name '_test'; {$ L testarray.obj} var arr: TARRA & lt; Double> Start arr: = TArray & lt; Double>. Create (1.0, 2.0, 3.0, 42.0, 666.0); Test (PD double (AR), length (AR)); Readln; End.   

Compile using C code, for example, Borland C compiler like this:

 bcc32 -c testarray.c  

And the output is: 1.00000000000000 E + 0000 3.00000000000000 E + 0000 4.20000000000000 E + 0001 6.66000000000000 E + 0002

Note that I have added the C code steadily because it was easy me. If you put C code in DLL, then there is not much change.

The conclusion is that I gave you the code in your reply, and I repeat here, that's right. This approach succeeds in passing through Delphi code in C. It seems that your diagnosis and debugging are in error.

You are only inspecting A [0] , so it is not surprising that you see only one value if you only a [1] , a [2] , ..., a [n -1] you will see that all the values ​​are being passed correctly or Perhaps your debugging was done on the wrong announcement of the external function, which was used by TArray & lt; T & gt; as the parameter.

No comments:

Post a Comment