The array pointer problem seems complicated to be simple. Please help me! Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiDB/html/delphi_20061219113016224.html
First, I made a DLL named "mydll"
Library mydll;
Uses
Sharemem,
Sysutils,
Extctrls,
Stdctrls;
Type TPS = record
Sno: integer; // serial number
Fjcode: String [200]; // code
End;
TP = array of TPS;
PTP = ^ TP;
{$ R *. Res}
Function select (VAR presult: PTP): integer; export; stdcall;
Begin
Try
New (presult );
Setlength (presult ^, 2 );
Presult ^ [0]. Sno: = 1;
Presult ^ [0]. fjcode: = 'day ';
Presult ^ [1]. Sno: = 2;
Presult ^ [1]. fjcode: = '小 ';
Result: = 1;
Finally
Dispose (presult );
End;
End;
Exports
Select;
Begin
End.
Then I use delpih with the following Program Call
VaR
Form1: tform1;
Type
TPS = record
Sno: integer; // serial number
Fjcode: pchar; // series code, such as DBF
End;
TP = array of TPS;
PTP = ^ TP;
Implementation
{$ R *. DFM}
Function select (VAR presult: PTP): integer; export; stdcall;
External 'mydll. dll ';
Procedure tform1.button1click (Sender: tobject );
VaR presult: PTP;
I: integer;
Begin
I: = select (presult );
Showmessage (presult ^ [0]. fjcode); // Why is the error in this line ???
Showmessage (presult ^ [1]. fjcode );
End;
The problem now is that I have read my DLL and it should be okay!
Why showmessage (presult ^ [0]. fjcode); the error "memory access error" is returned if the word "big" is not displayed '!
How can I check whether the output result of presult in the function after DLL is called Is 'Big?
It has been an hour since I posted my post. I have been online for more than an hour. I am so worried. Please help me!
Http://community.csdn.net/Expert/topic/5238/5238067.xml? Temp =. 7243158.
Maozefa (a fa) brother, because I do not know how big the array I uploaded is, so I must dynamically set the length in the DLL. How should I write it?
In addition, if you first open the array and then enter the DLL assignment, what changes should I do to get my output result of 'Big? I hope you don't want to give me some advice. I am not very good at this level, and I am forced to reach this level. I hope you can get your guidance !!!
First, modify the structure:
TPS = packed record
Sno: integer; // serial number
Fjcode: array [0 .. 199] of char;
End;
Second, add a function in the DLL to return the array size. The application calls this function first, and then allocates memory.
Use a pointer as a parameter to call the select
Procedure tform1.button1click (Sender: tobject );
VaR
Presult: TPS;
I, Count: integer;
Begin
Count: = getselectcount; // assume that the function added by DLL returns the number of arrays.
Setlength (presult, count );
I: = select (@ presult );
Showmessage (presult ^ [0]. fjcode );
Showmessage (presult ^ [1]. fjcode );
End;
No tests. For more information, see.
I did it according to your method, that is, allocating the address first and then calling the DLL
However, according to yourCode:
Setlength (presult, count); // there is an error. It should be setlength (presult ^, count );//
I: = select (@ presult); // when this statement is executed, the real parameter and the form parameter are of different types.
Debugging has not been completed yet. That is to say, I haven't got the word 'Big data' yet. Can you help me...
Type
TPS = packed record
Sno: integer; // serial number
Fjcode: array [0 .. 199] of char;
End;
PTP = ^ TPS;
Tparray = array of TPS;
Function select (presult: PTP): integer; export; stdcall;
Begin
Tparray (presult) [0]. Sno: = 1;
Tparray (presult) [0]. fjcode: = 'day ';
Tparray (presult) [1]. Sno: = 2;
Tparray (presult) [1]. fjcode: = '小 ';
Result: = 1;
End;
Function getselectcount: integer; export; stdcall;
Begin
Result: = 2;
End;
Procedure tform1.button1click (Sender: tobject );
VaR
Presult: PTP;
I, Count: integer;
Begin
Count: = getselectcount; // assume that the function added by DLL returns the number of arrays.
Getmem (presult, sizeof (TPS) * count );
Try
I: = select (presult );
Showmessage (tparray (presult) [0]. fjcode );
Showmessage (tparray (presult) [1]. fjcode );
Finally
Freemem (presult, sizeof (TPS) * count );
End;
End;
I have fully tested it. No problem:
Library project2;
Uses
Sysutils,
Classes;
Type
TPS = packed record
Sno: integer; // serial number
Fjcode: array [0 .. 199] of char;
End;
PTP = ^ TPS;
Tparray = array of TPS;
Function select (presult: PTP): integer; export; stdcall;
Begin
Tparray (presult) [0]. Sno: = 1;
Tparray (presult) [0]. fjcode: = 'day ';
Tparray (presult) [1]. Sno: = 2;
Tparray (presult) [1]. fjcode: = '小 ';
Result: = 1;
End;
Function getselectcount: integer; export; stdcall;
Begin
Result: = 2;
End;
{$ R *. Res}
Exports
Select,
Getselectcount;
Begin
End.
Type
TPS = packed record
Sno: integer; // serial number
Fjcode: array [0 .. 199] of char;
End;
PTP = ^ TPS;
Tparray = array of TPS;
Function getselectcount: integer; stdcall; External 'project2. dll ';
Function select (presult: PTP): integer; stdcall; External 'project2. dll ';
Procedure tform1.button1click (Sender: tobject );
VaR
Presult: PTP;
I, Count: integer;
Begin
Count: = getselectcount; // assume that the function added by DLL returns the number of arrays.
Getmem (presult, sizeof (TPS) * count );
Try
I: = select (presult );
Showmessage (tparray (presult) [0]. fjcode );
Showmessage (tparray (presult) [1]. fjcode );
Finally
Freemem (presult, sizeof (TPS) * count );
End;
End;
Maozefa (AFA) Thank you. The result is displayed based on your method! But I want to ask the following question:
Type
TPS = packed record
Sno: integer; // serial number
Fjcode: array [0 .. 199] of char;
End;
PTP = ^ TPS;
Tparray = array of TPS;
According to your definition of this method and the use of DLL (that is, array of...) when other languages call the dll I wrote in Delphi, will it be a problem?
Thank you for your reply. You are my benefactor...
No problem. Other languages, such as C/C ++ pointers, can directly use the lower mark.
In addition, the function select (presult: PTP): integer; export; stdcall;
Changed to function select (VAR presult: PTP): integer; export; stdcall;
You can use setlength to allocate the length of the array.
Procedure tform1.button1click (Sender: tobject );
VaR
Presult: array of TPS;
I, Count: integer;
Begin
Count: = getselectcount; // assume that the function added by DLL returns the number of arrays.
Setlength (presult, count );
I: = select (@ presult );
Showmessage (presult [0]. fjcode );
Showmessage (presult [1]. fjcode );
End;
However, do not change the function description in the program.
Or function select (presult: PTP): integer; stdcall; External 'project2. dll ';
The following definition makes it easier for other languages, such as C/C ++, to define the structure.
TPS = packed record
Thank you very much. I want to read and understand many things! Thank you!