自己写的DLL函数,第二次执行就报错Invalid Pointer Operation

usa112233 2005-09-21 02:38:07
这是我的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
...全文
157 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
swayi21 2005-09-26
  • 打赏
  • 举报
回复
Delphi 都已经告诉你了,你还在问!!!

{ 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. }
usa112233 2005-09-22
  • 打赏
  • 举报
回复
高手们多指点指点啊!!
abc3000 2005-09-21
  • 打赏
  • 举报
回复
Function SFZ15To18(AiIDCard : String) : String; stdcall;
对,不要用string 建议用 shortstring (我用过pchar也报这个错,到现在也不明白为什么)
劣势 2005-09-21
  • 打赏
  • 举报
回复
问题还比较大的
Function SFZ15To18(AiIDCard : String) : String; stdcall;
用String还是有问题的,用PChar或者用ShortString都可以的,身份证号码可以用ShortString(256个字符)
Function SFZ15To18(AiIDCard : ShortString) : ShortString; stdcall;

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧