我做了一个最简单的动态库。
DLL: 用一个名称为hi的function返回string 'hello'
调试程序:用一个按钮来调用动态库的函数hi结果.......我得到了hello,可是随后却报了一个invalid pointer operation!!!我没有用指针啊,Delphi老大,俺冤枉啊~~~~~~~~~~ 555555我的源程序如下,请大虾指点啊:
//***********************************************
//调试程序的 Project1
//************************************************
program Project1;uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.//***********************************************
//调试程序的 Unit1
//************************************************unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation
   function hi():string; external 'testdll.dll';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
      ShowMessage(Format('%x', [Longint(@hi)]));
      ShowMessage(hi());
end;end.//*********************************************
//  DLL端的 library testdll
//*********************************************
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,
  hello in 'hello.pas';{$R *.res}
    exports hi;
begin
end.//*********************************************
//  DLL端的 unit hello
//*********************************************
unit hello;interface
  uses Dialogs;
  function hi():string;
implementation
  function hi():string;
  begin
      hi := 'hello';
  end;end.

解决方案 »

  1.   

    function hi():string; external 'testdll.dll';stdcall;
    解决。
      

  2.   

    楼上是对的,另外,很重的是:
    在DLL中无论输出函数的返回值或者参数如果用String,必须把ShareMM放在DLL和调用DLL的程序Uses之后,并且分发程序时,brlndMem.DLL必须一起发放。
      

  3.   

    我加了stdcall还是没有用啊,还是错啊???string我得到了阿,hi函数正常运行了,可就是退出了调试程序的Unit1后出错~~~~~
      

  4.   

    在dll中也要申明成 function hi():string; stdcall;我看代码,应该不会再出错才对了阿,如果你有照楼上的几位说的修改!
    还有,你可试试这样看如何?
    procedure TForm1.Button1Click(Sender: TObject);
    var s: string;
    begin
          ShowMessage(Format('%x', [Longint(@hi)]));
          s := hi;
          ShowMessage(s);
          dispose(s);
    end;