如何在 VB.NET 中使用 <DllImport>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2229793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use <DllImport> in VB.NET?
提问by MilMike
How should I DLLImport things in VB.NET? An example would be:
我应该如何 DLLImport 在VB.NET 中的东西?一个例子是:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
End Function
If I put it inside a Class or somewhere else, I get "DLLimport is not defined" I am using Visual Studio 2008Professional
如果我把它放在一个类或其他地方,我会得到“未定义 DLLimport” 我使用的是Visual Studio 2008Professional
回答by Anton Gogolev
You have to add Imports System.Runtime.InteropServices
to the top of your source file.
您必须添加Imports System.Runtime.InteropServices
到源文件的顶部。
Alternatively, you can fully qualify attribute name:
或者,您可以完全限定属性名称:
<System.Runtime.InteropService.DllImport("user32.dll", _
SetLastError:=True, CharSet:=CharSet.Auto)> _
回答by Luhmann
Imports System.Runtime.InteropServices
回答by Nap
I saw in getwindowtext (user32)on pinvoke.net that you can place a MarshalAs
statement to state that the StringBuffer is equivalent to LPSTR.
我在pinvoke.net 上的 getwindowtext (user32)中看到,您可以放置一个MarshalAs
语句来声明 StringBuffer 等效于 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
回答by Todd Harvey
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:
我知道这已经得到了回答,但这里有一个例子,供试图在 vb 项目中使用 SQL Server 类型的人使用:
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
回答by Christopher Aicher
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 Functioninstead of DllImport... Its more simply, its shorter and does the same
我总是使用声明函数而不是DllImport......它更简单,它更短并且做同样的事情