This documentation is archived and is not being maintained.

ModuleBuilder.DefineInitializedData Method

Defines initialized data field in the .sdata section of the portable executable (PE) file.

[Visual Basic]
Public Function DefineInitializedData( _
   ByVal name As String, _
   ByVal data() As Byte, _
   ByVal attributes As FieldAttributes _
) As FieldBuilder
[C#]
public FieldBuilder DefineInitializedData(
 string name,
 byte[] data,
 FieldAttributes attributes
);
[C++]
public: FieldBuilder* DefineInitializedData(
 String* name,
 unsigned char data __gc[],
 FieldAttributes attributes
);
[JScript]
public function DefineInitializedData(
   name : String,
 data : Byte[],
 attributes : FieldAttributes
) : FieldBuilder;

Parameters

name
The name used to refer to the data. name cannot contain embedded nulls.
data
The blob of data.
attributes
The attributes for the field. The default is Static.

Return Value

A field to reference the data.

Exceptions

Exception Type Condition
ArgumentException The length of name is zero.

-or-

The size of data is less than or equal to zero or greater than or equal to 0x3f0000.

ArgumentNullException name or data is a null reference (Nothing in Visual Basic).
InvalidOperationException CreateGlobalFunctions has been previously called.

Example

[Visual Basic] 
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain
myAssemblyName = New AssemblyName()
myAssemblyName.Name = "TempAssembly"
' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = _
      currentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")
' Define the initialized data field in the .sdata section of the PE file.
Dim myFieldBuilder As FieldBuilder = _
      myModuleBuilder.DefineInitializedData("MyField", New Byte() {1, 0, 1}, _
      FieldAttributes.Static Or FieldAttributes.Public)
myModuleBuilder.CreateGlobalFunctions()

[C#] 
AppDomain currentDomain;
AssemblyName myAssemblyName;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.Run);

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");

// Define the initialized data field in the .sdata section of the PE file.
FieldBuilder myFieldBuilder = 
    myModuleBuilder.DefineInitializedData("MyField",new byte[]{01,00,01},
               FieldAttributes.Static|FieldAttributes.Public);
 myModuleBuilder.CreateGlobalFunctions();

[C++] 
AppDomain* currentDomain;
AssemblyName* myAssemblyName;

// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName->Name = S"TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);

// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule(S"TempModule");

// Define the initialized data field in the .sdata section of the PE file.
Byte temp0 [] = {01,00,01};
FieldBuilder* myFieldBuilder = myModuleBuilder->DefineInitializedData(
   S"MyField",
   temp0,
   static_cast<FieldAttributes>(FieldAttributes::Static|FieldAttributes::Public));
myModuleBuilder->CreateGlobalFunctions();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

ModuleBuilder Class | ModuleBuilder Members | System.Reflection.Emit Namespace

Show: