簡體   English   中英

將帶有數組的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,所以我使用編組但Marshal.SizeOf(MyCD)返回錯誤值(12)如果我不使用InterOp Size,並且,welp,所有使用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())

這里的鏈接看起來有一些相當廣泛的討論(包括示例代碼): https//social.msdn.microsoft.com/Forums/en-US/3df9e61d-440f-4bea-9556-b2531b30e5e6/problem-with- DeviceIoControl的功能?論壇= vblanguage

您的結構只是缺少Tracks成員上的屬性,以告訴編譯器它是一個內聯100個成員的數組。

從鏈接:

<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

(鏈接還包括結構中的一些便利功能,我在這里省略了。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM