用delphi写了个DLL,里面有些数据库的操作,用的是ADOQuery1,
DLL的出口函数是
function ShowRegion(hnd:THandle):string;
var
  Form:TForm1;
begin
  CoInitialize(nil);
  Application.Handle:=hnd;
  Form:=TForm1.Create(Application);
  try
    Form.ShowModal;
    Result:=Form.title;
  finally
    Form.Free;
  end;
  CoUnInitialize;
end;调用这个函数是的代码是
procedure TForm1.edRegionDblClick(Sender: TObject);
begin
var
  hnd:THandle;
  ShowRegion:TShowRegion;
begin
  hnd:=LoadLibrary('Region.dll');
  try
    if hnd<>0 then
    begin
      @ShowRegion:=GetProcAddress(hnd,'ShowRegion');
      if not (@ShowRegion=nil) then
        edRegion.Text:=ShowRegion(Application.Handle)
      else
        MessageDlg('没有找到出口函数,请确认', mtInformation,[mbOk], 0);
    end
    else
      MessageDlg('没有找到DLL,请确认', mtInformation,[mbOk], 0);
  finally
    FreeLibrary(hnd);
  end;     
end;为什么每次关闭程序的时候都会错误,但是调用的时候没错误

解决方案 »

  1.   

    Application.Handle:=hnd;
    这改变了application.handle,最后还要变回去好吧
    OldHnd := Application.Handle;
    Application.Handle:=hnd;
    try
     ...
    finally
      Application.Handle:=OldHnd;
    end;
      

  2.   

    function ShowRegion(hnd:THandle):string;
    var
      Form:TForm1;
    =======
    dll 里有 string pchar 等操作,需要 sharemem 单元在支持
      

  3.   

    函数返回值为string时,好像会出错,这个怎么办
      

  4.   

    我把程序改成这样子后,出现了新错误procedure TForm1.edRegionDblClick(Sender: TObject);
    var
      hnd:THandle;
      ShowRegion:TShowRegion;
    begin
      hnd:=LoadLibrary('Region.dll');
      try
        if hnd<>0 then
        begin
          @ShowRegion:=GetProcAddress(hnd,'ShowRegion');
          if not (@ShowRegion=nil) then
          begin
            edRegion.Text:=strpas(ShowRegion(Application.Handle));
          end
          else
            MessageDlg('没有找到出口函数,请确认', mtInformation,[mbOk], 0);
        end
        else
          MessageDlg('没有找到DLL,请确认', mtInformation,[mbOk], 0);
      finally
        FreeLibrary(hnd);
      end;
    end;DLL的出口函数为
    function ShowRegion(hnd:THandle):PChar;
    var
      Form:TForm1;
      OldHandle:THandle;
    begin
      CoInitialize(nil);
      OldHandle:=Application.Handle;
      Application.Handle:=hnd;
      Form:=TForm1.Create(Application);
      try
        Form.ShowModal;
        Result:=PChar(Form.returnvalues);
      finally
        Form.Free;
        CoUnInitialize;
        Application.Handle:=OldHandle;
      end;
    end;每次调用完DLL的时候出现"指令引用的“0x00000000”该内存不能为“read”,这个是怎么回事
      

  5.   

    不要用String做为返回值的类型,返回值一定要用Pchar类型,就不会出错了
      

  6.   

    函数类型问题,string应该是属于D特有类型,用PChar即可。PChar是指针。