帮我解释一下这个函数及参数DllEntryPoint???

yulei0707 2002-11-22 01:34:53
BOOL WINAPI DllEntryPoint(

HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
);


Parameters

hinstDLL

A handle to the DLL. The value is the base address of the DLL. The HINSTANCE of a DLL is the same as the HMODULE of the DLL, so hinstDLL can be used in subsequent calls to the
GetModuleFileName function and other functions that require a module handle.

fdwReason

Specifies a flag indicating why the DLL entry-point function is being called. This parameter can be one of the following values:

Value Meaning
DLL_PROCESS_ATTACH
Indicates that the DLL is attaching to the address space of the current process as a result of the process starting up or as a result of a call to LoadLibrary. DLLs can use this opportunity to initialize any instance data or to use the TlsAlloc function to allocate a thread local storage (TLS) index.
During initial process startup or after a call to LoadLibrary, the operating system scans the list of loaded DLLs for the process. For each DLL that has not already been called with the DLL_PROCESS_ATTACH value, the system calls the DLL's entry-point function. This call is made in the context of the thread that caused the process address space to change, such as the primary thread of the process or the thread that called LoadLibrary.
DLL_THREAD_ATTACH
Indicates that the current process is creating a new thread. When this occurs, the system calls the entry-point function of all DLLs currently attached to the process. The call is made in the context of the new thread. DLLs can use this opportunity to initialize a TLS slot for the thread. A thread calling the DLL entry-point function with the DLL_PROCESS_ATTACH value does not call the DLL entry-point function with the DLL_THREAD_ATTACH value.
Note that a DLL's entry-point function is called with this value only by threads created after the DLL is attached to the process. When a DLL is attached by LoadLibrary, existing threads do not call the entry-point function of the newly loaded DLL.
DLL_THREAD_DETACH
Indicates that a thread is exiting cleanly. If the DLL has stored a pointer to allocated memory in a TLS slot, it uses this opportunity to free the memory. The operating system calls the entry-point function of all currently loaded DLLs with this value. The call is made in the context of the exiting thread. There are cases in which the entry-point function is called for a terminating thread even if the DLL never attached to the thread ?for example, the entry-point function was never called with the DLL_THREAD_ATTACH value in the context of the thread in either of these two situations:?The thread was the initial thread in the process, so the system called the entry-point function with the DLL_PROCESS_ATTACH value. ?The thread was already running when a call to the LoadLibrary function was made, so the system never called the entry-point function for it.


DLL_PROCESS_DETACH
Indicates that the DLL is detaching from the address space of the calling process as a result of either a clean process exit or of a call to FreeLibrary. The DLL can use this opportunity to call the TlsFree function to free any TLS indices allocated by using TlsAlloc and to free any thread local data. When a DLL detaches from a process as a result of process termination or as a result of a call to FreeLibrary, the operating system does not call the DLL's entry-point function with the DLL_THREAD_DETACH value for the individual threads of the process. The DLL is only given DLL_PROCESS_DETACH notification. DLLs can take this opportunity to clean up all resources for all threads attached and known to the DLL.


lpvReserved

Specifies further aspects of DLL initialization and cleanup.
If fdwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads and non-NULL for static loads.
If fdwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if DllEntryPoint has been called by using FreeLibrary and non-NULL if DllEntryPoint has been called during process termination.

...全文
411 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gk0205 2002-12-12
  • 打赏
  • 举报
回复
好东西
invalid 2002-11-28
  • 打赏
  • 举报
回复
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
if(lpReserved)
OutputDebugString("DLL_PROCESS_ATTACH Static load");
else
OutputDebugString("DLL_PROCESS_ATTACH Dynamic load");
GetModuleFileName(hinst,dllpath,255);
break;
case DLL_PROCESS_DETACH:
if(lpReserved)
OutputDebugString("DLL_PROCESS_DETACH Call process termination!");
else
OutputDebugString("DLL_PROCESS_DETACH FreeLibrary");
break;
case DLL_THREAD_ATTACH:
OutputDebugString("DLL_THREAD_ATTACH");
break;
case DLL_THREAD_DETACH:
OutputDebugString("DLL_THREAD_DETACH");
break;
default:
OutputDebugString("DllEntryPoint");
}
return 1;
bobZ 2002-11-28
  • 打赏
  • 举报
回复
DLL入口点函数,reason值是系统传递的参数,当此DLL第一次被进程装载时reason值为DLL_PROCESS_ATTATCH,DLL最终被卸载时,reason值为DLL_PROCESS_DETATCH.
SuperSuperLéon 2002-11-22
  • 打赏
  • 举报
回复
你看这个:

BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to the DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
);

这个和下面的是一个。

BOOL WINAPI DllEntryPoint(

HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
);

呵呵。


因此,DllMain 和DllEntryPoint 是一回事,只不过DllEntryPoint是老版本的。

更好的E 文资料见 .net Msdn.

关于 DllMain 的具体说法你去看:

《新编 Windows Api 参考大全》P489。这里写的很详细。

不过有两句关于 DllMain 的 翻译得不够准确。

我要是把它打上来,也太那个了。



ok?

啊,哈欠,我困了,我睡了。bye.
SuperSuperLéon 2002-11-22
  • 打赏
  • 举报
回复
DllEntryPoint 是 标准C 下的

动态库入口函数。

你在使用中,参照DllMain就可以了。

除非你有特殊应用,否则,不用理会得太深。
kingfish 2002-11-22
  • 打赏
  • 举报
回复
推荐你看《windows核心编程》第20章
yulei0707 2002-11-22
  • 打赏
  • 举报
回复
还是不太清楚能不能详细一点
KingOf007 2002-11-22
  • 打赏
  • 举报
回复
而这个函数呢,是由操作系统调用,为了在加载或卸载时,作一些处理用的
KingOf007 2002-11-22
  • 打赏
  • 举报
回复
靠,看能看懂,翻译起来倒挺麻烦的,简单点说就是:
hinstDLL是动态链接库的基址,可以在调用一些API函数时用到,
reason就是操作系统对此DLL的操作:
DLL_PROCESS_ATTACH:是作为一个进程加载
DLL_PROCESS_DETACH:是进程卸载此DLL
DLL_THREAD_ATTACH:是作为一个线程加载
DLL_THREAD_DETACH:是作为线程加载后的卸载
最后一个参数:是在进程加载或卸载时,传入的一个参数
如果是动态加载,则为NULL,如果是静态加载,则为非空
如果是调用FreeLibrary来卸载,此值为NULL,如果是随进程一起结束,为非NULL
KingOf007 2002-11-22
  • 打赏
  • 举报
回复
这段英语还是挺简单的,为了100分,我给你翻译一下吧
3.1 程序说明 当启动程序Try.EXE调用SetMouseHook()后,Windows系统将MouseHook.DLL映射入每一个有鼠标消息传入的进程地址空间。映射时将用DLL_PROCESS_ATTACH作为参数fdwReason的值调用DllEntryPoint(),DllEntryPoint()调用ModifyCall()搜索该进程对TextOutA()的调用并将其替换为调用MyTextOut()。这样当该进程调用GDI32.DLL的TextOutA()时实际调用的却是MouseHook.DLL的MyTextOut()。 ModifyCall()利用进程的HINSTANCE(也即HMODULE,对于Win32而言它们是一回事,即装载基址)找到DOS文件头结构IMAGE_DOS_HEADER,再利用IMAGE_DOS_HEADER中的e_lfanew成员找到Win32的IMAGE_NT_HEADERS结构,该结构含有动态连接所需的信息。IMAGE_NT_HEADERS中的OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]含有DLL函数引入表的RVA(相对虚拟地址)和大小。搜索该表所指向的DLL引入函数地址,值与GetProcAddress()返回值相同的单元就是对应的DLL函数地址存放单元,将MyTextOut()地址写入即可。详细情形请参阅有关PE文件格式的说明和Winnt.h中的定义。 当启动程序调用UnMouseHook()时,过程与之类似,只是此时是为了卸下WH_MOUSE全局钩子并恢复原来对TextOutA()的调用。 此处设置WH_MOUSE全局钩子的目的只是利用全局钩子的特性将MouseHook.DLL“挤进”其它进程的地址空间,因此钩子过程MouseProc()很简单,只是传递一下消息而已。 两个#pragma data_seg()编译器指令是为了定义一个名为.MouseHook的数据段(更确切地说是数据节),该数据段在MouseHook.DEF中被说明为共享,之所以如此是因为各个进程空间中的MouseProc()需要该钩子的句柄hMouseHook,而hMouseHook只在启动程序Try.EXE调用SetMouseHook()时得到一次,因此只能放在共享内存中,否则编程将变得复杂起来。至于每个进程中被替换下来的TextOutA()地址,是属于单个进程空间的,故放在局部数据中,Windows系统会为每次映射使用不同的内存。实际上,TextOutA()的引入地址在所有的进程中都是相同的,这是因为为了页面管理的简单和进程切换的效率,对每个进程Windows 9x将系统DLL映射在同一地址上,但这不是Windows对外保证的,而只是权宜之计,以后可能改变,且Windows NT的情况也可能不同。 MyTextOut()将截获的TextOutA()的参数lpText(即输出字符串)改变以后才输出,从而可以看到截获是否成功。之所以改变两个字符而不是简单的一个,是因为只改变一个字符将导致汉字输出乱码。 MyTextOut()源码中唯一的一条汇编语句__asm sub esp,14h是所有源码中最难写的语句。如果没有这条指令,MyTextOut()将无法正确返回到进程调用TextOutA()处的下一条指令上,出现的“意外”情况是:进程调用TextOutA()的最后一个参数、即输出字符串长度参数将作为返回地址从堆栈中弹出,从而使EIP为一个很小的值,程序进入Windows系统用作指针检查的低端内存,导致非法内存访问。在调试过程中发现导致这种现象的原因是MyTextOut()在临返回前使用了add esp,14h来清除并不需要清除的堆栈,从而破坏了堆栈。显然,原因在于函数调用说明使编译器产生了“错误”的堆栈管理代码,我不知道如何改正这一点,只好使用__asm sub esp,14h强行使堆栈指针指向“正确”的返回地址。有知晓个中奥妙的同志请与作者联系,多多指教。 以下程序在Windows 98、Microsoft Visual Studio 97中调试通过,由于编程中并未使用Windows 9x的特性,且程序依靠的PE文件格式在Windows 9x和Wiundows NT中是通用的,因此上述方法在Windows NT可能也是可行的,只是我并未验证(我没有装Windows NT的机器)。另外,某些方面的情况由于编译器和操作系统不同可能会有所不同(如编译器生成的指令),我的叙述会因此而偏颇甚至错误,在此先做个提醒,也欢迎来意见改进编程。

1,222

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧