将带有数组的 VB6 类型转换为 VB.NET 结构
Convert VB6 Type with arrays to VB.NET Structure
我尝试将那些 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,所以我使用 Marshalling 但是 Marshal.SizeOf(MyCD)
return 错误值 (12) 如果我不使用 InterOp Size,并且,好吧,所有使用 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())
link 此处似乎有一些相当广泛的讨论(包括示例代码):
https://social.msdn.microsoft.com/Forums/en-US/3df9e61d-440f-4bea-9556-b2531b30e5e6/problem-with-deviceiocontrol-function?forum=vblanguage
您的结构只是缺少 Tracks
成员的属性以告诉编译器它是一个内联的 100 成员数组。
来自link:
<StructLayout(LayoutKind.Sequential)> _
Structure CDROM_TOC
Public Length As UShort
Public FirstTrack As Byte
Public LastTrack As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=100)> _
Public TrackData() As TRACK_DATA
End Structure
(link 结构中还包含一些我在这里省略的便利函数。)
我尝试将那些 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,所以我使用 Marshalling 但是 Marshal.SizeOf(MyCD)
return 错误值 (12) 如果我不使用 InterOp Size,并且,好吧,所有使用 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())
link 此处似乎有一些相当广泛的讨论(包括示例代码): https://social.msdn.microsoft.com/Forums/en-US/3df9e61d-440f-4bea-9556-b2531b30e5e6/problem-with-deviceiocontrol-function?forum=vblanguage
您的结构只是缺少 Tracks
成员的属性以告诉编译器它是一个内联的 100 成员数组。
来自link:
<StructLayout(LayoutKind.Sequential)> _
Structure CDROM_TOC
Public Length As UShort
Public FirstTrack As Byte
Public LastTrack As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=100)> _
Public TrackData() As TRACK_DATA
End Structure
(link 结构中还包含一些我在这里省略的便利函数。)