How do I intercept DLL load/unload events in a VB6 ActiveX DLL?
I've inherited a legacy classic ASP ap开发者_JAVA技巧plication that uses VB6 ActiveX DLLs to perform the business logic.
I'd like to track the points at which the DLLs are loaded and unloaded. Is there any way of intercepting these events in a VB6 DLL?
On a related note, should Class_Terminate events always run when a their containing DLL unloads?
Use Sub Main as your start up object.
Make a module like this
Option Explicit
Private TerminateDetect As Terminate
Public Sub Main()
Set TerminateDetect = New Terminate
MsgBox "Setup"
End Sub
Then your terminate class looks like this
Option Explicit
Private Sub Class_Terminate()
MsgBox "I terminated"
End Sub
A test class I made is this
Option Explicit
Public Description As String
Public Sub Test()
MsgBox "test"
End Sub
I made a form with no references like this
Option Explicit
Private O As Object
Private Sub Command1_Click()
Set O = CreateObject("TestUnload.Dummy")
O.Test
End Sub
Private Sub Command2_Click()
Set O = Nothing
End Sub
When I click on Command1 I get two message one for loading up the DLL and another for running Test. Then when I click on Command2.
This example is rather crude so I hope you get the point.
Summary Make a TDLLManagement Class in every ActiveX you have. Put your Initialize Code in Class_Initialize and your Terminate code in Class_Terminate. Then have a Sub Main create a instance of that class and assign to a private module variable. Note if you have any GlobalMultisuse classes and directly reference the ActiveX DLL you want to do simple tests to see where the DLL loads.
Bad things happen in ASP if a VB dll gets unloaded it just doesn't handle this concept very well. The dll ought to be compiled with the "Retain in Memory" flag on (as well as "Unattended execution").
You can use the Sub Main to detect loading.
The classic VB6 add-in vbAdvance lets you add code to the DLLMain, and therefore get startup and teardown notification for your DLLs. EDIT: unfortunately if you use this your DLL must be a standard DLL, it can't be a COM (ActiveX) DLL. A word of warning - don't do anything too scary in DLLMain.
As far as I know Class_Terminate should always run before a containing DLL unloads, except in the case of abnormal termination of the application (e.g. using End).
精彩评论