遅延バイディングによるDLL呼び出し(Visual Basic編)

naohiro19
記事: 256
登録日時: 14年前
住所: 愛知県

遅延バイディングによるDLL呼び出し(Visual Basic編)

投稿記事 by naohiro19 » 10年前

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
最後に編集したユーザー naohiro19 on 2015年6月25日(木) 14:56 [ 編集 1 回目 ]

コメントはまだありません。