首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在VB.NET中使用<DllImport>?

如何在VB.NET中使用<DllImport>?
EN

Stack Overflow用户
提问于 2010-02-09 22:34:41
回答 3查看 116.7K关注 0票数 27

我应该如何在VB.NET中DLLImport东西?下面是一个例子:

代码语言:javascript
运行
复制
<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

如果我将它放在一个类中或其他地方,我会得到"DLLimport is not defined“,我正在使用Visual Studio 2008专业版

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-02-09 22:36:20

您必须将Imports System.Runtime.InteropServices添加到源文件的顶部。

或者,您可以完全限定属性名称:

代码语言:javascript
运行
复制
<System.Runtime.InteropService.DllImport("user32.dll", _
    SetLastError:=True, CharSet:=CharSet.Auto)> _
票数 38
EN

Stack Overflow用户

发布于 2010-02-09 22:37:35

代码语言:javascript
运行
复制
Imports System.Runtime.InteropServices
票数 7
EN

Stack Overflow用户

发布于 2018-06-01 04:14:34

我知道这个问题已经回答过了,但这里有一个例子,给那些试图在vb项目中使用SQL Server类型的人:

代码语言:javascript
运行
复制
            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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2229793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档