473,749 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection, InvokeMember, and out parameters

We have a VB6 COM component that I don't have control over. All of the
values are returned from method calls using byref parameters. They
frequently break binary compatibility, in fact, there are three different
versions of the component, one for dev, one for test, and one for
production, all of which are broken with each re-compile, which means I
can't use early binding and COM interop. Ugh.

Using InvokeMember, how do I get values from an out parameter? I saw the
documentation about ParameterModifi er, but I'm afraid that I just don't get
it. :) A code sample would be appreciated.

Here's the code I have so far (the Response.Write value returns an empty
string as the return value, which isn't valid):

Type commonLogonType =
Type.GetTypeFro mProgID("ADESec urity.CSecurity ");

object commonLogon=
Activator.Creat eInstance(commo nLogonType);

arrayInput = new Object[] {""};
commonLogonType .InvokeMember(" GetUserRightsAs XML",BindingFla gs.InvokeMethod ,
null,commonLogo n,arrayInput,mo ds,null,null);

Response.Write( "Returned XML value=" +
Convert.ToStrin g(arrayInput[arrayInput.GetL owerBound(0)]));

Response.End();

}

Here's the VB method call (the return value is a 0 or non-zero value
indicating success or failure of the call, r_szXMLResult is the actual value
that is returned):

Public Function GetUserRightsAs XML(ByRef r_szXMLResult As Variant, _
Optional ByVal p_szXMLRootName As Variant
= "adesecurityper missions", _
Optional ByVal p_lEntityID As Variant =
0, _
Optional ByVal p_bDebugDisplay As Boolean
= False _
) As Variant

Any help would be greatly appreciated.

Robert

Jul 21 '05 #1
2 14448
Robert May <ra******@donts pamhotmail.com> wrote:
We have a VB6 COM component that I don't have control over. All of the
values are returned from method calls using byref parameters. They
frequently break binary compatibility, in fact, there are three different
versions of the component, one for dev, one for test, and one for
production, all of which are broken with each re-compile, which means I
can't use early binding and COM interop. Ugh.

Using InvokeMember, how do I get values from an out parameter? I saw the
documentation about ParameterModifi er, but I'm afraid that I just don't get
it. :) A code sample would be appreciated.


I don't whether COM is screwing you up or not, but normally you just
look at the value of the parameter array afterwards. Here's an example:

using System;
using System.Reflecti on;

public class Test
{
public static void GiveHello (out string x)
{
x = "hello";
}

static void Main()
{
Type t = typeof(Test);
MethodInfo method = typeof(Test).Ge tMethod("GiveHe llo");
object[] p = new object[]{null};
method.Invoke(n ull, p);
t.InvokeMember( "GiveHello" ,
BindingFlags.In vokeMethod|
BindingFlags.St atic|
BindingFlags.Pu blic,
null, null, p);
Console.WriteLi ne (p[0]);
}
}

I'm not sure why you're using arrayInput.GetL owerBound(0) in your code
rather than just 0, but it certainly looks like it should be working...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
It must be something to do with COM. For example, the documentation on
invoke memeber says the following:

ParameterModifi er is only used when calling through COM interop, and only
parameters that are passed by reference are handled.

So I need to be able to send in a parameter modifier that tells COM that
this is an out parameter, but I don't know how to use the ParameterModifi er.
All it has is an integer that tells how many parameters to modify.

Ah hah! I just figured it out, and it's not terribly intuitive. The help
states in several places that you can set the parameter modifier to pdout,
pdin, pdlcid, etc . . . However, this is in reference to what the binder
does with COM code, not to the parameter modifier itself. It can only be
set to true or false, which I'm guessing simply tells the binder that it
should check to see if a variable is passed by reference. What fun.

As to the GetLowerBounds call, I'm a vb hack, and can never keep my bounds
straight . . . :). It's been changed. Here's the invoke member code that
actually works (starting with the definition of the arguments):

arrayInput = new Object[] {""};

ParameterModifi er[] mods=new ParameterModifi er[1]{new ParameterModifi er(1)};

mods[0][0] = true;

commonLogonType .InvokeMember(" GetUserRightsAs XML",BindingFla gs.InvokeMethod ,
null,commonLogo n,arrayInput,mo ds,null,null);

Thanks for your help.
Robert

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Robert May <ra******@donts pamhotmail.com> wrote:
We have a VB6 COM component that I don't have control over. All of the
values are returned from method calls using byref parameters. They
frequently break binary compatibility, in fact, there are three different versions of the component, one for dev, one for test, and one for
production, all of which are broken with each re-compile, which means I
can't use early binding and COM interop. Ugh.

Using InvokeMember, how do I get values from an out parameter? I saw the documentation about ParameterModifi er, but I'm afraid that I just don't get it. :) A code sample would be appreciated.


I don't whether COM is screwing you up or not, but normally you just
look at the value of the parameter array afterwards. Here's an example:

using System;
using System.Reflecti on;

public class Test
{
public static void GiveHello (out string x)
{
x = "hello";
}

static void Main()
{
Type t = typeof(Test);
MethodInfo method = typeof(Test).Ge tMethod("GiveHe llo");
object[] p = new object[]{null};
method.Invoke(n ull, p);
t.InvokeMember( "GiveHello" ,
BindingFlags.In vokeMethod|
BindingFlags.St atic|
BindingFlags.Pu blic,
null, null, p);
Console.WriteLi ne (p[0]);
}
}

I'm not sure why you're using arrayInput.GetL owerBound(0) in your code
rather than just 0, but it certainly looks like it should be working...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

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

Similar topics

1
2625
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to Activator.CreateInstance() it always fails. However if I walk my assembly and get a type value, the call to...
1
5172
by: Robert May | last post by:
We have a VB6 COM component that I don't have control over. All of the values are returned from method calls using byref parameters. They frequently break binary compatibility, in fact, there are three different versions of the component, one for dev, one for test, and one for production, all of which are broken with each re-compile, which means I can't use early binding and COM interop. Ugh. Using InvokeMember, how do I get values...
0
3086
by: Alex Zhitlenok | last post by:
Hi, On Google, one can find a lot of statements that Invoke and InvokeMemeber work alike. However, my own experience contradicts this statement. 1. Surprisingly, Invoke and InvokeMember methods work differently! I'm working with some legacy COM. The COM object (CMyCOM) implements an interface that is not dual. The interface has a method, let say, HRESULT Foo(); The corresponded coclass defines an interface
12
6995
by: Gus | last post by:
Hi, I´m triying to invoke a member using reflection, but this member requiere two parameters, the first patrameter is a string and the second is a reference to a dataset (output parameter). The parameters has to be passed in an array of objects. I´ve tried something like this: object parameters = new object; parameters ="text";
0
1311
by: Pat Ireland | last post by:
I continue to investigate issues with making explicit InvokeMember calls (necessary when dynamically loading classes). My quest lets me use a simple class to invoke another class's member method by normal means and by using the InvokeMember. By examining the underlying il code with ildasm, when attempting to pass by reference, the il code generates a box command so that a pointer to the actual data is passed. This is clearly seen with...
1
1170
by: rob thomson | last post by:
I need to call different functions on classes eg MyFunction(param1 as boolean, param2 as string) as datatable MyFunction2(param1 as date, param2 as int64) as datatable using reflection, I can load the object by name but dont know how to call the function using the parameters. Can anyone help me with this TIA
2
463
by: Robert May | last post by:
We have a VB6 COM component that I don't have control over. All of the values are returned from method calls using byref parameters. They frequently break binary compatibility, in fact, there are three different versions of the component, one for dev, one for test, and one for production, all of which are broken with each re-compile, which means I can't use early binding and COM interop. Ugh. Using InvokeMember, how do I get values...
6
1854
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 { .....
0
1700
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
8997
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
9568
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
9389
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...
0
9256
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
8257
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
6801
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.