怎么使dll使像一个数据库应用程序一样具备窗体、按钮、数据表格等操作,并能被其它应用程序以dll方式调用!请给一个例子!

解决方案 »

  1.   

    DLL源代码:
    library Project2;uses
      SysUtils,
      Classes,
      Dialogs,
      Forms,
      Unit2 in 'Unit2.pas' {Form2};{$R *.RES}
    var
      ccc: Pchar;procedure OpenForm(mainForm:TForm);stdcall;
    var
      Form1: TForm1;
      ptr:PLongInt;
    begin
      ptr:=@(Application.MainForm);
      ptr^:=LongInt(mainForm);
      Form1:=TForm1.Create(mainForm);
    end;procedure InputCCC(Text: Pchar);stdcall;
    begin
      ccc := Text;
    end;procedure ShowCCC;stdcall;
    begin
      ShowMessage(String(ccc));
    end;exports
      OpenForm;
      InputCCC,
      ShowCCC;
    begin
    end.调用方源代码:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    procedure OpenForm(mainForm:TForm);stdcall;External'project2.dll';
    procedure ShowCCC;stdcall;External'project2.dll';
    procedure InputCCC(Text: Pchar);stdcall;External'project2.dll';procedure TForm1.Button1Click(Sender: TObject);
    var
      Text: Pchar;
    begin
      Text := Pchar(Edit1.Text);
    //  OpenForm(Application.MainForm);//为了调MDICHILD
      InputCCC(Text);//为了实验DLL中的全局变量是否在各个应用程序间共享
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      ShowCCC;//这里表明WINDOWS 32位应用程序DLL中的全局变量也是在应用程序地址空间中,16位应用程序或许不同,没有做实验。
    end;
      

  2.   

    看看这篇文章:
       
    ---- 在我们编制Delphi应用程序,常常需要提供一个密码验证框,对应用程序的使用者进行身份核对。如果能有一个通用的密码验证框,便能够为我们的工作节约不少的时间,更能使我们编制的程序小而快。以下的程序为你提供了这样的一个DLL文件。 //文件名为CheckPasswordForm.dpr;
    编译此文件生成CheckPasswordForm.dll
        library CheckPasswordForm;    uses
          XcqCheck in 'XcqCheck.pas' {XcqForm};    exports
          CheckPassword name 'CheckPassword';//dll入口    begin
        end.
        //文件名为XcqCheck.pas
        unit XcqCheck;    interface    uses
          Windows, Messages, SysUtils, Classes,
    Graphics, Controls, Forms, Dialogs,
          StdCtrls, Mask, Menus;    type
          TXcqForm = class(TForm)
          XcqEdit: TMaskEdit;//密码输入框
          OkBtn: TButton;
          procedure OkBtnClick(Sender: TObject);
        end;    var
          XcqForm: TXcqForm;
          Check: Boolean;//验证输入密码的正确性
          function CheckPassword: Boolean;export;
    //本dll文件的关键实现    implementation    {$R *.DFM}    procedure TXcqForm.OkBtnClick(Sender: TObject);
        begin
          if XcqEdit.Text = 'xcq' then
            Check := True;
          Close;
        end;    function CheckPassword;export;
        begin
          Check := False;
          XcqForm := TXcqForm.Create(Application);
    //创建密码输入框
          XcqForm.ShowModal;//显示密码输入框; 
    注意:不能用XcqForm.Show!
    Result := Check;//返回给调用本dll的应用程序判断
    XcqForm.Free;//释放本dll文件所占资源
        end;
        end.---- 在调用以上dll中CheckPassword函数的应用程序中只需加入以下语句在implementation 后,function CheckPassword: Boolean; external 'CheckPasswordForm.dll' name 'CheckPassword';在需要进行密码验证的地方加入下面的条件语句就完全实现了密码验证的功能。 
        if CheckPassword then 
        begin
          ...//如XcqQuery.Open;
        end;---- 上面的程序只是一个简单的密码验证框,我们可以将密码以写入注册表或文件的方式保存,动态修改,更可以利用算术算法对密码进行简单的加密等,使要保密的数据更安全。 
    ---- 其实,上文中如何在dll中实现窗口让我走了不少弯路,我觉得此法对大家更有意义,这是避免程序庞大的一种有效方法,不妨试试。 
      

  3.   

    几位侠士,虽然有窗体和按钮了,但在dll中怎样和数据库连接呀????????????????????给个例子后继续给分!!!
      

  4.   

    建立dll的窗体!
    {
    Copyright © 1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira
    }unit DLLFrm;interfaceuses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, Grids, Calendar;type  TDLLForm = class(TForm)
        calDllCalendar: TCalendar;
        procedure calDllCalendarDblClick(Sender: TObject);
      end;{ Declare the export function }
    function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;implementation
    {$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
    var
      DLLForm: TDllForm;
    begin
      // Copy application handle to DLL's TApplication object
      Application.Handle := AHandle;
      DLLForm := TDLLForm.Create(Application); 
      try
        DLLForm.Caption := ACaption;
        DLLForm.ShowModal;
        Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result
      finally
        DLLForm.Free;
      end;
    end;procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
    begin
      Close;
    end;end.