Posted By

sekihin on 06/15/09


Tagged

VBNet


Versions (?)

VB.NETで、構造体をファイルに書き込む


 / Published in: VB.NET
 

URL: http://winofsql.jp/VA003334/dnettool080617015020.htm

  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.  
  5. ' ********************************************************
  6. ' * 構造体定義
  7. ' ********************************************************
  8. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
  9. Public Class INDIVIDUALINFO
  10. Public dwOriginalSize As Integer = 1
  11. Public dwCompressedSize As Integer = 1
  12. Public dwCRC As Integer = 1
  13. Public uFlag As Integer = 1
  14. Public uOSType As Integer = 1
  15. Public wRatio As Short = 1
  16. Public wDate As Short = 1
  17. Public wTime As Short = 1
  18. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=513)> _
  19. Public szFileName As String = "A"
  20. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> _
  21. Public dummy1 As String = "B"
  22. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
  23. Public szAttribute As String = "C"
  24. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
  25. Public szMode As String = "D"
  26. End Class
  27.  
  28. <DllImport("Kernel32.dll", CharSet:=CharSet.Ansi, EntryPoint:="RtlMoveMemory")> _
  29. Private Shared Sub CopyMyStruct( _
  30. ByVal forFile As Byte(), _
  31. <[In](), Out()> ByVal Source As INDIVIDUALINFO, _
  32. ByVal Length As Integer)
  33. End Sub
  34.  
  35. Private Sub Button1_Click(ByVal sender As System.Object, _
  36. ByVal e As System.EventArgs) Handles Button1.Click
  37.  
  38. Dim data As New INDIVIDUALINFO()
  39. Dim forFile As Byte() = New Byte(Marshal.SizeOf(data) - 1) {}
  40.  
  41. CopyMyStruct(forFile, data, Marshal.SizeOf(data))
  42. System.IO.File.WriteAllBytes("struct_test.dat", forFile)
  43.  
  44. End Sub
  45. End Class

Report this snippet  

You need to login to post a comment.