AW: DLL-Erstellung mit Visual Studio 2008
14.07.2009 21:20:31
Nico
Mit VBA habe ich noch folgendes um den Verweis per Script zu erstellen. Aber auch hier muss VBA-Project vertraut werden.
Man erstelle ein Klassenmodul namens "clsDLLreg" unbd kopiert diesen Code rein:
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
Public Sub RegServeDLL(ByVal Path As String, mode As Boolean)
Dim insthLib As Long, lpLibAdr As Long, hThd As Long, lpExCode As Long
Dim procName As String, Result As Long, okFlag As Boolean
'DLL in den Speicher laden
insthLib = LoadLibrary(ThisWorkbook.Path & "\" & 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)
Dim test
test = 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 Sub
Public Sub REGmyVerweis(dllName As String, reg As Boolean, Optional dllFileName As String)
'Registriert/Unregistriert den Verweis (myDll) --> VB-Editor - Extras/Verweise...
'dll muss sich im selben Verzeichnis befinden wie die Excel-Datei
'registrieren --> Call REGmyVerweis("atest", True, "myFirst.dll")
'unregistrieren --> Call REGmyVerweis("atest", False)
On Error GoTo error
Dim project As String
Dim checkProject As String
Dim x As Object
Dim i As Integer
Dim j As Integer
Dim y As Object
Dim myDll As Boolean
Dim myVerweisItem As Integer
project = ThisWorkbook.Path & "\" & ThisWorkbook.name
Set x = Application.VBE.VBProjects
myDll = False
For i = 1 To x.Count
checkProject = x.Item(i).Filename
If checkProject = project Then
Set y = x.Item(i).References
For j = 1 To y.Count
If (UCase(y.Item(j).name) = UCase(dllName)) Then
myDll = True
myVerweisItem = j
j = y.Count
End If
Next j
If myDll = False Then
y.AddFromFile ThisWorkbook.Path & "\" & dllFileName
Else
If reg = False Then y.Remove y.Item(myVerweisItem)
End If
i = x.Count
End If
Next i
error:
Dim errNumber
errNumber = Err.number
Select Case (errNumber)
Case 0
'Resume Next
Case Else
'MsgBox (Err.Number & vbCr & Err.Description)
Debug.Print Err.number
'error 1004 bei set x = application --> Zugriff auf das VBA-Project wird nicht vertraut!!
'error -2147319779 bei if(y.item...) --> Zugriff auf References nicht möglich korrupter _
Verweis-Eintrag
End Select
End Sub
Nun ein neues Modul erstellen mit folgenden Code:
'###################################################################
'## Klassenmodul-Variablen ##
'###################################################################
Public objDLLreg As New clsDLLreg
Sub auto_open()
'Verweise und Registrierung erstellen
Call objDLLreg.REGmyVerweis("Titel_der_DLL_Verweisname", True, "Dateiname.dll")
Call objDLLreg.RegServeDLL("Dateiname.dll", True)
Call starts
End Sub
Sub auto_close()
'Verweise und Registrierung entfernen
Call objDLLreg.REGmyVerweis("Titel_der_DLL_Verweisname", False)
Call objDLLreg.RegServeDLL("Dateiname.dll", False)
End Sub
Sub starts()
Call codiere
End Sub
Aber Tino ich habe gesehn das Du auch Auftragsarbeiten machst. Weisst Du nicht vieleicht wie ich eine Dll mit VS2008 erstelle um diese dann genau so anwenden zu können wie die unter VB6.0 erstellte?