使用VB.NET使用char指针调用非托管C DLL

时间:2015-03-21 19:33:09

标签: c vb.net char marshalling

我已经做了很多关于如何正确调用非托管C DLL函数的研究,该函数包含一个char指针,以字符串形式传递给VB.NET,但我仍然遇到以下代码的访问冲突异常< / p>

<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);

提前致谢。

1 个答案:

答案 0 :(得分:0)

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)