自己写的DLL函数,第二次执行就报错Invalid Pointer Operation
这是我的DLL源码
library testdll;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes;
{$R *.res}
Function SFZ15To18(AiIDCard : String) : String; stdcall;
Const
IDCardGene : Array[1..18] Of Integer = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
IDCardParity : Array[0..10] Of Char = ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
ValidLen = 15;
Var
sIDCard : String;
i, s : Integer;
Begin
Result := '';
s := 0;
sIDCard := Trim(AiIDCard);
If Length(sIDCard)=ValidLen Then
Begin
Insert('19', sIDCard, 7);
For i := 1 To ValidLen+2 Do
Begin
s := s + StrToInt(sIDCard[i]) * IDCardGene[i];
End;
Result := sIDCard + IDCardParity[(s Mod 11)]
End;
End;
exports
SFZ15To18;
end.
这是主程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function SFZ15To18(str:string):string;StdCall;
implementation
function SFZ15To18(str:string):string;stdcall;external 'testdll.dll' name 'SFZ15To18';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
sfz:string;
begin
sfz:=SFZ15To18(edit1.Text);
edit2.Text:=sfz;
end;
end.
点击按钮,第一次没问题,执行结果正确,继续点击第二次,就报错Invalid Pointer Operation