473,837 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reflection to read/write unmanaged data stucture

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
{
.....
[StructLayout(La youtKind.Sequen tial)]
public struct InputA
{
public fixed double array1[3];
public double dt;
public fixed double array2[3];
}
.....
}

The unmanaged DLL is compiled as part of my Solution so there should be
a bunch of metadata laying around. I now know how to read a value from
a field in the structure thru reflection - more or less like this:

Inputs.InputA inp = new Inputs.InputA() ;
....
Type t = inp.GetType();
MemberInfo[] m = t.GetMembers();

// Find the index in m[] where the Field "dt" is located (not a
problem) - "9" in this example

Object dt = t.InvokeMember( m[9].Name, BindingFlags.Ge tField,
null, inp, null);

Wonderfully, dt contains the value from the inp structure. Now the
problem... I want to set the value of "dt" in the inp structure to
something else. I try the following:

Object[] newDT= new object[] { 2.777 };
t.InvokeMember( myMemberInfo[9].Name,
BindingFlags.De claredOnly | BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance | BindingFlags.St atic |
BindingFlags.Se tField, null, inp, newDT);

The value of "dt" in the inp structure does NOT change, and there is no
error message. Any suggestions greatfully received. Yes, I know that I
can access the structure directly. However, I REALLY DO have a good
reason to want to use reflection to do this.

TIA,

Bill

Nov 7 '06 #1
6 1858

"bill" <wg****@draper. comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
| 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
| {
| ....
| [StructLayout(La youtKind.Sequen tial)]
| public struct InputA
| {
| public fixed double array1[3];
| public double dt;
| public fixed double array2[3];
| }
| ....
| }
|
| The unmanaged DLL is compiled as part of my Solution so there should be
| a bunch of metadata laying around. I now know how to read a value from
| a field in the structure thru reflection - more or less like this:
|
| Inputs.InputA inp = new Inputs.InputA() ;
| ...
| Type t = inp.GetType();
| MemberInfo[] m = t.GetMembers();
|
| // Find the index in m[] where the Field "dt" is located (not a
| problem) - "9" in this example
|
| Object dt = t.InvokeMember( m[9].Name, BindingFlags.Ge tField,
| null, inp, null);
|
| Wonderfully, dt contains the value from the inp structure. Now the
| problem... I want to set the value of "dt" in the inp structure to
| something else. I try the following:
|
| Object[] newDT= new object[] { 2.777 };
| t.InvokeMember( myMemberInfo[9].Name,
| BindingFlags.De claredOnly | BindingFlags.Pu blic |
| BindingFlags.No nPublic |
| BindingFlags.In stance | BindingFlags.St atic |
| BindingFlags.Se tField, null, inp, newDT);
|
| The value of "dt" in the inp structure does NOT change, and there is no
| error message. Any suggestions greatfully received. Yes, I know that I
| can access the structure directly. However, I REALLY DO have a good
| reason to want to use reflection to do this.
|
| TIA,
|
| Bill
|

InvokeMember( , BindingFlags.Se tField,..)
involves a boxing operation, you have to box the value type and pass the
boxed value to InvokeMember's target and unbox after the call.
Quite expensive, you must have very good reasons to do this.

Willy.
Nov 7 '06 #2

"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>
"bill" <wg****@draper. comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
| 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
| {
| ....
| [StructLayout(La youtKind.Sequen tial)]
| public struct InputA
| {
| public fixed double array1[3];
| public double dt;
| public fixed double array2[3];
| }
| ....
| }
|
| The unmanaged DLL is compiled as part of my Solution so there should be
| a bunch of metadata laying around. I now know how to read a value from
| a field in the structure thru reflection - more or less like this:
That's not really an unmanaged structure, because you have metadata to
manage it. Maybe better to call it "explicit layout". The only kind of
"metadata" you can get for an unmanaged structure is the .pdb debug
database.
|
| Inputs.InputA inp = new Inputs.InputA() ;
| ...
| Type t = inp.GetType();
| MemberInfo[] m = t.GetMembers();
|
| // Find the index in m[] where the Field "dt" is located (not a
| problem) - "9" in this example
|
| Object dt = t.InvokeMember( m[9].Name, BindingFlags.Ge tField,
| null, inp, null);
|
| Wonderfully, dt contains the value from the inp structure. Now the
| problem... I want to set the value of "dt" in the inp structure to
| something else. I try the following:
|
| Object[] newDT= new object[] { 2.777 };
| t.InvokeMember( myMemberInfo[9].Name,
| BindingFlags.De claredOnly | BindingFlags.Pu blic |
| BindingFlags.No nPublic |
| BindingFlags.In stance | BindingFlags.St atic |
| BindingFlags.Se tField, null, inp, newDT);
|
| The value of "dt" in the inp structure does NOT change, and there is no
| error message. Any suggestions greatfully received. Yes, I know that I
| can access the structure directly. However, I REALLY DO have a good
| reason to want to use reflection to do this.
|
| TIA,
|
| Bill
|

InvokeMember( , BindingFlags.Se tField,..)
involves a boxing operation, you have to box the value type and pass the
boxed value to InvokeMember's target and unbox after the call.
Quite expensive, you must have very good reasons to do this.
More to the point, InvokeMember operates on the boxed copy, not the real
value. You need to keep a reference to the boxed copy:

object boxinp = inp;
InvokeMember(.. .inp...);

Now boxinp should be changed. You can set all the variables you want, then
at the end,
inp = (InputA)boxinp;
>
Willy.


Nov 7 '06 #3

"Ben Voigt" <rb*@nospam.nos pamwrote in message
news:OK******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
| >
| "bill" <wg****@draper. comwrote in message
| news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
| | 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
| | {
| | ....
| | [StructLayout(La youtKind.Sequen tial)]
| | public struct InputA
| | {
| | public fixed double array1[3];
| | public double dt;
| | public fixed double array2[3];
| | }
| | ....
| | }
| |
| | The unmanaged DLL is compiled as part of my Solution so there should
be
| | a bunch of metadata laying around. I now know how to read a value from
| | a field in the structure thru reflection - more or less like this:
|
| That's not really an unmanaged structure, because you have metadata to
| manage it. Maybe better to call it "explicit layout". The only kind of
| "metadata" you can get for an unmanaged structure is the .pdb debug
| database.
|
| |
| | Inputs.InputA inp = new Inputs.InputA() ;
| | ...
| | Type t = inp.GetType();
| | MemberInfo[] m = t.GetMembers();
| |
| | // Find the index in m[] where the Field "dt" is located (not a
| | problem) - "9" in this example
| |
| | Object dt = t.InvokeMember( m[9].Name, BindingFlags.Ge tField,
| | null, inp, null);
| |
| | Wonderfully, dt contains the value from the inp structure. Now the
| | problem... I want to set the value of "dt" in the inp structure to
| | something else. I try the following:
| |
| | Object[] newDT= new object[] { 2.777 };
| | t.InvokeMember( myMemberInfo[9].Name,
| | BindingFlags.De claredOnly | BindingFlags.Pu blic |
| | BindingFlags.No nPublic |
| | BindingFlags.In stance | BindingFlags.St atic |
| | BindingFlags.Se tField, null, inp, newDT);
| |
| | The value of "dt" in the inp structure does NOT change, and there is
no
| | error message. Any suggestions greatfully received. Yes, I know that I
| | can access the structure directly. However, I REALLY DO have a good
| | reason to want to use reflection to do this.
| |
| | TIA,
| |
| | Bill
| |
| >
| InvokeMember( , BindingFlags.Se tField,..)
| involves a boxing operation, you have to box the value type and pass the
| boxed value to InvokeMember's target and unbox after the call.
| Quite expensive, you must have very good reasons to do this.
|
| More to the point, InvokeMember operates on the boxed copy, not the real
| value.

Hmmm... where exactly did I say that it operates on the real value?
InvokeMember 'involves a boxing operation', that is, it implicitly boxes the
value type passed in as an argument, but after the call the "boxed" instance
vanishes waiting to be collected (it's no longer referenced) , so you must
box before calling an pass the boxed value to InvokeMember and unbox when
done.

like this:

object temp = new Inputs() as object;
typeof(Inputs). InvokeMember("i ", BindingFlags.Se tField , null, temp,
new object[] {1.23});
Inputs inp = (Inputs) so;

| You need to keep a reference to the boxed copy:
|
| object boxinp = inp;
| InvokeMember(.. .inp...);
|

small correction..... you need to pass the 'boxed' reference boxinp.

| Now boxinp should be changed. You can set all the variables you want,
then
| at the end,
| inp = (InputA)boxinp;
|
| >
| Willy.
| >
| >

Willy.
Nov 7 '06 #4
Gentlemen,

Thanks so much, its works perfectly.

Bill
Willy Denoyette [MVP] wrote:
"Ben Voigt" <rb*@nospam.nos pamwrote in message
news:OK******** ********@TK2MSF TNGP02.phx.gbl. ..
|
| "Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
| news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
| >
| "bill" <wg****@draper. comwrote in message
| news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
| | 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
| | {
| | ....
| | [StructLayout(La youtKind.Sequen tial)]
| | public struct InputA
| | {
| | public fixed double array1[3];
| | public double dt;
| | public fixed double array2[3];
| | }
| | ....
| | }
| |
| | The unmanaged DLL is compiled as part of my Solution so there should
be
| | a bunch of metadata laying around. I now know how to read a value from
| | a field in the structure thru reflection - more or less like this:
|
| That's not really an unmanaged structure, because you have metadata to
| manage it. Maybe better to call it "explicit layout". The only kind of
| "metadata" you can get for an unmanaged structure is the .pdb debug
| database.
|
| |
| | Inputs.InputA inp = new Inputs.InputA() ;
| | ...
| | Type t = inp.GetType();
| | MemberInfo[] m = t.GetMembers();
| |
| | // Find the index in m[] where the Field "dt" is located (not a
| | problem) - "9" in this example
| |
| | Object dt = t.InvokeMember( m[9].Name, BindingFlags.Ge tField,
| | null, inp, null);
| |
| | Wonderfully, dt contains the value from the inp structure. Now the
| | problem... I want to set the value of "dt" in the inp structure to
| | something else. I try the following:
| |
| | Object[] newDT= new object[] { 2.777 };
| | t.InvokeMember( myMemberInfo[9].Name,
| | BindingFlags.De claredOnly | BindingFlags.Pu blic |
| | BindingFlags.No nPublic |
| | BindingFlags.In stance | BindingFlags.St atic |
| | BindingFlags.Se tField, null, inp, newDT);
| |
| | The value of "dt" in the inp structure does NOT change, and there is
no
| | error message. Any suggestions greatfully received. Yes, I know that I
| | can access the structure directly. However, I REALLY DO have a good
| | reason to want to use reflection to do this.
| |
| | TIA,
| |
| | Bill
| |
| >
| InvokeMember( , BindingFlags.Se tField,..)
| involves a boxing operation, you have to box the value type and pass the
| boxed value to InvokeMember's target and unbox after the call.
| Quite expensive, you must have very good reasons to do this.
|
| More to the point, InvokeMember operates on the boxed copy, not the real
| value.

Hmmm... where exactly did I say that it operates on the real value?
InvokeMember 'involves a boxing operation', that is, it implicitly boxes the
value type passed in as an argument, but after the call the "boxed" instance
vanishes waiting to be collected (it's no longer referenced) , so you must
box before calling an pass the boxed value to InvokeMember and unbox when
done.

like this:

object temp = new Inputs() as object;
typeof(Inputs). InvokeMember("i ", BindingFlags.Se tField , null, temp,
new object[] {1.23});
Inputs inp = (Inputs) so;

| You need to keep a reference to the boxed copy:
|
| object boxinp = inp;
| InvokeMember(.. .inp...);
|

small correction..... you need to pass the 'boxed' reference boxinp.

| Now boxinp should be changed. You can set all the variables you want,
then
| at the end,
| inp = (InputA)boxinp;
|
| >
| Willy.
| >
| >

Willy.
Nov 8 '06 #5
| InvokeMember( , BindingFlags.Se tField,..)
| involves a boxing operation, you have to box the value type and pass
the
| boxed value to InvokeMember's target and unbox after the call.
| Quite expensive, you must have very good reasons to do this.
|
| More to the point, InvokeMember operates on the boxed copy, not the real
| value.

Hmmm... where exactly did I say that it operates on the real value?
You didn't... now I see you were suggesting exactly the example code I
provided, but the first time I read through it, I thought you were
describing the compiler creating a temporary boxing variable.

I also think you were confusing the issue with "quite expensive". Boxing
and unboxing are more expensive than direct access, but so much faster than
the reflection invoke as to be lost in the noise.

I wasn't contradicting you, I was trying to provide a succinct summary with
example code.
Nov 8 '06 #6

"Ben Voigt" <rb*@nospam.nos pamwrote in message
news:ey******** ********@TK2MSF TNGP04.phx.gbl. ..
|
| | InvokeMember( , BindingFlags.Se tField,..)
| | involves a boxing operation, you have to box the value type and pass
| the
| | boxed value to InvokeMember's target and unbox after the call.
| | Quite expensive, you must have very good reasons to do this.
| |
| | More to the point, InvokeMember operates on the boxed copy, not the
real
| | value.
| >
| Hmmm... where exactly did I say that it operates on the real value?
| You didn't... now I see you were suggesting exactly the example code I
| provided, but the first time I read through it, I thought you were
| describing the compiler creating a temporary boxing variable.
|
| I also think you were confusing the issue with "quite expensive". Boxing
| and unboxing are more expensive than direct access, but so much faster
than
| the reflection invoke as to be lost in the noise.
|
| I wasn't contradicting you, I was trying to provide a succinct summary
with
| example code.
|
|

Ben, Notice that the OP is using fixed buffers in it's structure, that means
that the data is in-lined, and moving such structure to and from the heap
can get quite costly (depending on the size of the buffers) this combined
with interop marshaling (which I guess the OP is using the struct for) may
have some negative inpact on performance.

Willy.
Nov 8 '06 #7

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

Similar topics

2
320
by: ichor | last post by:
hi i have read a few examples that use the System.Reflection api , but i cant understand in what way is it helpful. i mean its practical use in the real world thanx
2
291
by: UJ | last post by:
Can somebody point me to an explanation of what Reflection is? I've tried looking in the help and it's not very helpful. TIA - Jeff.
2
3579
by: Dave Griffin | last post by:
This is a followup to my question in doing unmanaged DLLs in .NET. I was able to create the DLL and the application was able to see it. So far so good. Now I need to make that unmanaged DLL read an XML file which contains a scenario I plan to follow within the library. Basically this DLL is an interface to a 1553 network with data being sent and received. By creating an artificial version, I hope to pretend to send and receive a huge...
9
2399
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:
3
4311
by: bill | last post by:
All, I have the following: ...... unsafe public struct Example { public double we;
7
2695
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
17
2315
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...
6
1972
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept quiet and smiled. What is reflection!? I have been reading it up, and all I can work out is that it has something to do with Late Binding, and the use of GetType and Type.
0
9843
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
9682
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
10570
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
10626
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
10273
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
7001
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
5670
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
4474
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
4041
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.