多维时空 发表于 2013-4-11 08:55:07

易语言DLL卸载自身

由于软件需要,DLL需要卸载自身

于是看了下msdn 发现有这么个api

FreeLibraryAndExitThread


The FreeLibraryAndExitThread function decrements the reference count of a loaded dynamic-link library (DLL) by one, then calls ExitThread to terminate the calling thread. The function does not return.


VOIDFreeLibraryAndExitThread(HMODULEhModule, DWORDdwExitCode);

ParametershModule Handle to the DLL module whose reference count the function decrements. The LoadLibrary or GetModuleHandleEx function returns this handle.

Do not call this function with a handle returned by the GetModuleHandle function, since this function does not maintain a reference count for the module.

dwExitCode Exit code for the calling thread. Return Values

This function does not return a value. Invalid module handles are ignored.

Remarks

The FreeLibraryAndExitThread function allows threads that are executing within a DLL to safely free the DLL in which they are executing and terminate themselves. If they were to call FreeLibrary and ExitThread separately, a race condition would exist. The library could be unloaded before ExitThread is called.

Requirements Requires Windows XP, Windows 2000 Professional, Windows NT Workstation 3.5 and later, Windows Me, Windows 98, or Windows 95.Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 3.5 and later.

Declared in Winbase.h; include Windows.h.

Link to Kernel32.lib.

Requires Kernel32.dll.


这个API将释放库并退出线程,于是乎……

将这个函数调用放到线程中去,创建这个线程就可以了。

FreeLibraryAndExitThread 的首参数是模块实例句柄 而易的入口函数 _启动子程序 并没有这个参数传入

但这并不妨碍我们下一步工作 要获取模块实例句柄有很多种方法.

例如 叮咚茶发过的 取模块实例句柄

.版本 2

.子程序 取自身模块句柄, 整数型, , 主要用于取DLL本身取模块句柄,弥补易语言DLL自身无法取模块句柄不足

置入代码 ({ 86, 232, 0, 0, 0, 0, 88, 100, 139, 53, 48, 0, 0, 0, 139, 118, 12, 139, 118, 28, 86, 139, 94, 8, 59, 195, 114, 13, 139, 78, 16, 3, 203, 59, 193, 119, 4, 139, 195, 235, 21, 139, 54, 139, 30, 59, 28, 36, 117, 227, 100, 161, 24, 0, 0, 0, 139, 64, 48, 139, 64, 8, 94, 94, 201, 195 })
返回 (0)


又或者你的dll支持黑月处理的话,可以通过黑月dll入口函数获取,又或是通过GetModuleHandle得到实例句柄,方法多种多样.


测试结果:一切正常~~
页: [1]
查看完整版本: 易语言DLL卸载自身