Dll 학습3Dll 상호 및 마스터 간의 데이터 공유 - 테스트가 통과되지 않았으므로 메모리로 매핑해야 합니다.

6514 단어 dll
테스트 환경: XP, DELPHI XE
유효성 검사 통과 구조: 주 프로그램 + Dll 창
공유 방식의 원리: 메인 프로그램과 각 Dll이 정의한 같은 매개 변수 구조체를 통해 메인 프로그램이 이 구조체를 실례화하고 각 Dll 간의 공유에 대해 메인 프로그램의 실례화된 구조체 지침을 통해 각 Dll과 메인 프로그램 간의 데이터 공유에 도달한다.Dll 방출은 마스터 프로그램의 구조체 인스턴스화에 영향을 주지 않는 메모리 공간
주 프로그램 코드:
unit Main_Unit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TPara = record
    ADOConnStr: String;
  end;
  TCreateFrm = procedure(AppHnd: THandle; APar: TPara); stdcall;		//                 
  TCreateSubFrm = procedure(AppHnd: THandle); stdcall;
  TDropFrm = procedure; stdcall;
  TFrm_Main = class(TForm)
    Btn_1: TButton;
    Btn_2: TButton;
    Btn_3: TButton;
    Btn_4: TButton;
    Btn_5: TButton;
    Btn_6: TButton;
    Btn_7: TButton;    procedure Btn_1Click(Sender: TObject);
    procedure Btn_2Click(Sender: TObject);
    procedure Btn_3Click(Sender: TObject);
    procedure Btn_4Click(Sender: TObject);
    procedure Btn_5Click(Sender: TObject);
    procedure Btn_6Click(Sender: TObject);
    procedure Btn_7Click(Sender: TObject);
  private
    LibHandle: THandle;
    FormRef: LongInt;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Frm_Main: TFrm_Main;
  APara: TPara;

implementation

{$R *.dfm}

procedure TFrm_Main.Btn_1Click(Sender: TObject);
begin
  if LibHandle = 0 then
  begin
    LibHandle := SafeLoadLibrary('SubMain.dll');
    if LibHandle = 0 then
      raise Exception.Create('Not Found Dll File')
    else
      ShowMessage('Dll Loaded');
  end;
end;

procedure TFrm_Main.Btn_2Click(Sender: TObject);
begin
  if LibHandle > 0 then
  begin
    FreeLibrary(LibHandle);
    LibHandle := 0;
    ShowMessage('Dll UnLoaded');
  end;
end;

procedure TFrm_Main.Btn_3Click(Sender: TObject);
var
  CreateFrm: TCreateFrm;
begin
  if LibHandle = 0 then
    raise Exception.Create('Place Load Dll File First');

  @CreateFrm := GetProcAddress(LibHandle,PChar('CreateFrm'));
  if @CreateFrm = nil then
    raise Exception.Create('Function Error');

  APara.ADOConnStr := 'Provider=SQLOLEDB.1;Password=*****;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=127.0.0.1';	//     
  CreateFrm(Application.Handle,APara);
end;

procedure TFrm_Main.Btn_4Click(Sender: TObject);
var
  DropFrm: TDropFrm;
begin
  @DropFrm := GetProcAddress(LibHandle,PChar('DropFrm'));
  if @DropFrm = nil then
    raise Exception.Create('Function Error');

  DropFrm;
end;

procedure TFrm_Main.Btn_5Click(Sender: TObject);
var
  CreateSubFrm: TCreateSubFrm;
begin
  if LibHandle = 0 then
    raise Exception.Create('Place Load Dll File First');

  @CreateSubFrm := GetProcAddress(LibHandle,PChar('CreateSubFrm'));
  if @CreateSubFrm = nil then
    raise Exception.Create('Function Error');

  CreateSubFrm(Application.Handle);
end;

procedure TFrm_Main.Btn_7Click(Sender: TObject);
begin
  ShowMessage(APara.ADOConnStr);			//    Dll ,               
end;

end.

Dll 참조 코드:
unit SubMain_Unit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, FyDataConn_Unit, ActiveX,
  ADODB, StdCtrls, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxStyles, dxSkinsCore, dxSkinBlueprint,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinHighContrast,
  dxSkinSevenClassic, dxSkinSharpPlus, dxSkinStardust, dxSkinTheAsphaltWorld,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinscxPCPainter, cxCustomData, cxFilter,
  cxData, cxDataStorage, cxEdit, cxNavigator, cxDBData, cxGridLevel, cxClasses,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, Grids, DBGrids,dxCore;

type
  TPara = record				//              
    ADOConnStr: String;
  end;
  TPPara = ^TPara;				//           
  TFrm_SubMain = class(TForm)
    Btn_1: TButton;
    GTV_1: TcxGridDBTableView;
    GL_1: TcxGridLevel;
    Grd_1: TcxGrid;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure Btn_1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    DSet: TADODataSet;
    DS: TDataSource;
    Conn: TADOConnection;
    { Private declarations }
  public
    { Public declarations }
  end;
  procedure CreateFrm(AppHnd: THandle; APar: TPara);export;stdcall;
  procedure DropFrm; export;stdcall;

var
  Frm_SubMain: TFrm_SubMain;
  LocPara: TPara;
  PPara: TPPara;

implementation

{$R *.dfm}
procedure CreateFrm(AppHnd: THandle; APar: TPara);
begin
  Application.Handle := AppHnd;
  PPara := @APar;		//  
  if not Assigned(Frm_SubMain) then
    Frm_SubMain := TFrm_SubMain.Create(Application);

  Frm_SubMain.Show;
end;

procedure DropFrm;
begin
  if Frm_SubMain <> nil then
    FreeAndNil(Frm_SubMain);
end;
procedure TFrm_SubMain.Btn_1Click(Sender: TObject);
var
  SQL: String;
begin
  DSet.Connection := Conn;
  DS.DataSet := DSet;
  SQL := 'Select * From Cg_CgDanSub';
  dbOpen(SQL,DSet);       //     ,       
  GTV_1.DataController.DataSource := DS;
  (GTV_1.DataController as IcxCustomGridDataController).DeleteAllItems;           //  cxGrid 
  (GTV_1.DataController as IcxCustomGridDataController).CreateAllItems(False);    //  cxGrid 

end;

procedure TFrm_SubMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TFrm_SubMain.FormCreate(Sender: TObject);
begin
  Conn := TADOConnection.Create(Application);
  Conn.LoginPrompt := False;
  Conn.ConnectionString := PPara.ADOConnStr;
  //Conn.ConnectionString := 'Provider=SQLOLEDB.1;Password=fydesign;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=127.0.0.1';
  Conn.Connected := True;
  DSet := TADODataSet.Create(Application);
  DS := TDataSource.Create(Application);
end;

procedure TFrm_SubMain.FormDestroy(Sender: TObject);
begin
  DSet.Free;
  DS.Free;
  FreeAndNil(Conn);
  Frm_SubMain := nil;
end;
initialization
  dxInitialize;
finalization
  dxFinalize;
end.

좋은 웹페이지 즐겨찾기