执行程序部分:
   TCard_RW = function(App: TApplication; AValue: Pdouble; AName,
    ALocal: PChar; AMaxValue: Double; ResultChar: PChar): integer; stdcall;procedure TForm1.Button1Click(Sender: TObject);
var Dllpath: string;
  Moudle: THandle;
  Pfunc: TCard_RW;
  Aint:Double;
  PA:PChar;
  SAveApp:TApplication;
begin
  Dllpath := 'D:\work\test_Interface.dll';
  aint:=1243.45;
  SAveApp:=Application;  Moudle := Loadlibrary(PChar(dllPath));
  GetMem(PA,255);
  try
    if Moudle <> 0 then
    begin
      @Pfunc := GetProcAddress(Moudle, 'Card_RW');
      if @Pfunc <> nil then
        begin
            ADOConnection1.Open();        Pfunc(Application,@Aint,'admin','001',5000,PA);
          ADOConnection1.Close;
         end;
    end;
  finally
    //  Application:=SAveApp;      FreeMem(PA);
    Freelibrary(Moudle);
  end;end;动态库 部分:function Card_RW(App: TApplication; AValue: Pdouble; AName, ALocal: PChar; AMaxValue: Double; ResultChar: PChar): integer; stdcall;
var Form: TConSumeForm;
  Aint: Double;
  Astr: string;
var OraApp: TApplication;
begin
 
  Result := 1;
  OraApp := App;
  Application := App;  coinitialize(nil);  DllPath := PChar(ExtractFilePath(Application.ExeName) + 'test_Interface');  Card_DataM := TCard_DataM.Create(nil);
  try
    with Card_DataM do
    begin      if not ConnectToServer then Exit;
      if not LoadMsg then Exit;      form := TConSumeForm.Create;
      try
        with form do
        begin
          SMaxValue := AMaxValue;
          LoadName := 'FrontFormDisp_Interface';
          Con_Value.Value := avalue^;
          if ShowModal = mrok then
          begin
            Astr := '卡号:' + sid.Text + ' 姓名:' + c_name.Text + ' 卡内余额:' + floattostr(SValue.Value - con_value.Value) +
              ' 有效期:' + ENDDATE.Text;
            StrCopy(ResultChar, PChar(Astr));
            aint := con_value.Value;
            AValue := @aint;
            Result := 0;          end else Result := 1;
        end;
      finally
        FreeAndNil(Form);
      end;
    end;  finally
    counInitialize;
    FreeAndNil(Card_DataM);      end;end;现象:执行测试程序通过动态库生成一个窗口,关闭这个窗口后,测试程序原来的窗口消失,请教如后解决

解决方案 »

  1.   

    问题出在Application := App;上。你Free窗体后没有还原主程序。
      

  2.   

    同意,最大的原因是你把exe里的application指针传给了dll,然后,把dll里的application指向了exe对象,而在dll被释放掉时,dll的application就被释放掉了,也就是把exe的application变量给释放掉了。
    要么把这句
    application := app去掉
    要么是在函数返回前用
    application := nil;
    不要让dll动链库自动释放这个指针所指向的对象可能也有其它的办法,嘿嘿,我就不会了
      

  3.   

    楼主的问题不在于传了App这个参数,而在于没把exe的Application的Handle传给dll的Application,所以ShowModal之后,没了可激活到前台的窗口!改成这样就Ok了!TCard_RW = function(AppHandle: THandle; AValue: Pdouble; AName, 
        ALocal: PChar; AMaxValue: Double; ResultChar: PChar): integer; stdcall;
    exe调用: Card_RW(Application.Handle,....);或者(Application.MainForm.Handle,....);
    dll部分:Application.Handle := AppHandle;
    DllPath := PChar(ExtractFilePath(ParamStr(0)) + 'test_Interface'); Card_DataM := TCard_DataM.Create(Application);
    try
    finally
      Card_DataM.Free;
    end;