在dll 封裝窗體

顯示dll中的窗體【模態顯示】設計說明:
1.  dll中函數及變量說明:
procedure  LoadForm(Handle:THandle;ADOCon: PChar);stdcall;
var
  sADOCon: String; // 數據庫連接字符串

function CurUserName: PChar;//取用戶名
Function CurUserPwd():PChar;// 取用戶密碼
procedure SetCurUserPwd(UserPwd: PChar);//【測試未通過】

procedure LoadForm(Handle: THandle; ADOCon: PChar);
begin
  Application.Handle:=Handle;
  sADOCon:= ADOCon;
  dllform:=Tdllform.Create(Application);
  try
    dllform.ShowModal;
  finally
    dllform.Free;
  end;
end;

function Tdllform.CurUserName: PChar;
var GetCurUserName: function: pchar;
begin
  @GetCurUserName := GetProcAddress(GetModuleHandle(pchar(application.Exename)), 'CurUserName');
  if Assigned(GetCurUserName) then 
    result := GetCurUserName
  else
    result := 'admin';
end;

function Tdllform.CurUserPwd: PChar;
var  CurUserPwd: function: pchar;
begin
  @CurUserPwd := GetProcAddress(GetModuleHandle(pchar(application.Exename)), 'CurUserPwd');
  if Assigned(CurUserPwd) then 
    result := CurUserPwd
  else
    result := 'admin';
end;

procedure Tdllform.SetCurUserPwd(UserPwd: PChar);
var  SetCurUserPwd: procedure(s:PChar);
begin
  @SetCurUserPwd := GetProcAddress(GetModuleHandle(pchar(application.Exename)), 'SetCurUserPwd');
  if Assigned(SetCurUserPwd) then 
    SetCurUserPwd(UserPwd);
end;

2.  主 窗體中調用dll 編寫說明:
type
  UserInfo=record
  UserNo,UserNa,UserPwd:string;
end;   // 為dll共享變量

  EDLLLoadError = class(Exception); 連接異常類
  T_ShowModel =procedure  (h:THandle; s:PChar);stdcall; //過程類模態窗體
  T_ShowChild =procedure  (pMainApp:Pointer); stdcall;//非模態窗體

導出函數:
  Function CurUserName():PChar; stdcall; // 當前用戶名
  Function CurUserPwd():PChar; stdcall;  //當前用戶密碼
  procedure SetCurUserPwd(UserPwd: PChar); stdcall;// 設置當前用戶密碼【測試未通過】
  注:dpr 中:exports  CurUserName,CurUserPwd,SetCurUserPwd;

模態方式顯示窗體過程:
Procedure ShowModelForm(sDllFiles,sDllProc: string);// 動態裝入dll中模態窗體
var
  DllHandle:THandle;
  LoadFrom:T_ShowModel;
  sADOCon:string;
  begin
    sADOCon:=ADOConnection1.ConnectionString;
    DllHandle:=LoadLibrary(PChar(sDllFiles));
    try
      if DllHandle=0 then
        raise EDLLLoadError.Create('Dll 裝載失敗,dll文件為'  +   sDllFiles);
      @LoadFrom:=GetProcAddress(DllHandle,PChar(sDllProc));
      if Assigned(@LoadFrom) then
        LoadFrom(Self.Handle,PChar(sADOCon))
      else
        raise EDLLLoadError.Create('Dll 入口函數錯誤!');
    finally
      FreeLibrary(DllHandle);
    end;
  end;
導 出函數:
function CurUserName: PChar;//取當前用戶名
begin
  Result:=pchar(sUserInfo.UserNo);
end;

function CurUserPwd: PChar;// 取當前用戶密碼
begin
  Result:=pchar(sUserInfo.UserPwd);
end;

procedure SetCurUserPwd(UserPwd: PChar);// 更新當前用戶密碼
begin
  sUserInfo.UserPwd:=UserPwd;
end;
posted @ 2010-07-07 13:18  iAdo  阅读(242)  评论(0编辑  收藏  举报