【问题标题】:Call unmanaged C DLL with char pointer with VB.NET使用 VB.NET 使用 char 指针调用非托管 C DLL
【发布时间】:2015-03-21 19:33:08
【问题描述】:

我已经对如何正确调用包含 char 指针的非托管 C DLL 函数进行了大量研究,以将其作为字符串传递给 VB.NET,但我仍然遇到以下代码的访问冲突异常

<DllImport("Sdk.dll")> _
Public Function Get_FileNameAt(ByVal pointer As IntPtr, ByVal index As UInt32, <MarshalAs(UnmanagedType.LPWStr)> ByVal name As StringBuilder, ByVal capacity As UInt32) As Integer
End Function

Dim StrName As New StringBuilder(256)
Dim pointer As IntPtr = IntPtr.Zero
Get_FileNameAt(pointer, 1, StrName, 256)

我尝试过使用和不使用 Pinvoke

C 函数声明

GoSensor_FileNameAt(void* pointer, unsigned __int32 index, char* name, unsigned __int32 capacity);

提前致谢。

【问题讨论】:

标签: c vb.net char marshalling


【解决方案1】:
Public Declare Function Get_FileNameAt Lib "Sdk.dll" (Byref pointer As IntPtr, ByVal index As UInt32, ByVal name As StringBuilder, ByVal capacity As UInt32) As Integer
End Function

Dim StrName As StringBuilder = New StringBuilder(256)
'Dim pointer As IntPtr = IntPtr.Zero  (IntPtr.Zero should pass directly because it's read-only)
Get_FileNameAt(IntPtr.Zero, 1, StrName, 256)

【讨论】:

    猜你喜欢
    • 2011-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多