procedure TFrmMain.ActMk(MstrMK:string); //MstrMK:选择运行某个模块程序的编码 type TRunMK = function(jb:Thandle; MstrMk,MstrPath:Pchar):Boolean; stdcall; var Module : THandle; MbooleanVal : Boolean; RunMK : TRunMK; S : Pchar; begin MbooleanVal := False; S := Pchar(MstrMk); Module := LoadLibrary('project1.dll'); <------------放到别处试试 if Module > 32 then begin @RunMk := GetProcAddress(Module,'RunMK'); if not (@RunMk = nil) then MbooleanVal := RunMk(Application.Handle,S,PChar(GetPath(0))); if not MbooleanVal then showmessage('执行失败!'); end; FreeLibrary(Module); <----------------------------放到别处试试 end;
Module := LoadLibrary('project1.dll'); if Module > 32 then begin @RunMk := GetProcAddress(Module,'RunMK'); if not (@RunMk = nil) then MbooleanVal := RunMk(Application.Handle,S,PChar(GetPath(0))); //---问题 end;//---------再调用时传递Handle不要写Application.Handle,这样会有窗体显示问题 //---------我也碰到过因为传递Application.Handle出现显示问题
to xiaocha(小查) : 备份程序我看了,和上面的代码一样,我估计是中午有一次非法关机的事(停电),但我重新开机后一切都正常,编译程序都通过了,我尝试重写工程文件和RunUnit单元也不行。 下面是备份的RunUnit单元:unit RunUnit; interfaceuses Forms, Dialogs, SysUtils, Classes, ActiveX;function RunMK(jb:Thandle; MstrMk,MstrPath:Pchar): Boolean;stdcall;implementationuses AbUnit, PublicUNIT, SbbUnit, BbUnit, DMMainUnit, NormalUnit, Sc_Nssb_SjsbUnit, Sc_Jczl_SjjsUnit, Sc_XxbUnit, SC_XzZzjgUnit; function RunMK(jb:Thandle; MstrMk,MstrPath:Pchar): Boolean; { 功能:执行选择的模块程序 输入:Jb:调用的主程序句柄。 MstrMk : 要调用的模块名: MstrPath : 数据库路径。 输出:成功返回True;否则返回False; } var MjbVal : THandle; BEGIN MjbVal := application.Handle; application.Handle := jb; if MstrMk = 'Ab' then begin Application.CreateForm(TDMMain,DMMain); DMMain.JoinDS(MstrPath); AbForm := TAbForm.Create(Application); AbForm.ShowModal; DMMain.Free; end; if MstrMk = 'Bb' then begin Application.CreateForm(TDMMain,DMMain); DMMain.JoinDS(MstrPath); BbForm := TBbForm.Create(Application); BbForm.ShowModal; DMMain.Free; end; if MstrMk = 'Sbb' then begin Application.CreateForm(TDMMain,DMMain); DMMain.JoinDS(MstrPath); SbbForm := TSbbForm.Create(Application); SbbForm.ShowModal; DMMain.Free; end; Result := True; application.Handle := MjbVal; end;Initialization CoInitialize(nil);Finalization CoUnInitialize; end.
showmodal;
showmessage('aa');
Free;
procedure TFrmMain.ActMk(MstrMK:string);
//MstrMK:选择运行某个模块程序的编码
type
TRunMK = function(jb:Thandle; MstrMk,MstrPath:Pchar):Boolean; stdcall;
var
Module : THandle;
MbooleanVal : Boolean;
RunMK : TRunMK;
S : Pchar;
begin
MbooleanVal := False;
S := Pchar(MstrMk); Module := LoadLibrary('project1.dll');
if Module > 32 then
begin
@RunMk := GetProcAddress(Module,'RunMK');
if not (@RunMk = nil) then
MbooleanVal := RunMk(Application.Handle,S,PChar(GetPath(0))); if not MbooleanVal then showmessage('执行失败!');
end;
FreeLibrary(Module);
end;下面是DLL中响应的单元
unit RunUnit;
interfaceuses Forms,Dialogs, SysUtils, Classes, ActiveX;function RunMK(jb:Thandle;MstrMk,MstrPath:Pchar):Boolean;stdcall;implementationuses AbUnit, BbUnit, DMMainUnit, PublicUNIT, SbbUnit, Sc_Jczl_SjjsUnit,
Sc_Nssb_SjsbUnit, Sc_XxbUnit, SC_XzZzjgUnit;
function RunMK(jb:Thandle;MstrMk,MstrPath:Pchar):Boolean;
{ 功能:执行选择的模块程序
输入:Jb:调用的主程序句柄。
MstrMk : 要调用的模块名:
MstrPath : 数据库路径。
输出:成功返回True;否则返回False;
}
var
MJbVal : THandle;
begin
MJbVal := Application.Handle;
Application.Handle := jb; if MstrMk = 'Ab' then
begin
DMMain := TDMMain.Create(Application);
DMMain.JoinDS(MstrPath); AbForm := TAbForm.Create(Application);
AbForm.ShowModal; DMMain.Free;
end; if MstrMk = 'Bb' then
begin
//Application.CreateForm(TDMMain,DMMain);
DMMain := TDMMain.Create(Application);
DMMain.JoinDS(MstrPath); BbForm := TBbForm.Create(Application);
BbForm.ShowModal; DMMain.Free;
end; if MstrMk = 'Sbb' then
begin
//Application.CreateForm(TDMMain,DMMain);
DMMain := TDMMain.Create(Application);
DMMain.JoinDS(MstrPath); SbbForm := TSbbForm.Create(Application);
SbbForm.ShowModal; DMMain.Free;
end; if MstrMk = 'Sc_Nssb_SjSb' then
begin
Application.CreateForm(TDMMain,DMMain);
DMMain.JoinDS(MstrPath); Sc_Nssb_SjSbForm := TSc_Nssb_SjSbForm.Create(Application);
Sc_Nssb_SjSbForm.ShowModal; DMMain.Free;
end; if MstrMk = 'Sc_Jczl_Sjjs' then
begin
Application.CreateForm(TDMMain,DMMain);
DMMain.JoinDS(MstrPath); Sc_Jczl_SjjsForm := TSc_Jczl_SjjsForm.Create(Application);
Sc_Jczl_SjjsForm.ShowModal; DMMain.Free;
end; Result := True;
Application.Handle := MJbVal;
end;
Initialization
CoInitialize(nil);Finalization
CoUnInitialize;end.
{ 功能:执行选择的模块程序
输入:Jb:调用的主程序句柄。
MstrMk : 要调用的模块名:
MstrPath : 数据库路径。
输出:成功返回True;否则返回False;
}
var
MJbVal : THandle;
begin
MJbVal := Application.Handle; <-------把这一行注释掉
//MstrMK:选择运行某个模块程序的编码
type
TRunMK = function(jb:Thandle; MstrMk,MstrPath:Pchar):Boolean; stdcall;
var
Module : THandle;
MbooleanVal : Boolean;
RunMK : TRunMK;
S : Pchar;
begin
MbooleanVal := False;
S := Pchar(MstrMk); Module := LoadLibrary('project1.dll'); <------------放到别处试试
if Module > 32 then
begin
@RunMk := GetProcAddress(Module,'RunMK');
if not (@RunMk = nil) then
MbooleanVal := RunMk(Application.Handle,S,PChar(GetPath(0))); if not MbooleanVal then showmessage('执行失败!');
end;
FreeLibrary(Module); <----------------------------放到别处试试
end;
DMMain.JoinDS(...);
except
ShowMessage('abc');
end;试试
if Module > 32 then
begin
@RunMk := GetProcAddress(Module,'RunMK');
if not (@RunMk = nil) then
MbooleanVal := RunMk(Application.Handle,S,PChar(GetPath(0)));
//---问题 end;//---------再调用时传递Handle不要写Application.Handle,这样会有窗体显示问题
//---------我也碰到过因为传递Application.Handle出现显示问题
备份程序我看了,和上面的代码一样,我估计是中午有一次非法关机的事(停电),但我重新开机后一切都正常,编译程序都通过了,我尝试重写工程文件和RunUnit单元也不行。
下面是备份的RunUnit单元:unit RunUnit;
interfaceuses Forms, Dialogs, SysUtils, Classes, ActiveX;function RunMK(jb:Thandle; MstrMk,MstrPath:Pchar): Boolean;stdcall;implementationuses AbUnit, PublicUNIT, SbbUnit, BbUnit, DMMainUnit, NormalUnit,
Sc_Nssb_SjsbUnit, Sc_Jczl_SjjsUnit, Sc_XxbUnit, SC_XzZzjgUnit;
function RunMK(jb:Thandle; MstrMk,MstrPath:Pchar): Boolean;
{ 功能:执行选择的模块程序
输入:Jb:调用的主程序句柄。
MstrMk : 要调用的模块名:
MstrPath : 数据库路径。
输出:成功返回True;否则返回False;
}
var
MjbVal : THandle;
BEGIN
MjbVal := application.Handle;
application.Handle := jb; if MstrMk = 'Ab' then
begin
Application.CreateForm(TDMMain,DMMain);
DMMain.JoinDS(MstrPath); AbForm := TAbForm.Create(Application);
AbForm.ShowModal; DMMain.Free;
end; if MstrMk = 'Bb' then
begin
Application.CreateForm(TDMMain,DMMain);
DMMain.JoinDS(MstrPath); BbForm := TBbForm.Create(Application);
BbForm.ShowModal; DMMain.Free;
end; if MstrMk = 'Sbb' then
begin
Application.CreateForm(TDMMain,DMMain);
DMMain.JoinDS(MstrPath); SbbForm := TSbbForm.Create(Application);
SbbForm.ShowModal; DMMain.Free;
end; Result := True;
application.Handle := MjbVal;
end;Initialization
CoInitialize(nil);Finalization
CoUnInitialize;
end.