473,386 Members | 1,720 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,386 software developers and data experts.

Using out parameter in Dll method calls from C# appl. to C# dll

Hello,
I have an application and dll both written in C#. The application uses methods and properties in the dll which works
fine so far, but I wasn't able use out parameters in methods call. I use the following structure to call the methods:

Assembly asmblyDll = Assembly.LoadFrom("theDll.dll");
Type tDll = asmblyDll.GetType("theDll.theDll");
object objDll = asmblyDll.CreateInstance("theDll.theDll");

string strToGet1 = string.Empty;
string strToGet2 = string.Empty;

ParameterModifier[] mods = new ParameterModifier[2] { new ParameterModifier(1), new ParameterModifier(1) };
mods[0][0] = true;
mods[1][0] = true;

object[] args = new object[2];
args[0] = strToGet1;
args[1] = strToGet2;

bool bRet = (bool)tDll.InvokeMember("theMethod", BindingFlags.InvokeMethod, null, objDll, args, mods, null, null);
theMethod is:
public bool theMethod(out string str1, out string str2)
{
str1 = "bla";
str2 = "blubb";
}

The ParameterModifier stuff was posted somewhere to use the parameters as out/ref parameters. But it didn't worked out.
Using ref in "theMethod" instead of out didn't work, too. The return value is correct and when the strToGet strings are
set to some strings then these are present in "theMethod" but changes to str1 and str2 didn't affect strToGet1 and
strToGet2.

Gunnar
May 10 '06 #1
6 6293
"Gunnar Frenzel" <Gu************@web.de> wrote in message
news:ur**************@TK2MSFTNGP02.phx.gbl...
Hello,
I have an application and dll both written in C#. The application uses
methods and properties in the dll which works fine so far, but I wasn't
able use out parameters in methods call. I use the following structure to
call the methods:

Assembly asmblyDll = Assembly.LoadFrom("theDll.dll");
Type tDll = asmblyDll.GetType("theDll.theDll");
object objDll = asmblyDll.CreateInstance("theDll.theDll");

string strToGet1 = string.Empty;
string strToGet2 = string.Empty;

ParameterModifier[] mods = new ParameterModifier[2] { new
ParameterModifier(1), new ParameterModifier(1) };
mods[0][0] = true;
mods[1][0] = true;

object[] args = new object[2];
args[0] = strToGet1;
args[1] = strToGet2;

bool bRet = (bool)tDll.InvokeMember("theMethod",
BindingFlags.InvokeMethod, null, objDll, args, mods, null, null);
theMethod is:
public bool theMethod(out string str1, out string str2)
{
str1 = "bla";
str2 = "blubb";
}

The ParameterModifier stuff was posted somewhere to use the parameters as
out/ref parameters. But it didn't worked out. Using ref in "theMethod"
instead of out didn't work, too. The return value is correct and when the
strToGet strings are set to some strings then these are present in
"theMethod" but changes to str1 and str2 didn't affect strToGet1 and
strToGet2.


I haven't tried it, but from my reading of the documentation the code for
setting up the "mods" variable should be:

ParameterModifier p = new ParameterModifier(2);
p[0] = true;
p[1] = true;
ParameterModifier[] mods = { p };

Chris Jobson
May 10 '06 #2
Yes, the one I posted was the last structure I used. I tried the way you wrote it, too, but this doesn't work, too. It
behave just the same way like the version I posted in my previous message.
Gunnar

Chris Jobson wrote:
I haven't tried it, but from my reading of the documentation the code for
setting up the "mods" variable should be:

ParameterModifier p = new ParameterModifier(2);
p[0] = true;
p[1] = true;
ParameterModifier[] mods = { p };

Chris Jobson

May 11 '06 #3
"Gunnar Frenzel" <Gu************@web.de> wrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
Yes, the one I posted was the last structure I used. I tried the way you
wrote it, too, but this doesn't work, too. It behave just the same way
like the version I posted in my previous message.


The problem seems to be that on return from the InvokeMember the Out/Ref
parameters have been updated, but ONLY in the args array. If you want the
original variables changed then you have to copy back the values from the
args array. The following code works fine for me (where my method
ClassLibrary1.Class1.Test takes two "out string" parameters):

string s1 = String.Empty;
string s2 = String.Empty;

Assembly asmblyDll = Assembly.LoadFrom("ClassLibrary1.dll");
Type tDll = asmblyDll.GetType("ClassLibrary1.Class1");
object objDll = asmblyDll.CreateInstance("ClassLibrary1.Class1");

ParameterModifier p = new ParameterModifier(2);
p[0] = true;
p[1] = true;
ParameterModifier[] mods = { p };

object[] args = new object[2];
args[0] = s1;
args[1] = s2;

bool bRet = (bool)tDll.InvokeMember("Test", BindingFlags.InvokeMethod, null,
objDll, args, mods, null, null);

// Must copy Out/Ref parameters back!
s1 = (string)args[0];
s2 = (string)args[1];

if (bRet) MessageBox.Show(s1 + " " + s2);

Chris Jobson

May 11 '06 #4
Oh yes, this solved the problem. This works even if no ParameterModifiers are passed.
I tried the overload without ParameterModifier array
bool bRet = (bool)tDll.InvokeMember("theMethod", BindingFlags.InvokeMethod, null, objDll, args);
and passed the values from the args array back to the strings and this worked fine as well.
Thanks a lot.
Gunnar

Chris Jobson wrote:
The problem seems to be that on return from the InvokeMember the Out/Ref
parameters have been updated, but ONLY in the args array. If you want the
original variables changed then you have to copy back the values from the
args array. The following code works fine for me (where my method
ClassLibrary1.Class1.Test takes two "out string" parameters):

string s1 = String.Empty;
string s2 = String.Empty;

Assembly asmblyDll = Assembly.LoadFrom("ClassLibrary1.dll");
Type tDll = asmblyDll.GetType("ClassLibrary1.Class1");
object objDll = asmblyDll.CreateInstance("ClassLibrary1.Class1");

ParameterModifier p = new ParameterModifier(2);
p[0] = true;
p[1] = true;
ParameterModifier[] mods = { p };

object[] args = new object[2];
args[0] = s1;
args[1] = s2;

bool bRet = (bool)tDll.InvokeMember("Test", BindingFlags.InvokeMethod, null,
objDll, args, mods, null, null);

// Must copy Out/Ref parameters back!
s1 = (string)args[0];
s2 = (string)args[1];

if (bRet) MessageBox.Show(s1 + " " + s2);

Chris Jobson

May 11 '06 #5
"Gunnar Frenzel" <Gu************@web.de> wrote in message
news:uE**************@TK2MSFTNGP04.phx.gbl...
Oh yes, this solved the problem. This works even if no ParameterModifiers
are passed.
I tried the overload without ParameterModifier array
bool bRet = (bool)tDll.InvokeMember("theMethod",
BindingFlags.InvokeMethod, null, objDll, args);
and passed the values from the args array back to the strings and this
worked fine as well.


I just noticed that in the documentation for Type.InvokeMember
(http://msdn2.microsoft.com/en-US/library/de3dhzwy.aspx) there is a comment
for the mofifiers parameter: "The default binder does not process this
parameter"! This explains what you've just found (it appears that
ParameterModifier is only used when calling a COM method). Anyway, glad it's
working now.

Chris
May 11 '06 #6
"Gunnar Frenzel" <Gu************@web.de> wrote in message
news:uE**************@TK2MSFTNGP04.phx.gbl...
Oh yes, this solved the problem. This works even if no ParameterModifiers
are passed.
I tried the overload without ParameterModifier array
bool bRet = (bool)tDll.InvokeMember("theMethod",
BindingFlags.InvokeMethod, null, objDll, args);
and passed the values from the args array back to the strings and this
worked fine as well.


I just noticed that in the documentation for Type.InvokeMember
(http://msdn2.microsoft.com/en-US/library/de3dhzwy.aspx) there is a comment
for the mofifiers parameter: "The default binder does not process this
parameter"! This explains what you've just found (it appears that
ParameterModifier is only used when calling a COM method). Anyway, glad it's
working now.

Chris
May 11 '06 #7

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

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
8
by: Pola | last post by:
Please Help I am using VC++ in win 2000 In my appl (Win32 project) I want to control the close operation of the apl (for example if somebody will try to close appl from the "Windows Task Manager")...
10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
2
by: Einar Høst | last post by:
Hi, I'm reading data from a tape device using p/invoke. It's working pretty well, but when I'm trying to get data about the tape device, I'm doing something wrong. I believe it has something to...
4
by: David Douglass | last post by:
I'm confused about the program below. Based on my reading of the C# spec, I don't think it should compile, but it does when using Beta 1. Could somebody please explain the function selection...
3
by: Pratcp | last post by:
Hello, I have an asp.net Web app in vb.net trying to call a C# web service which takes a reference parameter. I tried a simple C# web app to call the Web service and it works perfectly. However,...
2
by: rain_c1 | last post by:
hi, i think this is a little exercise for real experts, but i suffer from real headaches because of it... :-\ i have an object method (method1), that calls setTimeout with an other method...
0
by: bughunter | last post by:
I found code from java - real bug with secondary parameter, should be Integer but called with String, But procedure completed without any errors and parameter correctly transformed to integer! ...
16
by: Joe Strout | last post by:
One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method -- i.e. storage that is persistent between calls, but not visible outside the method. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.