Import C dll methods to C# app

G

Guest

Hi this is my first post on this newsgroup!! Excited.

My question has to do with importing a dll that was written in C into c#. I
have been able to import it using DLLImport but the issue is the following.
The function call returns the result by reference, but c# does not allow that
so how can I get my result from the function call? here is an example of a
call from the dll.

myAddMethod(double a, double b, double answer)

the result is returned as the variable answer.

How can I get the value of answer in my app?

Thanks.
 
W

Willy Denoyette [MVP]

kapsolas said:
Hi this is my first post on this newsgroup!! Excited.

My question has to do with importing a dll that was written in C into c#.
I
have been able to import it using DLLImport but the issue is the
following.
The function call returns the result by reference, but c# does not allow
that
so how can I get my result from the function call? here is an example of a
call from the dll.

myAddMethod(double a, double b, double answer)

the result is returned as the variable answer.

How can I get the value of answer in my app?

Thanks.

.... ,ref double answer)

Willy.
 
G

Guest

The type double method signature parameter "answer" should hold the result
after the method call is made. You may need to prepend the ref keyword before
the parameter. All you need to do is make the method call and then use the
value in "answer".

Peter
 
G

Guest

That didnt do it for me.

When I import this dll into a vb6 project I can do this:
Dim ans as Integer
Dim a as Integer
Dim b as Integer

myAddMethod(a, b, ans)

MsgBox("Your answer is: " + ans)

and this works fine.

when trying this in a c# ans has no value. here is some sample code

int a;
int b;
int answer;


myAddMethod(a, b, ref answer);

MessageBox.Show("Your answer is: " + answer.toString());


answer is always equal to ""

txtANS.Text = answer;
 
W

Willy Denoyette [MVP]

What type of DLL is it , you said "when I import into a VB6" project, is
this a COM library.

Willy.
 
W

Willy Denoyette [MVP]

If the third arg. of the COM method is DOUBLE*, then you need to call it
like this:

out double answer;
myAddMethod(double a, double b, out double answer);

Willy.
 
G

Guest

I had tried the ref and out keywords before posting on the discussion board.
Those both did not work.

I'll keep hunting around. maybe I can get the dll source and modifyit to
have the function return the resulting value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top