ImportsSystemImportsSystem.Runtime.InteropServices Public ClassDllinvoke Public Sub New(ByValDllPath as String) Me. Hlib =dllinvoke.loadlibrary (DllPath)End Sub Protected Overrides SubFinalize ()TryDllinvoke.freelibrary (Me. Hlib)Finally MyBase. Finalize ()End Try End Sub<dllimport ("Kernel32.dll") > _ Private Shared FunctionFreeLibrary (ByVal[Lib] asINTPTR) as Boolean End Function<dllimport ("Kernel32.dll") > _ Private Shared FunctionGetProcAddress (ByVal[Lib] asINTPTR,ByValFuncName as String) asIntPtrEnd Function Public FunctionInvoke (ByValApiname as String,ByValT asType) as[Delegate] ReturnMarshal.getdelegateforfunctionpointer (Dllinvoke.getprocaddress (Me. Hlib, Apiname), T)End Function<dllimport ("Kernel32.dll") > _ Private Shared FunctionLoadLibrary (ByValPath as String) asIntPtrEnd Function PrivateHlib asIntPtrEnd Class
ImportsSystemImportsSystem.Runtime.CompilerServicesImportsSystem.Runtime.InteropServicesImportsSystem.Text Public ClassTestPrivate Declare FunctionMessageBoxALib "User32" Alias "MessageBoxA"(ByValhwnd as Integer,ByValLptext asStringBuilder,ByValLpcaption asStringBuilder,ByValWtype as Integer) as Long Private Delegate FunctionMessageboxadelegate (ByValhwnd as Integer,ByValLptext asStringBuilder,ByValLpcaption asStringBuilder,ByValWtype as Integer) as Long Public Shared SubMain ()DimInvoke as NewDllinvoke ("user32.dll") DimMode asMessageboxadelegate =DirectCast(Invoke. Invoke ("MessageBoxA",GetType(Messageboxadelegate)), messageboxadelegate) mode. Invoke (0,NewStringBuilder ("haha"),NewStringBuilder ("title"),0) Console.WriteLine ("OK") Console.ReadLine ()End SubEnd Class