掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
構造体->Byte配列->構造体とコピーする方法は? (ID:89174)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
魔界の仮面弁士さん、ご回答ありがとうございます。 ># このあたりで詰まっているようであれば、 ># IntPtr操作には手を出さない方が無難かも……。 なかなか痛いところです(汗) >先に提示したコードの場合...型変換が必要だったわけですね。 >ところで、「Option Strict On」を使っている場合には、...という事は理解されていますでしょうか? 理解はしていたつもりでしたが、object型をなかなか使う機会が なかなかなかったせいか、なかなか頭の中で整理がつかず混乱して しまい、的外れとなってしまいました。まだまだです… そして、質問の続きになるのですが… 諦め悪く、IntPtr操作を使って以下の様に組んでみました。 これでいいのでは?と思っておりますが、どうでしょう(汗) *********** 構造体->バイナリ->構造体 *********** Dim B(19) As Byte Dim P As System.IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(TEST1_T))) Dim test As TEST1_T Dim test2 As TEST1_T test.test1_1 = 10 test.test1_2 = 20 test.test1_3 = 30 test.test1_4 = 40 test.test1_5 = 50 Marshal.StructureToPtr(test, P, True) Marshal.Copy(P, B, 0, 20) test2 = DirectCast(Marshal.PtrToStructure(P, GetType(TEST1_T)), TEST1_T) Marshal.FreeHGlobal(P) Trace.WriteLine(String.Format("test :{0}(0x{0:x}),{1}(0x{1:x}),{2}(0x{2:x}),{3}(0x{3:x}),{4}(0x{4:x})", _ test.test1_1, test.test1_2, test.test1_3, test.test1_4, test.test1_5)) Trace.WriteLine(String.Format("test2:{0}(0x{0:x}),{1}(0x{1:x}),{2}(0x{2:x}),{3}(0x{3:x}),{4}(0x{4:x})", _ test2.test1_1, test2.test1_2, test2.test1_3, test2.test1_4, test2.test1_5)) *** <System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=1)> Structure TEST1_T Dim test1_1 As Integer Dim test1_2 As Short Dim test1_3 As Long Dim test1_4 As Short Dim test1_5 As Integer End Structure
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.