473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 ParameterModifier, 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.GetTypeFromProgID("ADESecurity.CSecurity");

object commonLogon=
Activator.CreateInstance(commonLogonType);

arrayInput = new Object[] {""};
commonLogonType.InvokeMember("GetUserRightsAsXML", BindingFlags.InvokeMethod,
null,commonLogon,arrayInput,mods,null,null);

Response.Write("Returned XML value=" +
Convert.ToString(arrayInput[arrayInput.GetLowerBound(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 GetUserRightsAsXML(ByRef r_szXMLResult As Variant, _
Optional ByVal p_szXMLRootName As Variant
= "adesecuritypermissions", _
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 14403
Robert May <ra******@dontspamhotmail.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 ParameterModifier, 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.Reflection;

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

static void Main()
{
Type t = typeof(Test);
MethodInfo method = typeof(Test).GetMethod("GiveHello");
object[] p = new object[]{null};
method.Invoke(null, p);
t.InvokeMember("GiveHello",
BindingFlags.InvokeMethod|
BindingFlags.Static|
BindingFlags.Public,
null, null, p);
Console.WriteLine (p[0]);
}
}

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

--
Jon Skeet - <sk***@pobox.com>
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:

ParameterModifier 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 ParameterModifier.
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[] {""};

ParameterModifier[] mods=new ParameterModifier[1]{new ParameterModifier(1)};

mods[0][0] = true;

commonLogonType.InvokeMember("GetUserRightsAsXML", BindingFlags.InvokeMethod,
null,commonLogon,arrayInput,mods,null,null);

Thanks for your help.
Robert

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Robert May <ra******@dontspamhotmail.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 ParameterModifier, 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.Reflection;

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

static void Main()
{
Type t = typeof(Test);
MethodInfo method = typeof(Test).GetMethod("GiveHello");
object[] p = new object[]{null};
method.Invoke(null, p);
t.InvokeMember("GiveHello",
BindingFlags.InvokeMethod|
BindingFlags.Static|
BindingFlags.Public,
null, null, p);
Console.WriteLine (p[0]);
}
}

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

--
Jon Skeet - <sk***@pobox.com>
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
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...
1
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...
0
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...
12
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...
0
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...
1
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...
2
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...
6
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.