给你一个例子吧主程序调用代码: function LoadModule(ALibraryName: String; AFunc_ID: ShortString; AImplementType: String; AConnect: TSocketConnection): Boolean; var CallModule: TAiCallModule; sFilePath : string; sFuncVersion: string; strExePath,sFileName:string;iPos: Integer; begin if not ExistLibraryHandle(ALibraryName) then begin LibHandle := LoadLibrary(PChar(ALibraryName)); if LibHandle <> 0 then LibHandleList.Add(ALibraryName + ':' + IntToStr(LibHandle)); end;
@CallModule := GetProcAddress(LibHandle,'AiCallModule'); if @CallModule <> nil then begin if not CallModule(AFunc_ID,Application) then begin Result := False; Exit; end; end; Result := True; end;DLL代码:procedure DLLEntryPoint(dwReason: DWord); begin case dwReason of DLL_PROCESS_ATTACH: begin GAppSave := Application; end; DLL_PROCESS_DETACH: begin Application := GAppSave; AnyiClientDog.Free; AnyiClientDog := nil; end; end; end; function AiCallModule(AFunc_ID: ShortString; AppHandle: TApplication): Boolean; stdcall;export; begin Result := False; Application := AppHandle; if AFunc_ID = AFunc_ID_Log then begin if Application.FindComponent('FormLog') = nil then begin FormLog := TFormLog.Create(Application); end; FormLog.Show; Result := True; end; end;procedure AiFreeModule(AFunc_ID: ShortString); stdcall; export; begin if AFunc_ID = AFunc_ID_Log then begin if Application.FindComponent('FormLog') <> nil then FormLog.Close; end; end; exports AiCallModule, AiFreeModule; begin DllProc := @DLLEntryPoint; DLLEntryPoint(DLL_PROCESS_ATTACH); end.
上面的 AnyiClientDog.Free; AnyiClientDog := nil; 可不要
在动态连接库中的MDI子窗体:library MDIDll; var SaveDLLApp:TApplication;procedure ShowMDIChildForm(MainApp:TApplication); var Child:TMDIForm; begin if not Assigned(SaveDllApp) then begin SaveDllApp:=Application; Application:=MainApp; end; Child:=TMDIForm.Create(Application.MainForm); Child.Show; end;procedure MyLibraryProc(Reason:integer); begin if Reason=DLL_PROCESS_DETACH then if Assinged(SaveDllApp) then Application:=SaveDllApp; end;export ShowMDIChildForm;begin DllProc:=@MyLibraryProc; end. 在调用dll的应用中,用下述方法生MDI窗体; procedure Form1.Button1Click(Sender:Tobject); begin ShowMDIChildForm(Application); end;
将你的这些代码放到一个公用的unit就完了,实在喜欢用动态连接库也可以使用Delphi的运行时包阿,怎么老是喜欢dll?将VCL放到dll本身就有很多问题(否则Borland也不会提出包的概念了)。其实包也就是一个类似dll的库阿。—————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
—————————————————————————————————
并且我也在作的一个小软件里用这种方法。我这有个修改过的例子,在d6+sp2上成功。
要得话留个Mail。
function LoadModule(ALibraryName: String; AFunc_ID: ShortString; AImplementType: String; AConnect: TSocketConnection): Boolean;
var
CallModule: TAiCallModule;
sFilePath : string;
sFuncVersion: string; strExePath,sFileName:string;iPos: Integer;
begin
if not ExistLibraryHandle(ALibraryName) then
begin
LibHandle := LoadLibrary(PChar(ALibraryName));
if LibHandle <> 0 then
LibHandleList.Add(ALibraryName + ':' + IntToStr(LibHandle));
end;
@CallModule := GetProcAddress(LibHandle,'AiCallModule');
if @CallModule <> nil then
begin
if not CallModule(AFunc_ID,Application) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;DLL代码:procedure DLLEntryPoint(dwReason: DWord);
begin
case dwReason of
DLL_PROCESS_ATTACH:
begin
GAppSave := Application;
end;
DLL_PROCESS_DETACH:
begin
Application := GAppSave;
AnyiClientDog.Free;
AnyiClientDog := nil;
end;
end;
end;
function AiCallModule(AFunc_ID: ShortString; AppHandle: TApplication): Boolean; stdcall;export;
begin
Result := False;
Application := AppHandle;
if AFunc_ID = AFunc_ID_Log then
begin
if Application.FindComponent('FormLog') = nil then
begin
FormLog := TFormLog.Create(Application);
end;
FormLog.Show;
Result := True;
end;
end;procedure AiFreeModule(AFunc_ID: ShortString); stdcall; export;
begin
if AFunc_ID = AFunc_ID_Log then
begin
if Application.FindComponent('FormLog') <> nil then
FormLog.Close;
end;
end;
exports
AiCallModule,
AiFreeModule;
begin
DllProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
AnyiClientDog.Free;
AnyiClientDog := nil;
可不要
var
SaveDLLApp:TApplication;procedure ShowMDIChildForm(MainApp:TApplication);
var
Child:TMDIForm;
begin
if not Assigned(SaveDllApp) then
begin
SaveDllApp:=Application;
Application:=MainApp;
end;
Child:=TMDIForm.Create(Application.MainForm);
Child.Show;
end;procedure MyLibraryProc(Reason:integer);
begin
if Reason=DLL_PROCESS_DETACH then
if Assinged(SaveDllApp) then
Application:=SaveDllApp;
end;export ShowMDIChildForm;begin
DllProc:=@MyLibraryProc;
end. 在调用dll的应用中,用下述方法生MDI窗体;
procedure Form1.Button1Click(Sender:Tobject);
begin
ShowMDIChildForm(Application);
end;