Why does the prompt "memory access error" appear after the DLL is used? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiDB/html/delphi_20061215160532289.html
I wrote a DLL myself and used Delphi to call the dll I wrote in Delphi. The running result can be displayed, but a memory access error will be prompted after running. During the second call, I directly reported a memory access error and failed to run. In Program The record set is used as the returned record.
Uses
Sharemem,
......
VaR
......
Type
TPS = record
Sno: integer;
Sname: string;
SKT: string;
End;
TP = array of TPS;
My function is
Function sel (S: integer, VAR presult: TP): integer; export; stdcall;
Begin
S: = 0;
......
If (condition satisfied) then
Begin
S: = S + 1;
Setlength (presult, S );
Presult [s-1]. Sno: = data;
Presult [s-1]. sname: = A data;
Presult [s-1]. STK: = A data;
End;
End;
Please help me to see what's wrong!
Replace string type with struct string
It may be otherCodeThe reason for this issue is to use the annotated method or conditions to compile and exclude them one by one.
I tried it, that is, changing it to pchar still won't work. It should be a problem with this code, because it was good before this code was added, and there was a problem when it was added, after running, the execution result is yes, but a memory error is reported. During the second operation, even if I couldn't even run it, I directly encountered an error.
1. Do not use sharemem if there is no string return value. (sharemem sometimes has problems and I have encountered it too)
2. Change string to string [250] // if the data length cannot exceed 250
Or change to Char [1000];
Do not change to pchar because it needs to apply for memory
Use the low high function to access the record Array
I generally use pchar for DLL access, but I only need to write a few more parameters to avoid many problems.
TP = array of TPS;
PTP = ^ TP;
Function sel (S: integer, VAR presult: PTP): integer; export; stdcall;
Use Pointer instead for TP