【发布时间】:2018-02-01 03:30:54
【问题描述】:
我尝试将那些 VB6 类型转换为 VB.NET 世界。
Type TRACK_DATA
Dim reserved As Byte
Dim Control As Byte
Dim Tracknumber As Byte
Dim reserved1 As Byte
Dim address As Long
End Type
Type CDTOC
Dim Length As Long
Dim FirstTrack As Byte
Dim LastTrack As Byte
Dim Tracks(100) As TRACK_DATA
End Type
当前的尝试失败了
<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Size:=8)>
Structure TRACK_DATA
Public reserved As Byte
Public Control As Byte
Public Tracknumber As Byte
Public reserved1 As Byte
Public address As UInteger
End Structure
<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Size:=806)>
Structure CDROM_TOC '4 + 1 + 1 + 800 = 806
Public Length As UInteger
Public FirstTrack As Byte
Public LastTrack As Byte
Public Tracks() As TRACK_DATA
End Structure
...
Dim MyCD As CDTOC
ReDim MyCD.Tracks(100)
任何提示如何做到这一点?
它是传递参数并将它们返回到外部 dll,所以我使用编组,但如果我不使用 InterOp Size,Marshal.SizeOf(MyCD) 返回错误值 (12),而且,所有使用 StructureToPtr 的尝试都以错误结束.
下面的代码,如果有任何帮助理解:
Toc_len = Marshal.SizeOf(MyCD)
Dim Toc_ptr As IntPtr = Marshal.AllocHGlobal(CInt(Toc_len))
'open the drive
...
'access to the TOC
DeviceIoControl(hFile, IOCTL_CDROM_READ_TOC, IntPtr.Zero, 0, Toc_ptr, Toc_len, BytesRead, IntPtr.Zero)
'copy back the datas from unmanaged memory
'fails here !
MyCD = Marshal.PtrToStructure(Toc_ptr, CDTOC.GetType())
【问题讨论】:
-
为了记录,VB6 的
Long通常映射到Integer,而不是UInteger(除非DLL 需要UInts,即)。
标签: arrays vb.net vb6 structure type-conversion