自己写了一个数学矩阵运算函数相关的dll,在dll初始是声明如下:
type
  Vector = array of double;type
  Matrix2= array of array of double;然后写了一个简单的测试函数
……写了些求矩阵转置,逆等相关函数,但未对外声明。
function test(inff:Matrix2):Double; stdcall;
begin
  result := inff[0,0];
end;建立了一个工程Project1
引入dll文件
function test(inff:Matrix2):Double; stdcall;
    external 'dllexample.dll';
添加一个按钮,按纽事件如下:procedure Tfmtest.Button4Click(Sender: TObject);
var
  tin:Matrix2;
  outff:Double;
begin
  SetLength(tin,4,2);
  tin[0,0]:=0.5;tin[0,1]:=1;
  tin[1,0]:=-1;tin[1,1]:=-1;
  tin[2,0]:=-1;tin[2,1]:=0.5;
  tin[3,1]:=0.5;tin[3,2]:=-1;  outff:=test(tin);
  Button4.Caption := FloatToStr(outff);
end;调用时出现"Invalid Pointer Operation"错误,但按钮的值已经改变为0.5。
设置断点,程序出错在“Button4.Caption := FloatToStr(outff);”这句。
不知道为什么,上网找了很多,但是大致都是说在dll中引用了string之类会出现这样的错误,我的dll中没有涉及到任何字符方面的使用,全是数值计算,而且我一开始就将ShareMem单元放在第一个引用单元。
还请高手指点迷津

解决方案 »

  1.   

    按照你的代码做了测试没有发现你说的情况。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;type 
      Vector = array of double; type 
      Matrix2= array of array of double;var
      Form1: TForm1;
    function test(inff:Matrix2):Double; stdcall; 
        external 'dllexample.dll';
    implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    var 
      tin:Matrix2; 
      outff:Double; 
    begin 
      SetLength(tin,4,2); 
      tin[0,0]:=0.5;tin[0,1]:=1; 
      tin[1,0]:=-1;tin[1,1]:=-1; 
      tin[2,0]:=-1;tin[2,1]:=0.5; 
      tin[3,1]:=0.5;tin[3,2]:=-1;   outff:=test(tin);
      btn1.Caption := FloatToStr(outff);
    end;end.library Project1;{ 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;type
      Vector = array of double;type
      Matrix2 = array of array of double;{$R *.res}function test(inff:Matrix2):Double; stdcall; 
    begin 
      result := inff[0,0]; 
    end; exports
      test;begin
    end. 
      

  2.   

    多谢1楼的回复,我也做了测试,发现问题好像和dll文件没什么关系,最后提取出问题的代码如下
    单独建立一个工程,添加一个按钮,写下如下代码,出现“Invalid Pointer Operation”错误。
    不知道为什么
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      Matrix2= array of array of double;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      tin:Matrix2;
      fin:Matrix2;
      rrr,temp,tem:Double;
      hehe:Double;
    begin
      SetLength(tin,4,2);
      SetLength(fin,1,2);
      tin[0,0]:=0.5;tin[0,1]:=1;
      tin[1,0]:=-1;tin[1,1]:=-1;
      tin[2,0]:=-1;tin[2,1]:=0.5;
      tin[3,1]:=0.5;tin[3,2]:=-1;
      fin[0,0]:=0.5;fin[0,1]:=1;
      hehe := tin[0,0];
      hehe:=hehe+1;
      Button1.Caption := FloatToStr(hehe);
    end;end.
      

  3.   

    找到了,问题出现在tin[3,1]:=0.5;tin[3,2]:=-1; 这一行上,应该改成tin[3,0]:=0.5;tin[3,1]:=-1;多谢pathletboy,辛苦了