InvokeMember

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

boolResult =
(bool)typeofobj.InvokeMember("ReadString",BindingFlags.InvokeMethod, null,
newobj, new object[]{HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult});


This returns True, which means the method was executed fine, but strResult
is empty, it's suppose to be what it read. Any ideas?
 
Chris said:
boolResult =
(bool)typeofobj.InvokeMember("ReadString",BindingFlags.InvokeMethod, null,
newobj, new object[]{HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult});


This returns True, which means the method was executed fine, but strResult
is empty, it's suppose to be what it read. Any ideas?

What are you invoking? What's the signature? My guess is that the last
parameter is passed by reference. strResult won't have changed, but the
contents of the array will have done. Unfortunately you're then
throwing the array away, effectively.

Try doing this instead:

object[] parameters = new object[]
{HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};

boolResult = (bool) typeofobj.InvokeMember ("ReadString",
BindingFlags.InvokeMethod, null, newobj, parameters);

strResult = (string) parameters[3];
 
That still returns null, strResult is suppose to be set by the object. I do
know that it's executing right etc, because boolResult is True.

E.g. in Delphi:

boolResult := AESreg.ReadString(HKEY_CURRENT_USER, 'Software\Example Reg
Path', 'Example String Value', strResult);

ShowMessage('Read result = ' + strResult)

Any ideas?

Jon Skeet said:
Chris said:
boolResult =
(bool)typeofobj.InvokeMember("ReadString",BindingFlags.InvokeMethod, null,
newobj, new object[]{HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult});


This returns True, which means the method was executed fine, but strResult
is empty, it's suppose to be what it read. Any ideas?

What are you invoking? What's the signature? My guess is that the last
parameter is passed by reference. strResult won't have changed, but the
contents of the array will have done. Unfortunately you're then
throwing the array away, effectively.

Try doing this instead:

object[] parameters = new object[]
{HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};

boolResult = (bool) typeofobj.InvokeMember ("ReadString",
BindingFlags.InvokeMethod, null, newobj, parameters);

strResult = (string) parameters[3];
 
Chris said:
That still returns null, strResult is suppose to be set by the object.

It can't be. strResult's value can't be changed by the code you posted,
as strings are immutable and the
I do know that it's executing right etc, because boolResult is True.

E.g. in Delphi:

boolResult := AESreg.ReadString(HKEY_CURRENT_USER, 'Software\Example Reg
Path', 'Example String Value', strResult);

ShowMessage('Read result = ' + strResult)

Any ideas?

Is this in a COM component? If so, you might need to use
ParameterModifiers. Have a look at this thread for more details:

http://groups.google.com/groups?threadm=MPG.1a6cd368e69f34ec989ddb%
40msnews.microsoft.com

(aka http://tinyurl.com/yrayh)
 
Jon,

When I do something like that:

object[] parameters = {HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};
ParameterModifier mods = new ParameterModifier(parameters.Length);
mods[3] = true;
object retVal = typeofobj.GetType().InvokeMember ("readstring",
BindingFlags.InvokeMethod, null, newobj, parameters, new
ParameterModifier[]{ mods }, null, null);

I get the following error:


An unhandled exception of type 'System.Reflection.TargetException' occurred
in mscorlib.dll

Additional information: Object does not match target type.

Not sure why it's this complicated :)


Thanks for all your help
 
How did you declare HKEY_CURRENT_USER?

Willy.

Chris said:
Jon,

When I do something like that:

object[] parameters = {HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};
ParameterModifier mods = new ParameterModifier(parameters.Length);
mods[3] = true;
object retVal = typeofobj.GetType().InvokeMember ("readstring",
BindingFlags.InvokeMethod, null, newobj, parameters, new
ParameterModifier[]{ mods }, null, null);

I get the following error:


An unhandled exception of type 'System.Reflection.TargetException'
occurred
in mscorlib.dll

Additional information: Object does not match target type.

Not sure why it's this complicated :)


Thanks for all your help
 
That has nothing to do with my problem at hand, but...

const uint HKEY_CURRENT_USER = 0x80000001;



Chris

Willy Denoyette said:
How did you declare HKEY_CURRENT_USER?

Willy.

Chris said:
Jon,

When I do something like that:

object[] parameters = {HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};
ParameterModifier mods = new ParameterModifier(parameters.Length);
mods[3] = true;
object retVal = typeofobj.GetType().InvokeMember ("readstring",
BindingFlags.InvokeMethod, null, newobj, parameters, new
ParameterModifier[]{ mods }, null, null);

I get the following error:


An unhandled exception of type 'System.Reflection.TargetException'
occurred
in mscorlib.dll

Additional information: Object does not match target type.

Not sure why it's this complicated :)


Thanks for all your help



Jon Skeet said:
That still returns null, strResult is suppose to be set by the object.

It can't be. strResult's value can't be changed by the code you posted,
as strings are immutable and the

I do know that it's executing right etc, because boolResult is True.

E.g. in Delphi:

boolResult := AESreg.ReadString(HKEY_CURRENT_USER,
'Software\Example
Reg
Path', 'Example String Value', strResult);

ShowMessage('Read result = ' + strResult)

Any ideas?

Is this in a COM component? If so, you might need to use
ParameterModifiers. Have a look at this thread for more details:

http://groups.google.com/groups?threadm=MPG.1a6cd368e69f34ec989ddb%
40msnews.microsoft.com

(aka http://tinyurl.com/yrayh)
 
Btw..

bool boolResult =
(bool)typeofobj.InvokeMember("WriteString",BindingFlags.Public |
BindingFlags.SetField |BindingFlags.InvokeMethod, null, newobj, new
object[]{HKEY_CURRENT_USER, "Software\\Example Reg Path", "Example String
Value", "hello.net" });

That works fine.

Its just the ReadString method, I cant get the read back from the function
into strResult....

Chris
Chris said:
That has nothing to do with my problem at hand, but...

const uint HKEY_CURRENT_USER = 0x80000001;



Chris

Willy Denoyette said:
How did you declare HKEY_CURRENT_USER?

Willy.

Chris said:
Jon,

When I do something like that:

object[] parameters = {HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};
ParameterModifier mods = new ParameterModifier(parameters.Length);
mods[3] = true;
object retVal = typeofobj.GetType().InvokeMember ("readstring",
BindingFlags.InvokeMethod, null, newobj, parameters, new
ParameterModifier[]{ mods }, null, null);

I get the following error:


An unhandled exception of type 'System.Reflection.TargetException'
occurred
in mscorlib.dll

Additional information: Object does not match target type.

Not sure why it's this complicated :)


Thanks for all your help



That still returns null, strResult is suppose to be set by the object.

It can't be. strResult's value can't be changed by the code you posted,
as strings are immutable and the

I do know that it's executing right etc, because boolResult is True.

E.g. in Delphi:

boolResult := AESreg.ReadString(HKEY_CURRENT_USER, 'Software\Example
Reg
Path', 'Example String Value', strResult);

ShowMessage('Read result = ' + strResult)

Any ideas?

Is this in a COM component? If so, you might need to use
ParameterModifiers. Have a look at this thread for more details:

http://groups.google.com/groups?threadm=MPG.1a6cd368e69f34ec989ddb%
40msnews.microsoft.com

(aka http://tinyurl.com/yrayh)
 
Chris said:
That has nothing to do with my problem at hand, but...

const uint HKEY_CURRENT_USER = 0x80000001;

How do you know that has nothing to do with the problem at hand? When
you're getting an "object does not match target type" exception, we
need to know how *everything* is defined.

To ask again, please could you post the signature of the ReadString
method?
 
Signature is:

HiveKey, Registry Path, Key, Returned Value (of the key)

Reason why I said that is because HKEY_CURRENT_USER is used in WriteString
method, and it works fine is all.

If I use:

boolResult =
(bool)typeofobj.InvokeMember("ReadString",BindingFlags.InvokeMethod, null,
newobj, new object[]{HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult})

It returns True, so I know it's reading the registry, just strResult is
null.

When I use the new method :

object[] parameters = {HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};
ParameterModifier mods = new ParameterModifier(parameters.Length);

mods[3] = true;
object retVal = typeofobj.GetType().InvokeMember ("readstring",
BindingFlags.InvokeMethod, null, newobj, parameters, new
ParameterModifier[]{ mods }, null, null);

I get the: object does not match target type.


In Delphi (working example) :

// 3) Read back from the registry
boolResult := AESreg.ReadString(HKEY_CURRENT_USER, 'Software\Example Reg
Path', 'Example String Value', strResult);
// Display result
if boolResult then
ShowMessage('Read result = ' + strResult)
else
ShowMessage('Registry read failed');


Thanks again for all the help guys.
 
Chris said:
boolResult =
(bool)typeofobj.InvokeMember("ReadString",BindingFlags.InvokeMethod,
null, newobj, new object[]{HKEY_CURRENT_USER, "Software\\Example Reg
Path", "Example String Value", strResult});

Why such a convoluted approach to reading registry values when that
functionality is inherent to the framework?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Chris said:
Signature is:

HiveKey, Registry Path, Key, Returned Value (of the key)

No, please give us the *full signature* - with type information.
Reason why I said that is because HKEY_CURRENT_USER is used in WriteString
method, and it works fine is all.

If I use:

boolResult =
(bool)typeofobj.InvokeMember("ReadString",BindingFlags.InvokeMethod, null,
newobj, new object[]{HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult})

It returns True, so I know it's reading the registry, just strResult is
null.

When I use the new method :

object[] parameters = {HKEY_CURRENT_USER, "Software\\Example Reg Path",
"Example String Value", strResult};
ParameterModifier mods = new ParameterModifier(parameters.Length);

mods[3] = true;
object retVal = typeofobj.GetType().InvokeMember ("readstring",
BindingFlags.InvokeMethod, null, newobj, parameters, new
ParameterModifier[]{ mods }, null, null);

I get the: object does not match target type.

I don't *think* that's how you're meant to use ParameterModifier, but I
can't say I've actually used it myself. I would use it by creating 4
instances of ParameterModifier - you've only got one. In particular,
the docs specify that "the args array and the modifiers array have the
same length".
 
Chris said:
Signature is:

HiveKey, Registry Path, Key, Returned Value (of the key)

Reason why I said that is because HKEY_CURRENT_USER is used in
WriteString
method, and it works fine is all.
Ok, that bring's us back to the fourth parameter. It's hard to know for
sure without you posting the signature, but I guess it's a VARIANT*.

Versions 1.0 and 1.1 of the framework don't have the possibility to marshal
Variants byref. If this is the case, your only option is to use the FCL to
read from the registry.

Willy.
 
Back
Top