T
twquac
Unregistered / Unconfirmed
GUEST, unregistred user!
问题一:
A为EXE,B为DLL,C为DLL
主程序(A)调用动态库B,B上有一事件可调用C,如果不进行参数传递到C,则C能否得知B所在文件路径?
问题二:
看下面代码:
library MachineInterface;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Windows,
System,
Forms,
dialogs,
Classes;
type
SetRegModeDll=function (MachineAddr:integer;RegMode:integer):integer;stdcall;
ClearAlarmClockDll=function (MachineAddr:integer):integer;stdcall;
Const
InterfaceFile=‘Test.dll';
var
DllHandle:THandle;
Dllfarproc:Tfarproc;
function SetRegMode(MachineAddr,ComPort,Baud,RegMode:integer):boolean;stdcall;
begin
Result:=False;
if DllHandle>32 then
if OpenComPort(ComPort,Baud) then
begin
DllfarProc:=GetProcAddress(DllHandle,'SetMode'); //获取函数入口
if DllfarProc<>Nil then
Result:=(SetRegModeDll(DllfarProc)(MachineAddr,RegMode)=0);
CloseComPort;
end;
end;
function ClearAlarmClock(MachineAddr,ComPort,Baud:integer):Boolean;stdcall;
begin
Result:=False;
if DllHandle>32 then
if OpenComPort(ComPort,Baud) then
begin
DllfarProc:=GetProcAddress(DllHandle,'ClearSirenTable'); //获取函数入口
if DllfarProc<>Nil then
Result:=(ClearAlarmClockDll(DllfarProc)(MachineAddr)=0);
CloseComPort;
end;
end;
{$R *.res}
exports
SetReadMode,ClearAlarmClock;
procedure DLLUnloadProc(Reason: Integer); register;
begin
{DLL_PROCESS_ATTACH:进程进入时
DLL_PROCESS_DETACH进程退出时
DLL_THREAD_ATTACH 线程进入时
DLL_THREAD_DETACH 线程退出时}
[red]if Reason = DLL_PROCESS_DETACH then
FreeLibrary(DllHandle);[/red]end; //为什么带了条件(if Reason = DLL_PROCESS_DETACH then),一调用DLL还是会执行FreeLibary(DllHandle),
begin
DllHandle:=LoadLibrary(InterfaceFile);
DLLProc := @DLLUnloadProc;
DLLUnloadProc(DLL_PROCESS_DETACH);
end.
我只想在退出调用DLL后再FreeLibary(InterfaceFile),到底该如何写代码?
A为EXE,B为DLL,C为DLL
主程序(A)调用动态库B,B上有一事件可调用C,如果不进行参数传递到C,则C能否得知B所在文件路径?
问题二:
看下面代码:
library MachineInterface;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Windows,
System,
Forms,
dialogs,
Classes;
type
SetRegModeDll=function (MachineAddr:integer;RegMode:integer):integer;stdcall;
ClearAlarmClockDll=function (MachineAddr:integer):integer;stdcall;
Const
InterfaceFile=‘Test.dll';
var
DllHandle:THandle;
Dllfarproc:Tfarproc;
function SetRegMode(MachineAddr,ComPort,Baud,RegMode:integer):boolean;stdcall;
begin
Result:=False;
if DllHandle>32 then
if OpenComPort(ComPort,Baud) then
begin
DllfarProc:=GetProcAddress(DllHandle,'SetMode'); //获取函数入口
if DllfarProc<>Nil then
Result:=(SetRegModeDll(DllfarProc)(MachineAddr,RegMode)=0);
CloseComPort;
end;
end;
function ClearAlarmClock(MachineAddr,ComPort,Baud:integer):Boolean;stdcall;
begin
Result:=False;
if DllHandle>32 then
if OpenComPort(ComPort,Baud) then
begin
DllfarProc:=GetProcAddress(DllHandle,'ClearSirenTable'); //获取函数入口
if DllfarProc<>Nil then
Result:=(ClearAlarmClockDll(DllfarProc)(MachineAddr)=0);
CloseComPort;
end;
end;
{$R *.res}
exports
SetReadMode,ClearAlarmClock;
procedure DLLUnloadProc(Reason: Integer); register;
begin
{DLL_PROCESS_ATTACH:进程进入时
DLL_PROCESS_DETACH进程退出时
DLL_THREAD_ATTACH 线程进入时
DLL_THREAD_DETACH 线程退出时}
[red]if Reason = DLL_PROCESS_DETACH then
FreeLibrary(DllHandle);[/red]end; //为什么带了条件(if Reason = DLL_PROCESS_DETACH then),一调用DLL还是会执行FreeLibary(DllHandle),
begin
DllHandle:=LoadLibrary(InterfaceFile);
DLLProc := @DLLUnloadProc;
DLLUnloadProc(DLL_PROCESS_DETACH);
end.
我只想在退出调用DLL后再FreeLibary(InterfaceFile),到底该如何写代码?