程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> dll窗體及數據調用-delphi DLL數據及窗體調用

dll窗體及數據調用-delphi DLL數據及窗體調用

編輯:編程綜合問答
delphi DLL數據及窗體調用

DLL工程文件代碼:

library DLLUSERS;
uses
  Windows,
  ADODB,
  Dialogs,
  Forms,
  SysUtils,
  Classes,
  U_DataModule in 'U_DataModule.pas' {DataModule1: TDataModule},
  U_Users in 'U_Users.pas' {Frm_Users},
  U_Initialize in 'U_Initialize.pas';
{$R *.res}
function GetForm(ClassName: PChar; DM: TDataModule1): TFormClass; stdcall;
begin
  DataModule1 := DM;
  Result:=TFormClass(FindClass(ClassName));
end;

procedure InitDLL(DM: TDataModule1); stdcall;
begin
  DataModule1:=DM;
end;

exports
  GetForm,InitDLL,SetUseName;
begin
end. 

DLL公共單元代碼:

unit U_Initialize;
{DLL公共單元UNIT}
interface

  function GetUseName: PChar; stdcall;
  procedure SetUseName(SName: PChar); stdcall;

var
  StrName: PChar;

implementation

uses
  U_DataModule, ActiveX;

function GetUseName: PChar; stdcall;
begin
  Result:=StrName;
end;

procedure SetUseName(SName: PChar); stdcall;
begin
  StrName:=SName;
end;

initialization
  CoInitialize(nil);
  DataModule1 := TDataModule1.Create(nil);
finalization
  DataModule1.Free;
  CoUninitialize;

end. 

DLL數據模塊代碼:

 unit U_DataModule;
{數據模塊}
interface

uses
  SysUtils, Classes, DB, ADODB;

type
  TDataModule1 = class(TDataModule)
    ADOCNT: TADOConnection;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DataModule1: TDataModule1;

implementation

{$R *.dfm}

end.

DLL內部窗體代碼:

 unit U_Users;
{DLL內部窗體}
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBGridEhGrouping, ComCtrls, GridsEh, DBGridEh, ExtCtrls,
  RzPanel, Menus, ADODB, DB, EhlibCDS, EhlibADO, Comobj, DBGridEhImpExp,
  U_DataModule;

type
  TFrm_Users = class(TForm)
    MainMenu1: TMainMenu;
    mmAdd: TMenuItem;
    mmEdit: TMenuItem;
    mmDelete: TMenuItem;
    mmRight: TMenuItem;
    mmFind: TMenuItem;
    mmDataOut: TMenuItem;
    mmClose: TMenuItem;
    RzGroupBox1: TRzGroupBox;
    DBGridEhUsers: TDBGridEh;
    StatusBar1: TStatusBar;
    SaveDialog1: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    ADOUsers,ADODelete:TADOQuery;
    DSUsers: TDataSource;
    { Public declarations }
  end;

var
  Frm_Users: TFrm_Users;

implementation

uses
  U_Initialize;

{$R *.dfm}

procedure TFrm_Users.FormCreate(Sender: TObject);
begin
  Font.Name:='Arial';
  ADOUsers:=TADOQuery.Create(nil);
  ADODelete:=TADOQuery.Create(nil);
  DSUsers:=TDataSource.Create(nil);
  ADOUsers.Connection:=DataModule1.ADOCNT;
  ADODelete.Connection:=DataModule1.ADOCNT;
  //設置文件類型列表和默認文件類型
  SaveDialog1.Filter:='Text files (*.txt)|*.TXT|Comma separated values (*.csv)|*.CSV|HTML file (*.htm)|*.HTM|Rich Text Format (*.rtf)|*.RTF|Microsoft Excel Workbook (*.xls)|*.XLS';
  SaveDialog1.FilterIndex:=0;
end;

procedure TFrm_Users.FormShow(Sender: TObject);
begin
  StrName:=GetUseName;
  with  ADOUsers  do
  begin
    Close;
    SQL.Clear;
    if String(StrName)='alsaby' then
    SQL.Add('select a.*,b.Person_Name,c.Partment_Name from t_User a '+
            'left join t_Person b on a.User_PersonId=b.Person_Id '+
            'left join t_Partment c on a.User_PartmentId=c.Partment_Id '+
            'order by a.User_Name') else
    if String(StrName)='admin' then
    SQL.Add('select a.*,b.Person_Name,c.Partment_Name from t_User a '+
            'left join t_Person b on a.User_PersonId=b.Person_Id '+
            'left join t_Partment c on a.User_PartmentId=c.Partment_Id '+
            'where a.User_Name<>''alsaby'' order by a.User_Name') else   
    SQL.Add('select a.*,b.Person_Name,c.Partment_Name from t_User a '+
            'left join t_Person b on a.User_PersonId=b.Person_Id '+
            'left join t_Partment c on a.User_PartmentId=c.Partment_Id '+
            'where a.User_Name<>''alsaby'' and a.User_Name<>''admin'' order by a.User_Name');
    Open;
  end;
  DSUsers.DataSet:=ADOUsers;
  DBGridEhUsers.DataSource:=DSUsers;
  StatusBar1.Panels[1].Text:=IntToStr(ADOUsers.RecordCount) +' 條數據。';
end;

procedure TFrm_Users.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ADOUsers.Close;
  ADOUsers.Destroy;
  ADODelete.Close;
  ADODelete.Destroy;
  DSUsers.Destroy;
  Action:=caFree;
end;

initialization
  RegisterClass(TFrm_Users);
finalization
  UnRegisterClass(TFrm_Users);
end.

主程序調用代碼:

 unit U_Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ImgList, ComCtrls, ADODB, DB, jpeg, ExtCtrls, RzPanel,
  RzSplit, RzTreeVw, U_DataModule;

type
  TInitDLL = procedure(DM: TFrm_DataModule); stdcall;
  TSetUseName = procedure(SName: PChar); stdcall;
  TGetForm = function(ClassName: PChar; DM: TFrm_DataModule): TFormClass; stdcall;
  TFrm_Main = class(TForm)
    MainMenu1: TMainMenu;
    mmSysFlies: TMenuItem;
    mmUserChange: TMenuItem;
    N2: TMenuItem;
    mmExit: TMenuItem;
    N1: TMenuItem;
    mmBakRecover: TMenuItem;
    mmSysUser: TMenuItem;
    N5: TMenuItem;
    StatusBar1: TStatusBar;
    OpenDialog1: TOpenDialog;
    ImageList1: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure mmSysUserClick(Sender: TObject);
  private
    { Private declarations }
  public
    UName: String;
    { Public declarations }
  end;

var
  Frm_Main: TFrm_Main;
implementation

uses
  U_Public;

{$R *.dfm}

procedure TFrm_Main.FormCreate(Sender: TObject);
begin
  Font.Name:='Arial';
  UName:=Frm_DataModule.ADO_User.FieldByName('User_Name').AsString;
end;

procedure TFrm_Main.mmSysUserClick(Sender: TObject);
var
  DLLName: String;
  DLLHandle: THandle;
  FarProc: TFarProc;
  Form: TForm;
  SetUseName: TSetUseName;
  GetForm: TGetForm;
  InitDLL: TInitDLL;
begin
  GetDir(0,DLLName);
  DLLName := DLLName + '\DLLUSERS.dll';
  DLLHandle:= SafeLoadLibrary(DLLName);
  if DLLHandle > 0 then
    Try
      FarProc := GetProcAddress(DLLHandle, 'InitDLL');
      if FarProc<>nil then
      begin
        InitDLL := TInitDLL(FarProc);
        InitDLL(Frm_DataModule);
      end;

      FarProc := GetProcAddress(DLLHandle, 'SetUseName');
      if FarProc<>nil then
      begin
        SetUseName := TSetUseName(FarProc);
        SetUseName(PChar(Trim(UName)));
      end;

      FarProc := GetProcAddress(DLLHandle, 'GetForm');
      if FarProc<>nil then
      begin
        GetForm := TGetForm(FarProc);
        Form := GetForm('TFrm_Users', Frm_DataModule).Create(nil);
        Form.ShowModal;
        FreeAndNil(Form);
      end;
    Finally
      FreeLibrary(DLLHandle);
    End
  else
  ShowMessage(DLLName+'文件不存在!');  
end;

end.

以上在運行程序時沒有錯誤,數據也正常顯示,但是關閉調用的DLL內部窗體後,再次通過主程序調用就出現了Read of Address 00000008錯誤,請高手指點這是咋回事?

最佳回答:


function GetForm(ClassName: PChar; DM: TDataModule1): TFormClass; stdcall;
begin
DataModule1 := DM;
Result:=TFormClass(FindClass(ClassName));
end;

procedure InitDLL(DM: TDataModule1); stdcall;
begin
DataModule1:=DM;
end;

傳遞了對象,是不可取的。必成傳遞TADOConnection的連接字符,就可以了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved