#If VBA7 Then Private Declare PtrSafe Function GetShortPathName Lib “Kernel32.dll” Alias “GetShortPathNameW” (ByVal LongPath As LongPtr, ByVal ShortPath As LongPtr, ByVal Size As Long) As Long Private Declare PtrSafe Function SetDllDirectory Lib “Kernel32.dll” Alias “SetDllDirectoryW” (ByVal Path As LongPtr) As Long Private Declare PtrSafe Sub LoadClr_x64 Lib “QlmCLRHost_x64.dll” (ByVal clrVersion As String, ByVal verbose As Boolean, ByRef CorRuntimeHost As IUnknown) Private Declare PtrSafe Sub LoadClr_x86 Lib “QlmCLRHost_x86.dll” (ByVal clrVersion As String, ByVal verbose As Boolean, ByRef CorRuntimeHost As IUnknown) #Else Private Declare Function GetShortPathName Lib “Kernel32.dll” Alias “GetShortPathNameW” (ByVal LongPath As Long, ByVal ShortPath As Long, ByVal Size As Long) As Long Private Declare Function SetDllDirectory Lib “Kernel32.dll” Alias “SetDllDirectoryW” (ByVal Path As Long) As Long Private Declare Sub LoadClr_x64 Lib “QlmCLRHost_x64.dll” (ByVal clrVersion As String, ByVal verbose As Boolean, ByRef CorRuntimeHost As IUnknown) Private Declare Sub LoadClr_x86 Lib “QlmCLRHost_x86.dll” (ByVal clrVersion As String, ByVal verbose As Boolean, ByRef CorRuntimeHost As IUnknown) #End If ‘ WinAPI Declarations ‘ Class variables Dim m_myobject As Object Dim m_homeDir As String
Private Function GetMyObject(dllPath As String, dllClass As String) As Object Dim LongPath As String Dim ShortPath As String LongPath = “\\?\” & m_homeDir ShortPath = String$(260, vbNull) PathLength = GetShortPathName(StrPtr(LongPath), StrPtr(ShortPath), 260) ShortPath = Mid$(ShortPath, 5, CLng(PathLength – 4)) Call SetDllDirectory(StrPtr(ShortPath)) Dim clr As mscoree.CorRuntimeHost If Is64BitApp() Then Call LoadClr_x64(“v4.0”, False, clr) Else Call LoadClr_x86(“v4.0”, False, clr) End If Call clr.Start Dim domain As mscorlib.AppDomain Call clr.GetDefaultDomain(domain) Dim myInstanceOfDotNetClass As Object Dim handle As mscorlib.ObjectHandle Set handle = domain.CreateInstanceFrom(dllPath, dllClass) Dim clrObject As Object Set GetMyObject = handle.Unwrap Call clr.Stop End Function Private Function Is64BitApp() As Boolean #If Win64 Then Is64BitApp = True #End If End Function
m_homeDir = ThisWorkbook.Path m_myobject = GetMyObject(m_homeDir & "\yourdotnet.dll", "namespace.class")The first argument is the full path to the .NET DLL.
test