473,473 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert short * to object for use with InvokeMember



I am dynamically loading a class. One of the methods of the class takes
a short * (by reference) argument. However, the InvokeMember call used
to invoke the method of the class passings arguments in and object [].
Since this is a pointer reference being passed it must be invoked in a
unsafe {} even thought the dynamically loaded class is a .Net entity.
The following is the test code that I am using to try and load a short *
into an object:
using System;
using System.Reflection;
namespace TestBoxing
{
class TestBoxing
{
[STAThread]
static void Main(string[] args)
{
Object MyObj;
Int16 MyVar = 1;
unsafe
{
System.Int16 * pVar = &MyVar;
MyObj = Pointer.Box(pVar,typeof(System.Int16 *));
Console.WriteLine("Type {0}",MyObj.GetType());
} // unsafe
} // static void Main ()
} // class TestBoxing
} // namespace TestBoxing
A more complete tester for reflection and trying to use InvokeMember is
as follows:
using System;
using System.Reflection;
namespace TestByReference
{
class MyClass
{
public void MyMethod (ref short MyParm)
{
MyParm = 6;
} // public void MyMethod ()
} // class MyClass
class TestByReference
{
[STAThread]
static void Main(string[] args)
{
try
{
// Normal invocation
MyClass MyObj = new MyClass();
short MyArg = 5;
Console.WriteLine("MyArg {0}",MyArg);
MyObj.MyMethod(ref MyArg);
Console.WriteLine("MyArg {0}",MyArg);
// InvokeMember
Type MyType = MyObj.GetType();
Console.WriteLine("MT {0}",MyType.FullName);
MethodInfo [] MyInfos = MyType.GetMethods();
Console.WriteLine("# MIs {0}",MyInfos.Length);
foreach(MethodInfo mInfo in MyInfos)
Console.WriteLine("Method {0}",mInfo.Name);
Console.WriteLine("MI[3] {0}",MyInfos[3].Name);
ParameterInfo [] MyParms;
MyParms = MyInfos[3].GetParameters();
Console.WriteLine("# MPs {0}",MyParms.Length);
Console.WriteLine("Type of Parmeter {0}",
MyParms[0].ParameterType);
object [] MyCallingArgs = new object [1];
unsafe
{
MyCallingArgs[0] = Pointer.Box(&MyArg,typeof(System.Int16 *));
Console.WriteLine("MyCallingArs[0] type
{0}",MyCallingArgs[0].GetType());
MyType.InvokeMember("MyMethod",
BindingFlags.InvokeMethod |
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance,
null,
MyObj,
MyCallingArgs);
} // unsafe
Console.WriteLine("MyArg {0}",MyArg);
} // try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} // catch (Exception ex)
} // static void Main ()
} // class TestByReference
} // namespace TestByReference

As a finally comment on my question - if you examine the underlying il
code the C# invocation does a boxing of the short *, but attempting to
use the InvokeMember can not construct the required argument list.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
3 2308
No need to resort to unsafe constructs, use the ParameterModifier and pass
the value byref like this:

MyCallingArgs[0] = MyArg;
ParameterModifier p = new ParameterModifier(1);
p[0] = true; // pass arg0 by ref
ParameterModifier[] mods = { p };

Console.WriteLine("MyCallingArs[0]
type{0}",MyCallingArgs[0].GetType());
MyType.InvokeMember("MyMethod",
BindingFlags.InvokeMethod |
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance,
null,
MyObj,
MyCallingArgs,
mods, null, null);

Willy.

"Patrick Ireland" <ir*******@earthlink.net> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...


I am dynamically loading a class. One of the methods of the class takes
a short * (by reference) argument. However, the InvokeMember call used
to invoke the method of the class passings arguments in and object [].
Since this is a pointer reference being passed it must be invoked in a
unsafe {} even thought the dynamically loaded class is a .Net entity.
The following is the test code that I am using to try and load a short *
into an object:
using System;
using System.Reflection;
namespace TestBoxing
{
class TestBoxing
{
[STAThread]
static void Main(string[] args)
{
Object MyObj;
Int16 MyVar = 1;
unsafe
{
System.Int16 * pVar = &MyVar;
MyObj = Pointer.Box(pVar,typeof(System.Int16 *));
Console.WriteLine("Type {0}",MyObj.GetType());
} // unsafe
} // static void Main ()
} // class TestBoxing
} // namespace TestBoxing
A more complete tester for reflection and trying to use InvokeMember is
as follows:
using System;
using System.Reflection;
namespace TestByReference
{
class MyClass
{
public void MyMethod (ref short MyParm)
{
MyParm = 6;
} // public void MyMethod ()
} // class MyClass
class TestByReference
{
[STAThread]
static void Main(string[] args)
{
try
{
// Normal invocation
MyClass MyObj = new MyClass();
short MyArg = 5;
Console.WriteLine("MyArg {0}",MyArg);
MyObj.MyMethod(ref MyArg);
Console.WriteLine("MyArg {0}",MyArg);
// InvokeMember
Type MyType = MyObj.GetType();
Console.WriteLine("MT {0}",MyType.FullName);
MethodInfo [] MyInfos = MyType.GetMethods();
Console.WriteLine("# MIs {0}",MyInfos.Length);
foreach(MethodInfo mInfo in MyInfos)
Console.WriteLine("Method {0}",mInfo.Name);
Console.WriteLine("MI[3] {0}",MyInfos[3].Name);
ParameterInfo [] MyParms;
MyParms = MyInfos[3].GetParameters();
Console.WriteLine("# MPs {0}",MyParms.Length);
Console.WriteLine("Type of Parmeter {0}",
MyParms[0].ParameterType);
object [] MyCallingArgs = new object [1];
unsafe
{
MyCallingArgs[0] = Pointer.Box(&MyArg,typeof(System.Int16 *));
Console.WriteLine("MyCallingArs[0] type
{0}",MyCallingArgs[0].GetType());
MyType.InvokeMember("MyMethod",
BindingFlags.InvokeMethod |
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance,
null,
MyObj,
MyCallingArgs);
} // unsafe
Console.WriteLine("MyArg {0}",MyArg);
} // try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} // catch (Exception ex)
} // static void Main ()
} // class TestByReference
} // namespace TestByReference

As a finally comment on my question - if you examine the underlying il
code the C# invocation does a boxing of the short *, but attempting to
use the InvokeMember can not construct the required argument list.

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2

Willy, Thank you for replying. However, I have tried that very solution
also. If you print out the value of MyArg after the InvokeMember call
using the ParameterModifier method, the value is the same as passed to
the function. This indicates that a byref must be place in the
MyCallingArgs[0] - or at least it indicates that to me. Unfortunately,
there is no connection made between the object[] argument list and the
ParameterModifier list until the actual invoke. Doing the assignmen of
the MyCallingArgs[0]=MyArg; just takes the value contained in MyArg (6
at the point), makes it an object and puts in the the 0th element of the
calling parameters. The assignment of a reference to MyArg needs to be
made when MyCallingArgs[0] is loaded. I have tried to use unsafe and
fixed to try and force a short * in the calling parameters but have not
been able to so far. By examining the underlying il code, I can tell
the the C# compiler boxes the reference when the invoked method is in a
class that has not been dynamically loaded (the dead give away is the
box instruction). I have not been able to generate equivalent code when
the class is dynamically loaded.

Do you have any more suggestions?

I have posted this very same question on the Microsoft C# user's group
but no one ever responded. I thank you for your efforts but I am at a
loss as to what to try next.

Pat, MCSD
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Willy,

Correction to my last response. I turned on the include source listing
in ildasm and quickly discovered the box command is being used with the
Console.WriteLine command. However, examining more carefully this time,
it is clear that the C# compiler is loading the address of the short
prior to invoking the method. It uses the ldloca.s which is a load the
address of a short command. Which brings me back the how do I do that
using the invokemember or invokemethod reflection call? The problem is
that C# is not allowing me to cast a short * to an object, so I can load
it into the args list which is a object []

Pat, MCSD, plus lots of other letters

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4

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

Similar topics

0
by: David | last post by:
Hi, I have a program in which I used invokemember to late bind to a component. In my code I use: SetupInfo setup; Object objTem = callLateBinding ("BackOfficeServer.BackOffice",...
3
by: Dent2 | last post by:
I wrote a nice C# excel routine to automate the formatting of some raw CSV data. I wrote the routine on a WinXP computer with Visual Studio .NET 2003 and Office XP installed. It compiled and ran...
12
by: Mark | last post by:
I have a C# add in that inserts a custom active x control into a worksheet. Once the object is inserted i can locate the object through both Shapes and Worksheet.OLEObjects. What I can't do is...
9
by: Kurt | last post by:
Hi I was trying to get this VB type code to work in C Sub SetColumns_Example( Dim ol As Outlook.Applicatio Dim MyFolder As MAPIFolde Dim itms As Item Dim itm As Objec Dim dtmStart As Date,...
0
by: Pat Ireland | last post by:
To use the InvokeMember for calling a method in a dynamically loaded class requires that any arguments to the method must be past in an object . However, when I try to perform a pass by reference...
1
by: Robin Dindayal | last post by:
Does anyone know how I can print a fully rendered .aspx to the server's printer? I know that, if I wanted to print to the client's printer it would be easy (ie. use javascript's window.print()). ...
7
by: Mike Howard | last post by:
I'm trying to convert the following (simplified) VB.Net code to C#, that makes use of some externally developed COM code written in VB6. VB.Net Code Sub Main() Dim oAPP As Object Dim oApp2...
4
by: adiel_g | last post by:
I am trying to set a value on a structure object dynamically using InvokeMember. After I call InvokeMember, the value on the Admin flag does not get updated. Here is the following sample code to...
0
by: =?Utf-8?B?RmFicml6aW8gQ2lwcmlhbmk=?= | last post by:
I need to access classic ASP intrinsic objects and their properties from a ..net assembly wrapped to COM. The COM .net assembly is then instanciated from a classic ASP page with...
0
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,...
0
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...
0
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...
0
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.