关于卸载DLL文件?
不知道为什么我用下面这个函数卸载DLL,每次要执行两遍才能卸载掉DLL,但执行时每一步都跟踪调试过,没有问题,有高手的话,给指点一下:
Function EnjectLib(ProcessId As Long, LibName As String) As Long
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessId)
If hProcess = 0 Then Exit Function
Dim hModuleSnap As Long
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessId)
Dim me32 As MODULEENTRY32
me32.dwSize = Len(me32)
Dim bFound As Boolean
bFound = False
Dim hMod As Long
hMod = 0
If Module32First(hModuleSnap, me32) <> 0 Then
Do
If InStr(LCase(me32.szExePath), LCase(LibName)) > 0 Then
hMod = me32.hModule
bFound = True
End If
Loop While (Not bFound) And (Module32Next(hModuleSnap, me32) <> 0)
End If
CloseHandle hModuleSnap
If hMod = 0 Then
CloseHandle hProcess
Exit Function
End If
Dim pfnRemote As Long
pfnRemote = GetProcAddress(GetModuleHandle("Kernel32"), "FreeLibrary")
If pfnRemote = 0 Then
CloseHandle hProcess
Exit Function
End If
Dim hThread As Long
hThread = CreateRemoteThread(hProcess, 0, 0, pfnRemote, hMod, 0, 0)
If hThread = 0 Then
CloseHandle hProcess
Exit Function
End If
WaitForSingleObject hThread, INFINITE
CloseHandle hProcess
CloseHandle hThread
EnjectLib = 1
End Function