VB.NETでユーザー定義型のメモリ内でのサイズを取得する方法

解決


nasiga  2004-07-23 18:02:54  No: 114995  IP: [192.*.*.*]

いつもお世話になっております。

開発環境:
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
--------------------------------------------------

よろしくお願いします。

編集 削除
特攻隊長まるるう  2004-07-23 18:36:49  No: 114996  IP: [192.*.*.*]

詳しく調べてないです。バイト数が合わない型がありましたらご報告下さい。
詳しく調べてみます。
[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

編集 削除
特攻隊長まるるう  2004-07-23 18:55:09  No: 114997  IP: [192.*.*.*]

あれ?おかしいね。これ。Integer が奇数個あると変だ。
ごめんなさい。忘れてください。

編集 削除
魔界の仮面弁士  2004-07-23 19:29:52  No: 114998  IP: [192.*.*.*]

> あれ?おかしいね。これ。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

編集 削除
特攻隊長まるるう  2004-07-24 08:36:16  No: 114999  IP: [192.*.*.*]

4バイト パディングって何!?(>△<)ノシ☆!
…いや、言わなくていいです。だいたい分かりつつありますからw
自分で調べます。

魔界の仮面弁士さんの実行結果でいいんですよね?
…じゃあ、問題ないです(汗)

編集 削除
魔界の仮面弁士  2004-07-24 15:01:04  No: 115000  IP: [192.*.*.*]

> …いや、言わなくていいです。だいたい分かりつつありますから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の動作でしたしね。

編集 削除
nasiga  2004-07-26 10:02:09  No: 115001  IP: [192.*.*.*]

特攻隊長まるるうさん、魔界の仮面弁士さん
ご回答ありがとうございます。ご連絡が遅れてすみません。

Marshal.SizeOf(typSizeInfo) とユーザー定義型宣言時に
<StructLayout(LayoutKind.Sequential, Pack:=4)> を追加することにより
期待するサイズを取得することができました。

ありがとうございました。

編集 削除