O well, here is my class, VB though:
Example:
pl = New DLL_Preload("dllname")
do something...
pl.dispose
Class:
Imports Microsoft.VisualBasic
Public Class DLL_Preload
Implements IDisposable
' Preloads an ordinary *win32* dll from the bin folder or found through
the Windows %path%.
' Once ASP.NET has called a function in this library, preloading is no
longer required.
' This is handled for you.
' It's for the best you unload this class by setting it to nothing when
done.
Declare Ansi Function GetModuleHandle Lib "KERNEL32.DLL" Alias
"GetModuleHandleA" (ByVal lpModuleName As String) As Int32
Declare Ansi Function LoadLibrary Lib "KERNEL32.DLL" Alias
"LoadLibraryA" (ByVal lpLibFileName As String) As Int32
Declare Ansi Function FreeLibrary Lib "KERNEL32.DLL" Alias "FreeLibrary"
(ByVal hLibModule As Int32) As Int32
Private __hLib As Int32
Private hLib As Int32
' sModuleName, library's module name, usually equal to dllfilename
WITHOUT extension and WITHOUT folder.
' sDLLShortFileName, the modulename can differ, if the module is not
loaded yet, this will optionally be used as dll filename.
' Provide filename without folder but add it's extension like: .dll
Public Sub New(ByVal sModuleName As String, Optional ByVal
sDLLShortFileName As String = "")
On Error Resume Next
If Len(sModuleName) = 0 Then Exit Sub
sDLLShortFileName = Trim(sDLLShortFileName)
If Len(sDLLShortFileName) = 0 Then sDLLShortFileName = sModuleName &
".dll"
'-------------------------------------------------------------------------------------
' Preloads library if not loaded yet, we try loading it from the
website folder.
'-------------------------------------------------------------------------------------
' Test for already loaded module.
hLib = GetModuleHandle(sModuleName)
' Module does not exist, force loading.
If hLib = 0 Then
' Load from this website folder.
hLib =
LoadLibrary(System.Web.HttpContext.Current.Server.MapPath("~\bin\" &
sDLLShortFileName))
Select Case hLib
' Failed, load from anywhere on the computer.
Case 0 To 32
hLib = LoadLibrary(sDLLShortFileName)
Select Case hLib
Case 0 To 32
hLib = 0
Case Else
End Select
End Select
__hLib = hLib
End If
End Sub
'--------------------------------------------------------------------------------
' IDisposable
'--------------------------------------------------------------------------------
Private disposed As Boolean = False
Private Overloads Sub Dispose(ByVal disposing As Boolean)
On Error Resume Next
If Not Me.disposed Then
If __hLib <> 0 Then FreeLibrary(__hLib)
__hLib = 0
End If
Me.disposed = True
End Sub
#Region " IDisposable Support "
' This code added by Visual Basic to correctly implement the disposable
pattern.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
'--------------------------------------------------------------------------------
Public Function GethInstance() As Int32
Return hLib
End Function
Public Function IsLoaded() As Boolean
Return CBool(GethInstance())
End Function
End Class
With the dll in the bin folder:
[quoted text clipped - 29 lines]