I couldn't help myself in the morning. I begged everyone for help...

Source: Internet
Author: User
I couldn't help myself in the morning. I begged everyone for help... Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiDB/html/delphi_20061218121845242.html
Library mydll;

Uses
Sharemem,
Sysutils,
Stdctrls;
VaR
Fjmain: TADODataSet;

Type
TPS = record
Sno: integer;
Fjcode: pchar;
Fjname: pchar ;//
End;
TP = array of TPS;

{$ R *. Res}

Function select (transmode: pchar; var presult: TP): integer; export; stdcall;
VaR
Fjcode, T1, T2: pchar;
Totalnum: integer;
Begin
Fjmain: = TADODataSet. Create (NiL );
Ado configuration process... (OK)
Fjmain. Connection: = adoconnection;
Try
Fjmain. close;
Fjmain. commandtext: = 'select * From fjmain order by fjid ';
Fjmain. open;
T1: = pchar (fjmain. fieldbyname ('fjcode'). asstring );
T2: = pchar (fjmain. fieldbyname ('fjname'). asstring );
While not fjmain. EOF do
Begin
If (condition satisfied) then
Begin
Totalnum: = totalnum + 1;
Setlength (presult, totalnum );
Presult [totalNum-1]. Sno: = totalnum;
Presult [totalNum-1]. fjcode: = T1;
Presult [totalNum-1]. fjname: = t2;
End;
Fjmain. Next;
End;
Result: = totalnum;
Finally
Fjmain. Free;
Couninitialize;
End;
End;

Exports
Select;

Begin

End.

The above is my Code The problem is
First, I have a lot of records that meet the conditions in the fjmain table loop. I saw that the result is only the fjcode In the last record. The fjname string is correct, other characters are messy, or the first few letters are garbled. Why?
Second, when I call the DLL twice in a row, a memory error occurs. Why is the pchar variable t1 and t2 not released?

This problem has been fixed for one morning. I tried to change pchar and string repeatedly. The result is still memory problems. Please help me to see it. Program Where is there a problem?

What is the initial value of totalnum?

This is not the case. The initial value is 0.

Array of is an open array unique to Delphi.
As a DLL interface, I am afraid there is a problem

Try fieldbyname (). value directly?

What is the initial value of totalnum?

The initial value is 0. I have such a statement before. My problem is that the string is used, because once I remove setlength (presult, totalnum );
Presult [totalNum-1]. Sno: = totalnum;
Presult [totalNum-1]. fjcode: = T1;
Presult [totalNum-1]. fjname: = t2; these few words, how to run will not problem.

1. What is the initial value of totalnum?
2. T1, T2 is of the pchar type, and pchar is a pointer and cannot be used to store data. After fjmain. Next, it should point to an invalid address space.
3. array of is an open array unique to Delphi. As a DLL interface, there must be a problem.
4. Use setlength (presult, totalnum) repeatedly in a loop; allocate memory space, which may cause loss of the previously assigned records and garbled characters

Thank you for your concern and answers. Although I have not completely solved the problem, I have provided a good idea. I will close the post first. By the way, I will discuss the following questions, ask the eldest brother:
3. array of is an open array unique to Delphi. As a DLL interface, there must be a problem.
(My array of is used only separately defined in functions. It is not used as an interface between DLL programs and external programs. Can it be used ?)

My array of is only defined separately in functions. It is not used as an interface between DLL programs and external programs. Can it be used?
========================================================== ========================================================== ===
It should be okay. In addition, it should be noted that the output record address should be allocated before the program calls the DLL function. do not allocate it in the DLL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.