А вот еще интересный пример:
********************************************************
'Dieser Source stammt von
http://www.activevb.de
'und kann frei verwendet werden. Für eventuelle Schäden
'wird nicht gehaftet.
'Um Fehler oder Fragen zu klären, nutzen Sie bitte unser Forum.
'Ansonsten viel Spaß und Erfolg mit diesem Source!
'------------- Anfang Projektdatei Project1.vbp -------------
'--------- Anfang Formular "Form1" alias Form1.frm ---------
'Control CommandButton: Command1
'Control CommandButton: Command2
'Control CommandButton: Command1
Option Explicit
Private Declare Function LoadLibrary Lib "kernel32" Alias _
"LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" _
(ByVal hModule As Long, ByVal lpProcName As String) As _
Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) _
As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Private Declare Function CreateThread Lib "kernel32" _
(lpThreadAttributes As Any, ByVal dwStackSize As Long, _
ByVal lpStartAddress As Long, ByVal lParameter As Long, _
ByVal dwCreationFlags As Long, lpThreadID As Long) As _
Long
Private Declare Function GetExitCodeThread Lib "kernel32" _
(ByVal hThread As Long, lpExitCode As Long) As Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode _
As Long)
Private Declare Function FreeLibrary Lib "kernel32" _
(ByVal hLibModule As Long) As Long
Const STATUS_WAIT_0 = &H0
Private Sub Command1_Click(Index As Integer)
'Un/Registrieren
Dim Mode As Boolean
Mode = IIf(Index = 0, True, False)
Call RegServe(App.Path & "\Multi.dll", Mode)
End Sub
Private Sub Command2_Click()
'Testen
Shell App.Path & "\dll-test.exe", vbNormalFocus
'Der Sourcecode der DLL-Test.exe
'---> Start Source Dll-Test
'On Error GoTo Abbruch
'Dim Calc As New Multiplikation
'Dim Ergebnis&
'Ergebnis = Calc.Multip(5,
'Call MsgBox("Die DLL ist registriert!" & vbCrLf & "Sie " & _
' "liefert bei Aufruf folgende Ausgabe:" & vbCrLf & _
' vbCrLf & "5 * 8 = " & Ergebnis)
'Exit Sub
'Abbruch:
'MsgBox ("Die DLL ist nicht registiert und kann" & vbCrLf & _
' "daher nicht ausgeführt werden!")
'<--- Ende Source Dll-Test
End Sub
Private Function RegServe(ByVal Path$, Mode As Boolean)
Dim insthLib&, lpLibAdr&, hThd&, lpExCode&
Dim procName$, Result&, okFlag As Boolean
'DLL in den Speicher laden
insthLib = LoadLibrary(Path)
'Aktion wählen
If insthLib Then
If Mode Then
procName = "DllRegisterServer"
Else
procName = "DllUnregisterServer"
End If
'Adresse der DLL im Speicher
lpLibAdr = GetProcAddress(insthLib, procName)
If lpLibAdr <> 0 Then
'Aktion starten
hThd = CreateThread(ByVal 0, 0, ByVal lpLibAdr, _
ByVal 0&, 0&, 0&)
If hThd Then
'Maximal 5 sec warten
Result = WaitForSingleObject(hThd, 5000)
If Result = STATUS_WAIT_0 Then
'Vorgang erfolgreich in 5 sec beendet
Call CloseHandle(hThd)
okFlag = True
Else
'5 sec überschritten -> Thread schließen
Call GetExitCodeThread(hThd, lpExCode)
Call ExitThread(lpExCode)
Call CloseHandle(hThd)
End If
End If
End If
'Speicher wieder freigeben
Call FreeLibrary(insthLib)
End If
If Not okFlag Then
MsgBox ("Fehler! Der Vorgang wurde abgebrochen.")
Else
MsgBox ("Der Vorgang war erfolgreich!")
End If
End Function
'---------- Ende Formular "Form1" alias Form1.frm ----------
'-------------- Ende Projektdatei Project1.vbp --------------
К сожалению коментарии на немецком.