C#でコンパイルしたDLLを
Telerik JustDecompleで逆コンパイルしたものです。
CODE:
Imports System
Imports System.Runtime.InteropServices
Public Class UnManagedDll
Implements IDisposable
Private moduleHandle As IntPtr
Public ReadOnly Property ModuleHandle As IntPtr
Get
Return Me.moduleHandle
End Get
End Property
Public Sub New(ByVal lpFileName As String)
MyBase.New()
Me.moduleHandle = UnManagedDll.LoadLibrary(lpFileName)
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
UnManagedDll.FreeLibrary(Me.moduleHandle)
End Sub
Private Shared Function FreeLibrary(ByVal hModule As IntPtr) As Boolean
End Function
Private Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal lpProcName As String) As IntPtr
End Function
Public Function GetProcDelegate(Of T As Class)(ByVal method As String) As T
Dim methodHandle As IntPtr = UnManagedDll.GetProcAddress(Me.moduleHandle, method)
Return DirectCast((TryCast(Marshal.GetDelegateForFunctionPointer(methodHandle, GetType(T)), T)), T)
End Function
Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
End Class
使い方
CODE:
Imports System
Class Program
Delegate Function MessageBoxADelegate(hWnd As IntPtr, lpText As String, lpCaption As String, uType As UInteger) As Integer
Shared Sub Main(args As String())
Using user32Dll As New UnManagedDll("user32.dll")
Dim MessageBoxA As MessageBoxADelegate = user32Dll.GetProcDelegate(Of MessageBoxADelegate)("MessageBoxA")
MessageBoxA(IntPtr.Zero, "テキスト", "キャプション", 0)
End Using
End Sub
End Class