How to use in VB.NET?

后端 未结 5 1106
星月不相逢
星月不相逢 2020-12-16 10:35

How should I DLLImport things in VB.NET? An example would be:

 _
Private Shared F         


        
相关标签:
5条回答
  • 2020-12-16 11:02

    You can also try this

    Private Declare Function GetWindowText Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
    

    I always use Declare Function instead of DllImport... Its more simply, its shorter and does the same

    0 讨论(0)
  • 2020-12-16 11:03

    You have to add Imports System.Runtime.InteropServices to the top of your source file.

    Alternatively, you can fully qualify attribute name:

    <System.Runtime.InteropService.DllImport("user32.dll", _
        SetLastError:=True, CharSet:=CharSet.Auto)> _
    
    0 讨论(0)
  • 2020-12-16 11:04

    I saw in getwindowtext (user32) on pinvoke.net that you can place a MarshalAs statement to state that the StringBuffer is equivalent to LPSTR.

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Ansi)> _
    Public Function GetWindowText(hwnd As IntPtr, <MarshalAs(UnManagedType.LPStr)>lpString As System.Text.StringBuilder, cch As Integer) As Integer
    End Function
    
    0 讨论(0)
  • 2020-12-16 11:10
    Imports System.Runtime.InteropServices
    
    0 讨论(0)
  • 2020-12-16 11:12

    I know this has already been answered, but here is an example for the people who are trying to use SQL Server Types in a vb project:

                Imports System
                Imports System.IO
                Imports System.Runtime.InteropServices
    
                Namespace SqlServerTypes
                    Public Class Utilities
    
    
    
                        <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
                        Public Shared Function LoadLibrary(ByVal libname As String) As IntPtr
    
                        End Function
    
                        Public Shared Sub LoadNativeAssemblies(ByVal rootApplicationPath As String)
                            Dim nativeBinaryPath = If(IntPtr.Size > 4, Path.Combine(rootApplicationPath, "SqlServerTypes\x64\"), Path.Combine(rootApplicationPath, "SqlServerTypes\x86\"))
                            LoadNativeAssembly(nativeBinaryPath, "msvcr120.dll")
                            LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial140.dll")
                        End Sub
    
                        Private Shared Sub LoadNativeAssembly(ByVal nativeBinaryPath As String, ByVal assemblyName As String)
                            Dim path = System.IO.Path.Combine(nativeBinaryPath, assemblyName)
                            Dim ptr = LoadLibrary(path)
    
                            If ptr = IntPtr.Zero Then
                                Throw New Exception(String.Format("Error loading {0} (ErrorCode: {1})", assemblyName, Marshal.GetLastWin32Error()))
                            End If
                        End Sub
                    End Class
                End Namespace
    
    0 讨论(0)
提交回复
热议问题