问题是由这个帖子而引起的http://community.csdn.net/Expert/topic/3813/3813006.xml?temp=.4357874你一直说是多线程共享支持,在网上有一个关于DLL的初始化和释放的例子,大致内容如下:library TestDLL;procedure MyDLLHandler(Reason: integer);
begin
case Reason of
DLL_Process_Attach: //整个DLL的初始化代码
DLL_Process_Detach: //整个DLL的善後程序
DLL_Thread_Attach: //当主叫端开始一个Thread时
DLL_Thread_Detach: //当主叫端终止一个Thread时
end;
end;begin
DLLProc := @MyDLLHandler;
MyDLLHandle(DLL_Process_Attach);
end.
可是我调用从来没有发生过DLL_Thread_Attach 和 DLL_Thread_Detach 。
怎样才能触发呢?
给个例子吧!这方面我不熟啦!
begin
case Reason of
DLL_Process_Attach: //整个DLL的初始化代码
DLL_Process_Detach: //整个DLL的善後程序
DLL_Thread_Attach: //当主叫端开始一个Thread时
DLL_Thread_Detach: //当主叫端终止一个Thread时
end;
end;begin
DLLProc := @MyDLLHandler;
MyDLLHandle(DLL_Process_Attach);
end.
可是我调用从来没有发生过DLL_Thread_Attach 和 DLL_Thread_Detach 。
怎样才能触发呢?
给个例子吧!这方面我不熟啦!
begin
case Reason of
DLL_Process_Attach: //整个DLL的初始化代码
DLL_Process_Detach: //整个DLL的善後程序
DLL_Thread_Attach: //当主叫端开始一个Thread时
DLL_Thread_Detach: //当主叫端终止一个Thread时
end;
end;begin
DLLProc := @MyDLLHandler;
MyDLLHandle(DLL_Process_Attach);//这里传进个常量?那不是总是执行初始化那段吗?
end.
以普Proc_att就足够用了http://lysoft.7u7.net
//DLL_Thread_Detach在有些情况下则不一定会发生。。
//下面是我刚测试用的代码。
library TestDLL;uses
SysUtils,
Classes,windows;{$R *.res}
const
DLL_PROCESS_DETACH = 0;
DLL_PROCESS_ATTACH = 1;
DLL_THREAD_ATTACH = 2;
DLL_THREAD_DETACH = 3;procedure WriteLog(s:string);
var
logname:string;
fo:TFileStream;
tmp:string;
threadid:Cardinal;
begin
logname:='d:\testdll.log';
if FileExists(logname) then
begin
fo:=TFileStream.Create(logname,fmOpenWrite);
fo.Position:=fo.Size;
end
else
begin
fo:=TFileStream.Create(logname,fmCreate);
end;
try
threadid:=GetCurrentThreadId;
tmp:=DateTimeToStr(now)+'(threadid:'+IntToStr(threadid)+')"'+s+'"'#13#10;
fo.Write(pchar(tmp)^,length(tmp));
finally
fo.Free;
end;
end;procedure DoProcessAttach;
begin
WriteLog('ProcessAttach');
end;
procedure DoProcessDetach;
begin
WriteLog('ProcessDetach');
end;
procedure DoThreadAttach;
begin
WriteLog('ThreadAttach');
end;
procedure DoThreadDetach;
begin
WriteLog('ThreadDetach');
end;procedure MyDLLHandler(Reason: integer);
begin
case Reason of
DLL_PROCESS_ATTACH: DoProcessAttach;//整个DLL的初始化代码
DLL_PROCESS_DETACH: DoProcessDetach;//整个DLL的善後程序
DLL_THREAD_ATTACH: DoThreadAttach;//当主叫端开始一个Thread时
DLL_THREAD_DETACH: DoThreadDetach;//当主叫端终止一个Thread时
end;
end;begin
DLLProc := @MyDLLHandler;
MyDLLHandler(DLL_Process_Attach);
end.
//BUTTON1按下,主线程载入DLL。。
//BUTTON2按下,主线程释放DLL。。
//BUTTON3按下,创建一个线程,自生自灭,不做什么事。//当先按下BUTTON1,然后按下BUTTON3。就可以去看看日志文件了。。
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TTestThread=class(TThread)
protected
procedure Execute;override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
dllhandle:THandle;procedure TForm1.Button1Click(Sender: TObject);
begin
dllhandle:=LoadLibrary('TestDll.dll');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
FreeLibrary(dllhandle);
end;procedure TTestThread.Execute;
begin
FreeOnTerminate:=true;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
TTestThread.Create(false);
end;
In the body of the function, you may handle any combination of the following scenarios in which the DLL entry point has been called:
A process loads the DLL (DLL_PROCESS_ATTACH).
The current process creates a new thread (DLL_THREAD_ATTACH).
A thread exits normally (DLL_THREAD_DETACH).
A process unloads the DLL (DLL_PROCESS_DETACH). The entry-point function should perform only simple initialization tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary) during process termination, because this can result in a DLL being used after the system has executed its termination code.Because Kernel32.dll is guaranteed to be loaded in the process address space when the entry-point function is called, calling functions in Kernel32.dll does not result in the DLL being used before its initialization code has been executed. Therefore, the entry-point function can create synchronization objects such as critical sections and mutexes, and use TLS, because these functions are located in Kernel32.dll. It is not safe to call the registry functions, for example, because they are located in Advapi32.dll.Calling other functions may result in problems that are difficult to diagnose. For example, calling User, Shell, and COM functions can cause access violation errors, because some functions in their DLLs call LoadLibrary to load other system components. Conversely, calling those functions during termination can cause access violation errors because the corresponding component may already have been unloaded or uninitialized.
经过我的反复测试,发现,其实还是这里调用的……library TestDLL;procedure MyDLLHandler(Reason: integer);
begin
case Reason of
DLL_Process_Attach: //整个DLL的初始化代码
DLL_Process_Detach: //整个DLL的善後程序
DLL_Thread_Attach: //当主叫端开始一个Thread时
DLL_Thread_Detach: //当主叫端终止一个Thread时
end;
end;begin
DLLProc := @MyDLLHandler;
MyDLLHandle(DLL_Process_Attach);//就是这里!
end.
你写啥就是啥了-_-!晕死我了,呵呵先下班,回去接着试去……
2005-03-03 11:44:53(threadid:2060)"ThreadAttach"
2005-03-03 11:44:53(threadid:2060)"ThreadDetach"
2005-03-03 11:44:54(threadid:704)"ProcessDetach"
不是研究,只是以前这个用法不知道,正好在这看见了,我就顺便试试,学习学习,
没想到一试试出个我的delphi编译有问题…… 真TMD郁闷阿