这是我的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

解决方案 »

  1.   

    问题还比较大的
    Function SFZ15To18(AiIDCard : String) : String; stdcall;
    用String还是有问题的,用PChar或者用ShortString都可以的,身份证号码可以用ShortString(256个字符)
    Function SFZ15To18(AiIDCard : ShortString) : ShortString; stdcall;
      

  2.   

    Function SFZ15To18(AiIDCard : String) : String; stdcall;
    对,不要用string  建议用 shortstring  (我用过pchar也报这个错,到现在也不明白为什么)
      

  3.   

    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. }