这是我的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;interfaceuses
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
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;interfaceuses
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
解决方案 »
- 为什么我自己写的函数和进程按住CTRL点鼠标它不会自动跳过去。(来者有分)
- 双击delphi工程文件可以打开一个新的delphi IDE环境吗?
- 为什么我的adodataset.nextrecordset要参数的?!
- 怎么合并两个指针指向的内容!谢谢各位大侠!急啊~~~~~~~~~~~~~~~~~~
- 高手请进:怎样用Delphi把EXCEL表导入数据库?
- 新手求教:怎样在一个运行时间长,结果多的程序中添加暂停和继续的功能?
- 文件操作
- 识别应用程序的简单问题!
- delphi 基础问题
- 如何把文本文件中一行數據讀到一數組中.
- 关于data module的问题(主要是打开表的时候就报错)
- 如果将控件移到最上层,超过Parnet的层级?
Function SFZ15To18(AiIDCard : String) : String; stdcall;
用String还是有问题的,用PChar或者用ShortString都可以的,身份证号码可以用ShortString(256个字符)
Function SFZ15To18(AiIDCard : ShortString) : ShortString; stdcall;
对,不要用string 建议用 shortstring (我用过pchar也报这个错,到现在也不明白为什么)
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. }