473,720 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use reflection to read an array in an unmanaged structure

All,

I have the following:

......

[StructLayout(La youtKind.Sequen tial)]
unsafe public struct Example
{
public double we;
public double mu;
public fixed double A1[9 ];
public fixed double A2[9 ];
}

Example ex;
......

I am using reflection to discover the contents of various unmanaged
structures. The one above is just a part of a larger unmanaged
structure. In any case lets suppose an instance of the containing
structure, objBig, has been passed into the code below and that it
contains the structure "Example ex". As I loop through Field members of
objBig and when Member.Name == "Ex" -

......
Object objInside = T.InvokeMember( Member.Name,
BindingFlags.Ge tField, null, objBig, null);
FieldInfo[] fio = objInside .GetType().GetF ields();
foreach (FieldInfo fo in fio)
{
Console.WriteLi ne("Object name, value is {0}, {1}",
fo.Name, fo.GetValue(o)) ;
}
......

the following is output:

Object name, value is we, 5678
Object name, value is mu, 1234
Object name, value is A1,
ANSNavigationU+ Inputs+NavILoad s+<A1>e__FixedB uffer1f
Object name, value is A2,
ANSNavigationU+ Inputs+NavILoad s+<A2>e__FixedB uffer20
I cannot figure out how to "get the value" of the 9 array elements in
either A1 or A2. I also need ot be able to "set the value" of the array
elements. I think the way to set the value of a scalar in the above
example would be like this:

Object[] objNew = new object[] { 4321};
myType.InvokeMe mber("mu",
BindingFlags.De claredOnly | BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance | BindingFlags.St atic |
BindingFlags.Se tField, null, objInside, objNew );

Also, when I inspect the "objInside" with the debugger I can see
exactly what is in the structure for all values whether they be scalars
or arrays or further nested structures. I would have thought it would
be a lot easier the read/write the structure programmaticall y.

Any help greatly appreciated,

Bill

Dec 7 '06 #1
3 4301
Hi Bill
I have the following:

.....

[StructLayout(La youtKind.Sequen tial)]
unsafe public struct Example
{
public double we;
public double mu;
public fixed double A1[9 ];
public fixed double A2[9 ];
}

Example ex;
.....

I am using reflection to discover the contents of various unmanaged
structures. The one above is just a part of a larger unmanaged
structure. In any case lets suppose an instance of the containing
structure, objBig, has been passed into the code below and that it
contains the structure "Example ex". As I loop through Field members of
objBig and when Member.Name == "Ex" -

.....
Object objInside = T.InvokeMember( Member.Name,
BindingFlags.Ge tField, null, objBig, null);
FieldInfo[] fio = objInside .GetType().GetF ields();
foreach (FieldInfo fo in fio)
{
Console.WriteLi ne("Object name, value is {0}, {1}",
fo.Name, fo.GetValue(o)) ;
}
.....

the following is output:

Object name, value is we, 5678
Object name, value is mu, 1234
Object name, value is A1,
ANSNavigationU+ Inputs+NavILoad s+<A1>e__FixedB uffer1f
Object name, value is A2,
ANSNavigationU+ Inputs+NavILoad s+<A2>e__FixedB uffer20
At this point your OK. The Array Type doesn't override ToString(), so
you're just seeing the class name (implementation of Object.ToString ). If
you try to cast the result of GetValue to double[] you should be fine:

double[] value = (double[]) fo.GetValue(o);

(See my next response below about boxing, which applies here as well for the
GetValue method)
I cannot figure out how to "get the value" of the 9 array elements in
either A1 or A2. I also need ot be able to "set the value" of the array
elements. I think the way to set the value of a scalar in the above
example would be like this:

Object[] objNew = new object[] { 4321};
myType.InvokeMe mber("mu",
BindingFlags.De claredOnly | BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance | BindingFlags.St atic |
BindingFlags.Se tField, null, objInside, objNew );

Also, when I inspect the "objInside" with the debugger I can see
exactly what is in the structure for all values whether they be scalars
or arrays or further nested structures. I would have thought it would
be a lot easier the read/write the structure programmaticall y.
You can set the value of the scalars and the array fields using the
FieldInfo.SetVa lue method;

Since you're dealing with a structure, however, you'll have to box it before
you set the value otherwise it won't be assigned to your instance. Instead,
it would be assigned to the boxed instance created by the SetValue method.
Here's what you should do:

object o = new Example();
....
fo.SetValue(o, 1023829d);

--
Dave Sexton
Dec 7 '06 #2
Dave,

I got the following exception error message:

"Additional information: Unable to cast object of type
'<ROT_ia_from_i sa>e__FixedBuff er1f' to type 'System.Double[]'."

when I tried the cast:

"double[] value = (double[]) fo.GetValue(o); "

Could this be because I am dealing with an unmanaged structure?

Bill

PS: "ROT_ia_from_is a" is the real name of the array, not "A1"
Dave Sexton wrote:
Hi Bill
I have the following:

.....

[StructLayout(La youtKind.Sequen tial)]
unsafe public struct Example
{
public double we;
public double mu;
public fixed double A1[9 ];
public fixed double A2[9 ];
}

Example ex;
.....

I am using reflection to discover the contents of various unmanaged
structures. The one above is just a part of a larger unmanaged
structure. In any case lets suppose an instance of the containing
structure, objBig, has been passed into the code below and that it
contains the structure "Example ex". As I loop through Field members of
objBig and when Member.Name == "Ex" -

.....
Object objInside = T.InvokeMember( Member.Name,
BindingFlags.Ge tField, null, objBig, null);
FieldInfo[] fio = objInside .GetType().GetF ields();
foreach (FieldInfo fo in fio)
{
Console.WriteLi ne("Object name, value is {0}, {1}",
fo.Name, fo.GetValue(o)) ;
}
.....

the following is output:

Object name, value is we, 5678
Object name, value is mu, 1234
Object name, value is A1,
ANSNavigationU+ Inputs+NavILoad s+<A1>e__FixedB uffer1f
Object name, value is A2,
ANSNavigationU+ Inputs+NavILoad s+<A2>e__FixedB uffer20

At this point your OK. The Array Type doesn't override ToString(), so
you're just seeing the class name (implementation of Object.ToString ). If
you try to cast the result of GetValue to double[] you should be fine:

double[] value = (double[]) fo.GetValue(o);

(See my next response below about boxing, which applies here as well for the
GetValue method)
I cannot figure out how to "get the value" of the 9 array elements in
either A1 or A2. I also need ot be able to "set the value" of the array
elements. I think the way to set the value of a scalar in the above
example would be like this:

Object[] objNew = new object[] { 4321};
myType.InvokeMe mber("mu",
BindingFlags.De claredOnly | BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance | BindingFlags.St atic |
BindingFlags.Se tField, null, objInside, objNew );

Also, when I inspect the "objInside" with the debugger I can see
exactly what is in the structure for all values whether they be scalars
or arrays or further nested structures. I would have thought it would
be a lot easier the read/write the structure programmaticall y.

You can set the value of the scalars and the array fields using the
FieldInfo.SetVa lue method;

Since you're dealing with a structure, however, you'll have to box it before
you set the value otherwise it won't be assigned to your instance. Instead,
it would be assigned to the boxed instance created by the SetValue method.
Here's what you should do:

object o = new Example();
...
fo.SetValue(o, 1023829d);

--
Dave Sexton
Dec 7 '06 #3
Hi Bill,

Yes, double[] won't work unless it's a double[] Type. I made that
assumption based on your code sample but I didn't realize that the "fixed"
keyword would create a pointer to handle its fixed nature - sorry.

Unfortunately, pointers are outside my area of expertise. I'm not really
sure how to reflect into them, let alone even assign a value to them.

You might have better luck posting in a different newsgroup (maybe a C++
group or something).

Sorry I couldn't help.

--
Dave Sexton

"bill" <wg****@draper. comwrote in message
news:11******** **************@ j72g2000cwa.goo glegroups.com.. .
Dave,

I got the following exception error message:

"Additional information: Unable to cast object of type
'<ROT_ia_from_i sa>e__FixedBuff er1f' to type 'System.Double[]'."

when I tried the cast:

"double[] value = (double[]) fo.GetValue(o); "

Could this be because I am dealing with an unmanaged structure?

Bill

PS: "ROT_ia_from_is a" is the real name of the array, not "A1"
Dave Sexton wrote:
>Hi Bill
I have the following:

.....

[StructLayout(La youtKind.Sequen tial)]
unsafe public struct Example
{
public double we;
public double mu;
public fixed double A1[9 ];
public fixed double A2[9 ];
}

Example ex;
.....

I am using reflection to discover the contents of various unmanaged
structures. The one above is just a part of a larger unmanaged
structure. In any case lets suppose an instance of the containing
structure, objBig, has been passed into the code below and that it
contains the structure "Example ex". As I loop through Field members of
objBig and when Member.Name == "Ex" -

.....
Object objInside = T.InvokeMember( Member.Name,
BindingFlags.Ge tField, null, objBig, null);
FieldInfo[] fio = objInside .GetType().GetF ields();
foreach (FieldInfo fo in fio)
{
Console.WriteLi ne("Object name, value is {0}, {1}",
fo.Name, fo.GetValue(o)) ;
}
.....

the following is output:

Object name, value is we, 5678
Object name, value is mu, 1234
Object name, value is A1,
ANSNavigationU+ Inputs+NavILoad s+<A1>e__FixedB uffer1f
Object name, value is A2,
ANSNavigationU+ Inputs+NavILoad s+<A2>e__FixedB uffer20

At this point your OK. The Array Type doesn't override ToString(), so
you're just seeing the class name (implementation of Object.ToString ).
If
you try to cast the result of GetValue to double[] you should be fine:

double[] value = (double[]) fo.GetValue(o);

(See my next response below about boxing, which applies here as well for
the
GetValue method)
I cannot figure out how to "get the value" of the 9 array elements in
either A1 or A2. I also need ot be able to "set the value" of the array
elements. I think the way to set the value of a scalar in the above
example would be like this:

Object[] objNew = new object[] { 4321};
myType.InvokeMe mber("mu",
BindingFlags.De claredOnly | BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance | BindingFlags.St atic |
BindingFlags.Se tField, null, objInside, objNew );

Also, when I inspect the "objInside" with the debugger I can see
exactly what is in the structure for all values whether they be scalars
or arrays or further nested structures. I would have thought it would
be a lot easier the read/write the structure programmaticall y.

You can set the value of the scalars and the array fields using the
FieldInfo.SetV alue method;

Since you're dealing with a structure, however, you'll have to box it
before
you set the value otherwise it won't be assigned to your instance.
Instead,
it would be assigned to the boxed instance created by the SetValue
method.
Here's what you should do:

object o = new Example();
...
fo.SetValue( o, 1023829d);

--
Dave Sexton

Dec 7 '06 #4

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

Similar topics

11
30573
by: Dan C | last post by:
Is there a routine in c# that will transform a string ie"Hello Mom" into a Byte array. I have found char cTmp = pString.ToCharArray(); But I have not been able to figure out how to convert a char into a hex value and place that value into the byte. Thanks for your help
1
9500
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this worked pretty well, accepting to write unsafe code ;-) But my next struct has an array inside and I don't get it passed over to the DLL correctly. Here my struct in c#:
2
11950
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C# using memory mapped file. We already have C++ code for handling memory mapped file. I am working on converting code for memory mapped file in C#. Now I have to pass pointer to the above structure. I converted this structure to C# as follows:
5
6312
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and array of UDT.
1
3770
by: Jonathan Amend | last post by:
I'm trying to port some C++ code to VB.NET but I have hit a snag. I need to add the pointer of a structure which includes an array to an array and then pass it to an API. Here are the two code snippets: long inbuf; // Array that gets passed to the API struct cmbuf { short cmds; long cm2; } cbuf; // Structure whos pointer must be added to inbuf cbuf.cmds = 0;
9
2392
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure that I use to communicate to some unmanaged code in a DLL written in C. It is not complicated, but will change and I would like to be able to sequentially access it without explicitly referring to each and every element. Here is the structure:
6
1853
by: bill | last post by:
All, I have an unmanaged data structure that I use to pass to an umanaged DLL written in C. This works great. The structure looks sort of like this: unsafe public struct Inputs { .....
6
8965
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array must be allocated (with known max length = 10) before the call to the dll function (the dll just fills it ,with no allocations). The definitions of Mystruct and :
17
2305
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection to build a compiler? One that will construct a user defined class "on the fly" (literally, the user defines a class, instantiates it, and runs it from the console mode, all the while prompted by the program)? I guess so, but my final question...
0
8833
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8727
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
9365
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
9210
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
9126
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
7998
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
6661
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
4482
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3186
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.