473,782 Members | 2,465 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 4306
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
30581
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
9503
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
11954
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
6314
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
3771
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
2397
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
1855
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
8967
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
2310
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
9641
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...
1
10080
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
9944
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
8968
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...
0
6735
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();...
0
5378
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.