StructLayoutAttribute クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > StructLayoutAttribute クラスの意味・解説 

StructLayoutAttribute クラス

StructLayoutAttribute クラス使用すると、クラスまたは構造体データ フィールド物理的なレイアウト制御できます

名前空間: System.Runtime.InteropServices
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct,
 Inherited:=False)> _
Public NotInheritable Class
 StructLayoutAttribute
    Inherits Attribute
Dim instance As StructLayoutAttribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false)]
 
public sealed class StructLayoutAttribute :
 Attribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Struct, Inherited=false)]
 
public ref class StructLayoutAttribute sealed
 : public Attribute
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct,
 Inherited=false) */ 
public final class StructLayoutAttribute extends
 Attribute
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false)
 
public final class StructLayoutAttribute extends
 Attribute
解説解説
使用例使用例

次に示すのは、GetSystemTime 関数マネージ宣言の例です。このコードは、MySystemTime クラスを LayoutKind.Explicit レイアウト定義しますまた、GetSystemTime は、システム タイム取得してコンソール出力します

   <StructLayout(LayoutKind.Explicit, Size := 16, CharSet := CharSet.Ansi)>  _
   Public Class MySystemTime
      <FieldOffset(0)> Public wYear As
 Short
      <FieldOffset(2)> Public wMonth As
 Short
      <FieldOffset(4)> Public wDayOfWeek As
 Short
      <FieldOffset(6)> Public wDay As
 Short
      <FieldOffset(8)> Public wHour As
 Short
      <FieldOffset(10)> Public wMinute As
 Short
      <FieldOffset(12)> Public wSecond As
 Short
      <FieldOffset(14)> Public wMilliseconds As
 Short
   End Class 'MySystemTime

   
   Class LibWrapper
      
      <DllImport("kernel32.dll")>  _
      Public Shared Sub
 GetSystemTime(<MarshalAs(UnmanagedType.LPStruct)> st As
 MySystemTime)
      End SUb    
   End Class 'LibWrapper

   Class TestApplication
      
      Public Shared Sub
 Main()
         Try
            Dim sysTime As New
 MySystemTime()
            LibWrapper.GetSystemTime(sysTime)
            Console.WriteLine("The System time is {0}/{1}/{2}
 {3}:{4}:{5}", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour,
 sysTime.wMinute, sysTime.wSecond)
         Catch e As TypeLoadException
            Console.WriteLine(("TypeLoadException : "
 + e.Message.ToString()))
         Catch e As Exception
            Console.WriteLine(("Exception : " + e.Message.ToString()))
         End Try
      End Sub 'Main
   End Class 'TestApplication
End Namespace 'InteropSample 
[StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
public class MySystemTime 
{
   [FieldOffset(0)]public ushort wYear; 
   [FieldOffset(2)]public ushort wMonth;
   [FieldOffset(4)]public ushort wDayOfWeek; 
   [FieldOffset(6)]public ushort wDay; 
   [FieldOffset(8)]public ushort wHour; 
   [FieldOffset(10)]public ushort wMinute; 
   [FieldOffset(12)]public ushort wSecond; 
   [FieldOffset(14)]public ushort wMilliseconds; 
}

class LibWrapper
{
   [DllImport("kernel32.dll")]
   public static extern void
 GetSystemTime([MarshalAs(UnmanagedType.LPStruct)]MySystemTime st);
};

class TestApplication
{      
   public static void Main()
   {
      try
      {
         MySystemTime sysTime = new MySystemTime();
         LibWrapper.GetSystemTime(sysTime);
         Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}",
 sysTime.wDay,
            sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
            
      }         
      catch(TypeLoadException e)
      {
         Console.WriteLine("TypeLoadException : " + e.Message);
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}
[StructLayout(LayoutKind::Explicit,Size=16,CharSet=CharSet::Ansi)]
value class MySystemTime
{
public:

   [FieldOffset(0)]
   short wYear;

   [FieldOffset(2)]
   short wMonth;

   [FieldOffset(4)]
   short wDayOfWeek;

   [FieldOffset(6)]
   short wDay;

   [FieldOffset(8)]
   short wHour;

   [FieldOffset(10)]
   short wMinute;

   [FieldOffset(12)]
   short wSecond;

   [FieldOffset(14)]
   short wMilliseconds;
};

ref class LibWrapper
{
public:

   [DllImport("kernel32.dll")]
   static void GetSystemTime( MySystemTime
 * st );
};

int main()
{
   try
   {
      MySystemTime sysTime;
      LibWrapper::GetSystemTime(  &sysTime );
      Console::WriteLine( "The System time is {0}/{1}/{2} {3}:{4}:{5}",
 sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond
 );
   }
   catch ( TypeLoadException^ e ) 
   {
      Console::WriteLine( "TypeLoadException : {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e->Message );
   }

}

/** @attribute StructLayout(LayoutKind.Explicit, Size = 16, 
    CharSet = CharSet.Ansi)
 */
public class MySystemTime
{
    /** @attribute FieldOffset(0)
     */
    public short wYear;
    
    /** @attribute FieldOffset(2)
     */
    public short wMonth;
    
    /** @attribute FieldOffset(4)
     */
    public short wDayOfWeek;

    /** @attribute FieldOffset(6)
     */
    public short wDay;

    /** @attribute FieldOffset(8)
     */
    public short wHour;

    /** @attribute FieldOffset(10)
     */
    public short wMinute;

    /** @attribute FieldOffset(12)
     */
    public short wSecond;

    /** @attribute FieldOffset(14)
     */
    public short wMilliseconds;

} //MySystemTime

class LibWrapper
{
    /** @attribute DllImport("kernel32.dll")
     */
    public static native void
 GetSystemTime(
        /** @attribute MarshalAs(UnmanagedType.LPStruct)
         */
        MySystemTime st);
} //LibWrapper

class TestApplication
{
    public static void main(String[]
 args)
    {
        try {
            MySystemTime sysTime = new MySystemTime();
            LibWrapper.GetSystemTime(sysTime);
            Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}"
,
                new Object[] { (Int32)sysTime.wDay, (Int32)sysTime.wMonth
,
                (Int32)sysTime.wYear, (Int32)sysTime.wHour, (Int32)sysTime.
                wMinute, (Int32)sysTime.wSecond });
        }
        catch (TypeLoadException e) {
            Console.WriteLine("TypeLoadException : " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : " + e.get_Message());
        }
    } //main
} //TestApplication
継承階層継承階層
System.Object
   System.Attribute
    System.Runtime.InteropServices.StructLayoutAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
StructLayoutAttribute メンバ
System.Runtime.InteropServices 名前空間
その他の技術情報
タイプ ライブラリ インポータ (Tlbimp.exe)



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「StructLayoutAttribute クラス」の関連用語

StructLayoutAttribute クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



StructLayoutAttribute クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS