|
-
Apr 15th, 2001, 10:00 PM
#1
Thread Starter
New Member
I'm writing a small application based around a free 3rd party DLL. The DLL is, unfortunately, not very well designed and so has a serious issue with VB, as follows:
The DLL expects to have its Initialize() function called immediately after loading. It must be called once (and only once) before unloading the DLL. This is normally not a problem: I simply make sure in my Sub Main that the Initialize function is the first function I call from that DLL, forcing it to be loaded and initialized in one step.
The problem is that when my application has finished running and exits, somehow the DLL is never unloaded from memory. I know this because when I run the application again, the Initialize call fails. This will continually fail until I reboot, and then the program will work again, but only once before requiring another reboot, etc.
Please note that this is while in the IDE, I have not been able to test this in a compiled EXE do to another (unrelated) issue.
So, my question at long last --
Is there any way to force a DLL to be unloaded from memory?
Anything, ranging from Win32 API calls to undocumented VB functions to voodoo chants and prayers will do. The only thing that is not an option is recoding the DLL, which would otherwise be the obvious solution.
-
Apr 15th, 2001, 10:06 PM
#2
I don't know about unloading it, but why not put On Error Resume Next before you load the DLL, then it shouldn't crap out when you run it the second time...
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 15th, 2001, 10:14 PM
#3
Thread Starter
New Member
Unfortunately, the DLL is so poorly written that it goes into an infinate loop (after corrupting half of my running programs) if Initialize is called multiple times. This is a real pain in the watoozi because there is no way for an On Error handler to trap it.
I really need some way to force VB to unload the DLL with a single command. Actually, come to think of it, I could force a call to FreeLibrary, but that might screw VB up completely. I'll have to experiment with that.
-
Apr 15th, 2001, 10:15 PM
#4
sorry I couldn't be of more help
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 15th, 2001, 10:18 PM
#5
Frenzied Member
Put this in a module
Code:
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * 6400
End Type
Public Function StopApp(myName As String) As Boolean
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
StopApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Function
usage
stopapp "c:\windows\mydll.dll"
-
Apr 15th, 2001, 10:19 PM
#6
yeah, I guess that's one way 
cool
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 15th, 2001, 10:32 PM
#7
Frenzied Member
hehe. A lil mean but still
-
Apr 15th, 2001, 10:35 PM
#8
Originally posted by Evan
hehe. A lil mean but still
It was only sarcasm, I forget sometimes that you can't here the tone of my voice, when reading my posts
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 15th, 2001, 10:38 PM
#9
Frenzied Member
=) Ok. Thats one downside to computers!
-
Apr 15th, 2001, 10:41 PM
#10
maybe that's a good idea for the forums...mic support Can you imagine...
crptcblade : "Hello"
*VBWorld server bursts into flames..*
maybe sometime in the distant future...
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Apr 15th, 2001, 10:45 PM
#11
Thread Starter
New Member
Thanks a million, Evan
One quick question - will this also work on Windows NT? I know permissions get a little hairy...
FYI, this DLL actually loads another EXE in a suspended state without running it, then calls functions within that EXE and passes back the return values ... a very sneaky way of getting at some hacker-protected functions. The problem is that the EXE is not being unloaded properly if the DLL is initialized multiple times, which causes all sorts of problems at the next Initialize call which then leads to an Unhandled Exception loop in Kernel32 on Win98.
What a mess, eh? But it looks like your StopApp funtion is great for killing that EXE before the situation gets out of hand. Thanks again 
-- EDIT -- I should mention that I can't StopApp the DLL directly because (appearantly) DLLs don't show up as processes. But I can still use this on the suspended EXE and the next call to Initialize works properly.
Last edited by Edgewize; Apr 15th, 2001 at 10:55 PM.
-
Apr 16th, 2001, 12:09 AM
#12
Frenzied Member
hehe,
There is no way to hide a process =)
at least from what I know of.
All of the calls in dll the should work in
NT just fine =).
I would test it yourself though, or ask
matthew gates, hes a smartie =)
Im glad I could help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|