vb.net Dynamic Call API

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.