473,769 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DynamicAssembly , FieldBuilder and fixed size buffers

Hi
I am trying to dynamically create the following structure using
AssemblyBuilder , TypeBuilder and DefineField:

[StructLayout(La youtKind.Sequen tial, Pack = 8)]
public struct SimpleType2
{
public double dScalar1;
public fixed double dArray[5];
public int iScalar1;
}

I've got pretty much everything working except the fixed keyword on the
dArray[5] member. I tried to add this using a custom attribute, but as soon
as I did that, the field was not added to the created type. Is this the
right approach? Could it be that I need to mark the code as unsafe or
something?

Any help would be much appreciated.

Best regards

Marek
Jun 27 '08 #1
6 2181
Hello Marek,

I'd suggest you use the utility "ILReader" from:
http://blogs.msdn.com/yirutang/archi...07/621125.aspx
plus the .net Reflector from
http://www.aisto.com/roeder/dotnet/

The two tools can help us write codes to dynamically create
assembly/module/type. With the help of the tools, I wrote the following
code for your SimpleType2 type:
(Note: because Newsgroup system may wrap the long sentences and make it
hard for you to read my codes, I attach the .cs file to this message for
your convenience. You can download the .cs code file with outlook express
or windows mail)

public class Test
{
static TypeBuilder fixedBufTypeBui lder;

public static void Main()
{
AppDomain.Curre ntDomain.TypeRe solve += new
ResolveEventHan dler(CurrentDom ain_TypeResolve );

#region AttributeBuilde rs
// UnsafeValueType Attribute
ConstructorInfo unsafeValueType Ctor =
typeof(UnsafeVa lueTypeAttribut e).GetConstruct or(new Type[] { });
CustomAttribute Builder unsafeAttrBuild er = new
CustomAttribute Builder(unsafeV alueTypeCtor, new object[] { });

// UnverifiableCod eAttribute
ConstructorInfo unverifiableCto r =
typeof(Unverifi ableCodeAttribu te).GetConstruc tor(new Type[] { });
CustomAttribute Builder unverifiableAtt rBuilder = new
CustomAttribute Builder(unverif iableCtor, new object[] { });

// CompilerGenerat edAttribute
ConstructorInfo compilerGenCtor =
typeof(Compiler GeneratedAttrib ute).GetConstru ctor(new Type[] { });
CustomAttribute Builder compilerGenAttr Builder = new
CustomAttribute Builder(compile rGenCtor, new object[] { });

// FixedBufferAttr ibute
ConstructorInfo fixedBufCtor =
typeof(FixedBuf ferAttribute).G etConstructor(n ew Type[] { typeof(Type),
typeof(int) });
CustomAttribute Builder fixedBufAttrBui lder = new
CustomAttribute Builder(fixedBu fCtor, new object[] { typeof(double), 5 });
#endregion

AssemblyName asmname = new AssemblyName("m ytest");
AssemblyBuilder asmbuild =
AppDomain.Curre ntDomain.Define DynamicAssembly (asmname,
AssemblyBuilder Access.RunAndSa ve);
ModuleBuilder moduleBuilder =
asmbuild.Define DynamicModule(" mytest", "mytest.dll ");
moduleBuilder.S etCustomAttribu te(unverifiable AttrBuilder);

TypeBuilder typeBuilder = moduleBuilder.D efineType("Simp leType2",
TypeAttributes. Public | TypeAttributes. SequentialLayou t,
typeof(ValueTyp e), PackingSize.Siz e8);

#region <dArray>e__Fixe dBuffer0
fixedBufTypeBui lder =
typeBuilder.Def ineNestedType(" <dArray>e__Fixe dBuffer0",
TypeAttributes. NestedPublic | TypeAttributes. SequentialLayou t,
typeof(ValueTyp e), 40);
fixedBufTypeBui lder.SetCustomA ttribute(compil erGenAttrBuilde r);
fixedBufTypeBui lder.SetCustomA ttribute(unsafe AttrBuilder);
fixedBufTypeBui lder.DefineFiel d("FixedElement Field",
typeof(double), FieldAttributes .Public);
#endregion

typeBuilder.Def ineField("dScal ar1", typeof(double),
FieldAttributes .Public);
FieldBuilder fixedBufFieldBu ilder =
typeBuilder.Def ineField("dArra y", fixedBufTypeBui lder,
FieldAttributes .Public);
fixedBufFieldBu ilder.SetCustom Attribute(fixed BufAttrBuilder) ;
typeBuilder.Def ineField("iScal ar1", typeof(int),
FieldAttributes .Public);

typeBuilder.Cre ateType();

asmbuild.Save(" mytest");
}

private static Assembly CurrentDomain_T ypeResolve(obje ct sender,
ResolveEventArg s args)
{
if (args.Name == "<dArray>e__Fix edBuffer0")
{
return fixedBufTypeBui lder.CreateType ().Assembly;
}
return null;
}
}

In the code above, the line
AppDomain.Curre ntDomain.TypeRe solve += new
ResolveEventHan dler(CurrentDom ain_TypeResolve );
Is used for the nested struct type. See:
https://connect.microsoft.com/Visual...ck.aspx?Feedba
ckID=98299

Below is my detailed steps when I write the code above (I think this may
help you if you encounter such cases again in future)
Step1. Compose a simple assembly with our target code:
[StructLayout(La youtKind.Sequen tial, Pack = 8)]
public unsafe struct SimpleType2
{
public double dScalar1;
public fixed double dArray[5];
public int iScalar1;
}

Step2. Compile the assembly, use .NET Reflector to open the output dll, and
use ILReader to parse the dll.
Step3. Translate the output of ILReader to our AssemblyBuilder ,
TypeBuilder, DefineField codes
Step4. Run our AssemblyBuilder , TypeBuilder, DefineField codes and get the
resulting assembly dll.
Step5. Use .Net Reflector to open the dll, and compare its result with the
that of the dll in step2.

If you have any other concerns or questions, feel free to let me know.

Regards,
Jialiang Ge (ji****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #2
Hi Jialiang
Fantastic - that has done the trick. Believe it or not this code is now
calling into a FORTRAN dll with a structure, which is exactly what I wanted
to do. As with all these things though, one answer generates another
question:

I would like to display the fixed array field correctly inside the property
grid and I notice the debugger has no problem working out what the field is,
whereas the property grid does. I guess I need some sort of TypeConverter?
Is there a standard way of achieving what I need?

Many thanks again.

Best regards

Marek

"Jialiang Ge [MSFT]" wrote:
Hello Marek,

I'd suggest you use the utility "ILReader" from:
http://blogs.msdn.com/yirutang/archi...07/621125.aspx
plus the .net Reflector from
http://www.aisto.com/roeder/dotnet/

The two tools can help us write codes to dynamically create
assembly/module/type. With the help of the tools, I wrote the following
code for your SimpleType2 type:
(Note: because Newsgroup system may wrap the long sentences and make it
hard for you to read my codes, I attach the .cs file to this message for
your convenience. You can download the .cs code file with outlook express
or windows mail)

public class Test
{
static TypeBuilder fixedBufTypeBui lder;

public static void Main()
{
AppDomain.Curre ntDomain.TypeRe solve += new
ResolveEventHan dler(CurrentDom ain_TypeResolve );

#region AttributeBuilde rs
// UnsafeValueType Attribute
ConstructorInfo unsafeValueType Ctor =
typeof(UnsafeVa lueTypeAttribut e).GetConstruct or(new Type[] { });
CustomAttribute Builder unsafeAttrBuild er = new
CustomAttribute Builder(unsafeV alueTypeCtor, new object[] { });

// UnverifiableCod eAttribute
ConstructorInfo unverifiableCto r =
typeof(Unverifi ableCodeAttribu te).GetConstruc tor(new Type[] { });
CustomAttribute Builder unverifiableAtt rBuilder = new
CustomAttribute Builder(unverif iableCtor, new object[] { });

// CompilerGenerat edAttribute
ConstructorInfo compilerGenCtor =
typeof(Compiler GeneratedAttrib ute).GetConstru ctor(new Type[] { });
CustomAttribute Builder compilerGenAttr Builder = new
CustomAttribute Builder(compile rGenCtor, new object[] { });

// FixedBufferAttr ibute
ConstructorInfo fixedBufCtor =
typeof(FixedBuf ferAttribute).G etConstructor(n ew Type[] { typeof(Type),
typeof(int) });
CustomAttribute Builder fixedBufAttrBui lder = new
CustomAttribute Builder(fixedBu fCtor, new object[] { typeof(double), 5 });
#endregion

AssemblyName asmname = new AssemblyName("m ytest");
AssemblyBuilder asmbuild =
AppDomain.Curre ntDomain.Define DynamicAssembly (asmname,
AssemblyBuilder Access.RunAndSa ve);
ModuleBuilder moduleBuilder =
asmbuild.Define DynamicModule(" mytest", "mytest.dll ");
moduleBuilder.S etCustomAttribu te(unverifiable AttrBuilder);

TypeBuilder typeBuilder = moduleBuilder.D efineType("Simp leType2",
TypeAttributes. Public | TypeAttributes. SequentialLayou t,
typeof(ValueTyp e), PackingSize.Siz e8);

#region <dArray>e__Fixe dBuffer0
fixedBufTypeBui lder =
typeBuilder.Def ineNestedType(" <dArray>e__Fixe dBuffer0",
TypeAttributes. NestedPublic | TypeAttributes. SequentialLayou t,
typeof(ValueTyp e), 40);
fixedBufTypeBui lder.SetCustomA ttribute(compil erGenAttrBuilde r);
fixedBufTypeBui lder.SetCustomA ttribute(unsafe AttrBuilder);
fixedBufTypeBui lder.DefineFiel d("FixedElement Field",
typeof(double), FieldAttributes .Public);
#endregion

typeBuilder.Def ineField("dScal ar1", typeof(double),
FieldAttributes .Public);
FieldBuilder fixedBufFieldBu ilder =
typeBuilder.Def ineField("dArra y", fixedBufTypeBui lder,
FieldAttributes .Public);
fixedBufFieldBu ilder.SetCustom Attribute(fixed BufAttrBuilder) ;
typeBuilder.Def ineField("iScal ar1", typeof(int),
FieldAttributes .Public);

typeBuilder.Cre ateType();

asmbuild.Save(" mytest");
}

private static Assembly CurrentDomain_T ypeResolve(obje ct sender,
ResolveEventArg s args)
{
if (args.Name == "<dArray>e__Fix edBuffer0")
{
return fixedBufTypeBui lder.CreateType ().Assembly;
}
return null;
}
}

In the code above, the line
AppDomain.Curre ntDomain.TypeRe solve += new
ResolveEventHan dler(CurrentDom ain_TypeResolve );
Is used for the nested struct type. See:
https://connect.microsoft.com/Visual...ck.aspx?Feedba
ckID=98299

Below is my detailed steps when I write the code above (I think this may
help you if you encounter such cases again in future)
Step1. Compose a simple assembly with our target code:
[StructLayout(La youtKind.Sequen tial, Pack = 8)]
public unsafe struct SimpleType2
{
public double dScalar1;
public fixed double dArray[5];
public int iScalar1;
}

Step2. Compile the assembly, use .NET Reflector to open the output dll, and
use ILReader to parse the dll.
Step3. Translate the output of ILReader to our AssemblyBuilder ,
TypeBuilder, DefineField codes
Step4. Run our AssemblyBuilder , TypeBuilder, DefineField codes and get the
resulting assembly dll.
Step5. Use .Net Reflector to open the dll, and compare its result with the
that of the dll in step2.

If you have any other concerns or questions, feel free to let me know.

Regards,
Jialiang Ge (ji****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights
Jun 27 '08 #3
Hi Marek,

Would you let me know what the "property grid" is? Do you mean the Property
window in Visual Studio IDE? How do you display the fixed field in it
currently?

Regards,
Jialiang Ge (ji****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== ====
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=============== =============== =============== ====

Jun 27 '08 #4
Hi Jialiang
The property grid is essentially the standard .NET (Windows Forms) property
grid control which I would like the users to use to populate this fixed array
structure before calling into the FORTRAN DLL.

I have tried to extract the the data from the object using code such as this:

object data =
parameterValues[0].GetType().Invo keMember("dArra y", BindingFlags.Ge tField,
null, parameterValues[0], null);

List<doublevalu es = new List<double>();
double start =
(double)instanc e.GetType().Get Field("FixedEle mentField").Get Value(instance) ;

fixed (double* pValue = (double*)(&star t))
{
for (int i = 0; i < 5; i++)
values.Add(*(pV alue + i));
}

But this is not giving me the values I am expecting. The first one is
correct, after that it all seems to go wrong!

If I could get the above to work, I might be able to attach a TypeConverter
to the field(?) so that it displays correctly in the .NET property grid.

Best regards

Marek
"Jialiang Ge [MSFT]" wrote:
Hi Marek,

Would you let me know what the "property grid" is? Do you mean the Property
window in Visual Studio IDE? How do you display the fixed field in it
currently?

Regards,
Jialiang Ge (ji****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== ====
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=============== =============== =============== ====

Jun 27 '08 #5
Hello Marek,

The following solution may work, but I understand it's a very bad and
tricky one. I post it here simply to let you know that I am still
researching the issue and will find a better one soon.

object data = myVal.GetType() .InvokeMember(" dArray",
BindingFlags.Ge tField, null, myVal, null);
List<doublevalu es = new List<double>();
double start =
(double)data.Ge tType().GetFiel d("FixedElement Field").GetValu e(data);
double* startAddr = &start;

// I find the array's values start from (startAddr + 3) in your
struct SimpleType2
// and the value at (startAddr + 2) is dScalar1
for (int i = 3; i < 3 + 5; i++)
values.Add(*(st artAddr + i));

Regards,
Jialiang Ge (ji****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== ====
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=============== =============== =============== ====

Jun 27 '08 #6
Hello Marek,

Based on my further researches, I find we can use the method:
Marshal.OffsetO f to get the correct field offset of the unmanaged form in
the managed struct/class. I write the following sample code which works
well on my side. Please try it and let me know if it helps in your .NET and
FORTRAN DLL interoperabilit y scenario.

unsafe
{
SimpleType2 myVal;
myVal.dScalar1 = 3.0;
myVal.dArray[0] = 1.1;
myVal.dArray[1] = 1.2;
myVal.dArray[2] = 1.3;
myVal.dArray[3] = 1.4;
myVal.dArray[4] = 1.5;
myVal.iScalar1 = 4;

byte* pStruct = (byte*)&myVal;
IntPtr dArrayPtr = Marshal.OffsetO f(myVal.GetType (), "dArray");
pStruct += (int)dArrayPtr;
double* pValue = (double*) pStruct;
List<doublevalu es = new List<double>();
for (int i = 0; i < 5; i++)
values.Add(*(pV alue + i));
}

If you have any other concerns or questions, feel free to let me know.

Regards,
Jialiang Ge
Microsoft Online Community Support

=============== =============== =============== ====
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=============== =============== =============== ====

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
32587
by: Stephan Steiner | last post by:
Hi Generally, FileInfo fi = new FileInfo(path); long size = fi.Length; gets you the length of a file in bytes. However, when copying files, even while the copy operation is still in progress, the filesize, as indicated in Windows Explorer or derived with the above two lines of code, will be the size of the file once the copy operation has completed. Is there a way to
4
1775
by: Paul | last post by:
I'm doing some Pinvoke stuff. How do I deal with functions that pass/receive structures that have fixed length char buffers? I see this: public struct MyArray // This code must appear in an unsafe block { public fixed char pathName; }
2
4656
by: ldawson | last post by:
From the same C++ source code, I'm attempting to generate both an unmanaged DLL and a managed assembly. This will eliminate interop as the calling code is slowly migrated to .NET. However, C++ native compilation allows fixed size buffers (e.g. char pathName) which we've used to represent a memory mapped file, and I can't find out how to implement the same in C++/CLI i.e. #ifdef _MANAGED ref class MyClass
4
23823
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type specifiers" error. Thank you, Scott
4
2195
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry { public byte MsgType; public uint Tag; public fixed byte ID; public fixed byte Val1;
1
10431
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
4
1855
by: Jack | last post by:
Hi, I do a webrequest and it returns some text data in a stream. I want to put this text data into a string. I've got it working just fine, but I have to put the text data into into a fixed-size buffer BEFORE I put it into a string (ConstBufferByteSize=1000000). This fixed size buffer wastes space. Is
6
7450
by: =?Utf-8?B?TFBldGVy?= | last post by:
Hi, I would copy the characters of a string variable to a fixed character buffer in a struct (and vice versa) in C#. public struct S { ... public fixed char cBuff; ...
2
11713
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory leaking(thoug i konw i just donot have to).Then i look inside in time.h file(mingw) ,and i found notes say"These functions write to and return pointers to static buffers that may be overwritten by other function calls".So how is the"static buffers"...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.