programing

VB.NET에서 사용하는 방법?

lastcode 2023. 5. 22. 21:06
반응형

VB.NET에서 사용하는 방법?

VB.NET에서 DLI로 가져오는 방법은 무엇입니까?예를 들어 다음과 같습니다.

<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

클래스 또는 다른 곳에 배치하면 "DLL 가져오기가 정의되지 않았습니다"라는 메시지가 표시됩니다. Visual Studio 2008 Professional을 사용하고 있습니다.

추가해야 합니다.Imports System.Runtime.InteropServices소스 파일의 맨 위에 있습니다.

또는 속성 이름을 완전히 한정할 수 있습니다.

<System.Runtime.InteropService.DllImport("user32.dll", _
    SetLastError:=True, CharSet:=CharSet.Auto)> _
Imports System.Runtime.InteropServices

이미 응답한 내용이지만 vb 프로젝트에서 SQL Server Types를 사용하려는 사용자를 위한 예는 다음과 같습니다.

            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

pinvoke.net 에서 getwindowtext(user32)에서 당신이 웹사이트에 게시할 수 있다는 것을 보았습니다.MarshalAsStringBuffer가 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

이것도 시도해 볼 수 있습니다.

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

항상 DllImport 대신 선언 기능을 사용합니다...그것은 더 단순하고, 더 짧고, 같은 일을 합니다.

언급URL : https://stackoverflow.com/questions/2229793/how-to-use-dllimport-in-vb-net

반응형