一段简单的调用dll的代码,写在按钮onclick中。
在delphi2010中编译代码,运行程序可以正常返回结果。
同样的代码,在delphi7中编译,调用过程都正常,但是就是没有返回结果。使用同一个调用dll的工程文件及代码,用Delphi2010打开编译,执行程序有返回结果,用Delphi7打开编译,执行程序就没有结果。调用dll的方式stdcall。

解决方案 »

  1.   

    代码如下unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TBulidIndex = function(sDir : string): BOOL; stdcall;  TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
        BulidIndex: TBulidIndex;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    var
      hdll:Integer;
    begin
      hdll := 0;
      hdll := LoadLibrary('ScanFileDll.dll');
      if hdll <> 0 then
      begin
        @BulidIndex := GetProcAddress(hdll, '_BulidIndexFileEx@4');
        BulidIndex('F:\test');
        FreeLibrary(hdll);
        showmessage('ok');
      end;
    end;end.
      

  2.   

    正常的返回结果应该是在"F:\test"目录下生成一个文件.但是现在用Delphi2010编译上面的代码生成的程序可以生成这个文件,但是用Delphi7编译后生成的程序就无法生成这个文件.
      

  3.   

    你的DLL是用什么编译的,同时用同一平台编译一下,应该就没有问题。这是平台的不一致造成的。
      

  4.   

    dll是vc写的,Delphi2010和Delphi7是在同一台电脑上.程序也是在这台电脑上测试的.调用的dll都是同一个文件.
      

  5.   

    TBulidIndex = function(sDir : string): BOOL; stdcall;dll的接口需要一个路径(string类型)作为输入参数,dll根据这个路径参数,在该路径下生成文件,生成成功返回true,失败返回false把上面的代码改成procedure TForm1.btn1Click(Sender: TObject);
    var
      hdll:Integer;
    begin
      //
      hdll := 0;
      hdll := LoadLibrary('ScanFileDll.dll');
      if hdll <> 0 then
      begin
        try
          @BulidIndex := GetProcAddress(hdll, '_BulidIndexFileEx@4');
          if BulidIndex('F:\test') then
          showmessage('ok');
        finally
          FreeLibrary(hdll);
        end;
      end;
    end;
    用Delphi7编译,返回"ok",但是F:\test目录中没有生成相应的文件,但是用Delphi2010编译之后就可以生成相应的文件.