いつもお世話になっております。
開発環境:
WinXP Pro
VB.NET 2003(.NET Framework 1.1)
タイトルのように
VB.NETでユーザー定義型のメモリ内でのサイズを取得する方法を探しています。
VB6 では、LenB を使い、サイズを取得できましたが、
VB.NET では、LenB をサポートしていないため、サイズ取得できません。
具体的な例を挙げると下記のようなソースです。
--------------------------------------------------
Private Structure SizeInfo
Dim intSizeA As Integer
Dim decSizeB As Decimal
Dim decSizeC As Decimal
End Structure
Private Sub GetSize()
Dim typSizeInfo As SizeInfo
dim lngSize As Long
' サイズを取得する(※VB6の記述)
lngSize = LenB(typSizeInfo)
' サイズを取得する(VB.NETの記述)
???
MesBox("サイズ[" & lngSize & "]")
End Sub
--------------------------------------------------
よろしくお願いします。
詳しく調べてないです。バイト数が合わない型がありましたらご報告下さい。
詳しく調べてみます。
[VB.NET]
Private Sub GetSize()
Dim typSizeInfo As SizeInfo
Dim lngSize As Long
' サイズを取得する(※VB6の記述)
'lngSize = LenB(typSizeInfo)
' サイズを取得する(VB.NETの記述)
lngSize = Runtime.InteropServices.Marshal.SizeOf(typSizeInfo)
MsgBox("サイズ[" & lngSize & "]")
End Sub
あれ?おかしいね。これ。Integer が奇数個あると変だ。
ごめんなさい。忘れてください。
> あれ?おかしいね。これ。Integer が奇数個あると変だ。
そうですか? 当方では、期待値を返してきていますよ。
'------------------------------------------------------
' Marshal.SizeOf(GetType(SizeInfo)) = 40
'------------------------------------------------------
Private Structure SizeInfo
Dim intSizeA As Integer '…4バイト
'…4バイト パディング
Dim decSizeB As Decimal '…16バイト
Dim decSizeC As Decimal '…16バイト
End Structure
'------------------------------------------------------
' Marshal.SizeOf(GetType(SizeInfo)) = 40
'------------------------------------------------------
Private Structure SizeInfo
Dim intSizeA As Integer '…4バイト
Dim intSizeB As Integer '…4バイト
Dim decSizeC As Decimal '…16バイト
Dim decSizeD As Decimal '…16バイト
End Structure
'------------------------------------------------------
' Marshal.SizeOf(GetType(SizeInfo)) = 48
'------------------------------------------------------
Private Structure SizeInfo
Dim intSizeA As Integer '…4バイト
'…4バイト パディング
Dim decSizeB As Decimal '…16バイト
Dim intSizeC As Integer '…4バイト
'…4バイト パディング
Dim decSizeD As Decimal '…16バイト
End Structure
'------------------------------------------------------
' Marshal.SizeOf(GetType(SizeInfo)) = 48
'------------------------------------------------------
Private Structure SizeInfo
Dim intSizeA As Integer '…4バイト
'…4バイト パディング
Dim decSizeB As Decimal '…16バイト
Dim decSizeC As Decimal '…16バイト
Dim intSizeD As Integer '…4バイト
'…4バイト パディング
End Structure
'------------------------------------------------------
' Marshal.SizeOf(GetType(SizeInfo)) = 40
'------------------------------------------------------
<StructLayout(LayoutKind.Sequential, Pack:=4)> _
Private Structure SizeInfo
Dim intSizeA As Integer '…4バイト
Dim decSizeB As Decimal '…16バイト
Dim decSizeC As Decimal '…16バイト
Dim intSizeD As Integer '…4バイト
End Structure
'------------------------------------------------------
' Marshal.SizeOf(GetType(SizeInfo)) = 36
'------------------------------------------------------
<StructLayout(LayoutKind.Sequential, Pack:=4)> _
Private Structure SizeInfo
Dim intSizeA As Integer '…4バイト
Dim decSizeB As Decimal '…16バイト
Dim decSizeC As Decimal '…16バイト
End Structure
4バイト パディングって何!?(>△<)ノシ☆!
…いや、言わなくていいです。だいたい分かりつつありますからw
自分で調べます。
魔界の仮面弁士さんの実行結果でいいんですよね?
…じゃあ、問題ないです(汗)
> …いや、言わなくていいです。だいたい分かりつつありますからw
> 自分で調べます。
調べるついでに、VB6のLen関数とLenB関数の動作の違いに付いても
調べてみると良いかも。(^^;)
Option Explicit
Private Type UDT
A As Byte
B As Integer
End Type
Private Sub Form_Load()
Dim X As UDT
Debug.Print LenB(X), Len(X)
End Sub
> 魔界の仮面弁士さんの実行結果でいいんですよね?
今回の質問で求められていたのは、LenではなくLenBの動作でしたしね。
特攻隊長まるるうさん、魔界の仮面弁士さん
ご回答ありがとうございます。ご連絡が遅れてすみません。
Marshal.SizeOf(typSizeInfo) とユーザー定義型宣言時に
<StructLayout(LayoutKind.Sequential, Pack:=4)> を追加することにより
期待するサイズを取得することができました。
ありがとうございました。