MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Call c++ function from c# with some difficult parameters

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp
Author Message
capiccc



Joined: 10 Dec 2007
Posts: 2

PostPosted: Mon Dec 10, 2007 12:45 pm    Post subject: Call c++ function from c# with some difficult parameters Reply with quote

Hello together

i just have a little bit tricky question about calling a function out
of a compiled c++ dll (which do some special mathematic thing) from a
c# application. The c# application works as cgi and should do, as
said, some mathematical iterative things (the Levenberg-Marquardt
method which try to reduce the value x^2 of a fit between a set of
data points).

For this, the c++ function in the dll is specified as follows
(declaration by documentation):

mrqmin(float x[], float y[], float sig[], int ndata, float a[], int
ia[], int ma, float **covar, float **alpha, float *chisq, void (*funcs)
(float, float [], float *, float [], int), float *alamda)

(and really declared in source code:
void NR::mrqmin(Vec_I_DP &x, Vec_I_DP &y, Vec_I_DP &sig, Vec_IO_DP &a,
Vec_I_BOOL &ia, Mat_O_DP &covar, Mat_O_DP &alpha, DP &chisq, void
funcs(const DP, Vec_I_DP &, DP &, Vec_O_DP &), DP &alamda))

That can be compiled and should work because the dll is working in
another, non c# application probably.

Now I try to declare the function of the dll through DLLImport in c#
for use with this:

[------------ c# code ---------------]
public class DllImports
{

public delegate double UebergabeFunk(double i);

[DllImport("NR.dll", EntryPoint="?fnNR@@YAHXZ")]
public static extern int fnNr();

[DllImport("NR.dll", EntryPoint="?mrqmin@NR@@YAXABV?
$NRVec@N@@00AAV2@ABV?$NRVec@_N@@AAV?$NRMat@N@@3AANP6AXN041@Z4@Z")]
public unsafe static extern void mrqmin(float[] x, float[] y,
float[] sig, int ndata, float[] a, int[] ia, int ma, float[][]* covar,
float[][]* alpha, float* chisq, UebergabeFunk func, float* alambda);

public DllImports()
{

}

public unsafe void fit(float[] x, float[] y, float[] sig, int ndata,
float[] a, int[] ia, int ma, float[][] covar, float[][] alpha, float
chisq, float alambda)
{
UebergabeFunk Uebergabe = new UebergabeFunk(this.EI);
DllImports.mrqmin(x,y,sig,ndata,a,ia,ma,&covar,&alpha,&chisq,
Uebergabe, &alambda);
}

public double EI(double i)
{
return 0;
}
}
[------------ end c# code ---------------]

This don't compile correctly, mostly of the problem that a have to
give a two dimensional array to the variables covar and alpha (these 2
arrays uses as working space in the dll for some iterative mathematics
and are described in docu with "The array covar[1..ma][1..ma],
alpha[1..ma][1..ma] are used as working space during most iterations".

Based on the declaration of function in the c++ dll, maybe someone can
give me a hint how the correct declaration of this function should
look like ? The real tricky thing here is the combination of c# and a c
++ dll with parameters that are pointer or pointer to functions.

For pointer to functions a delegate seems right, but how to declare a
pointer to a 2-dim array ? (and, at least, are c++ arrays declared
with foo[,] or with foo[][] ?)

Thanks for any hint or help,
with greetings from germany

Andre

Archived from group: microsoft>public>dotnet>languages>csharp
Back to top
View user's profile Send private message
Serge Baltic



Joined: 08 Aug 2007
Posts: 4

PostPosted: Tue Dec 11, 2007 5:39 pm    Post subject: Re: Call c++ function from c# with some difficult parameters Reply with quote

Hello,

> This don't compile correctly, mostly of the problem that a have to
> give a two dimensional array to the variables covar and alpha (these 2
> arrays uses as working space in the dll for some iterative mathematics
> and are described in docu with "The array covar[1..ma][1..ma],
> alpha[1..ma][1..ma] are used as working space during most iterations".

Basically, the C implementation typically expects to have just some memory
space allocated at the pointer. I think you should try declaring the parameter
as "ref float[]" and allocate the array to the width*height length.

I'm a little bit puzzled by the mangling you have on the method name, which
implies on that it's C++, while it should be pure C by its contract.

(H) Serge
Back to top
View user's profile Send private message
Ben Voigt [C++ MVP]



Joined: 08 Aug 2007
Posts: 189

PostPosted: Tue Dec 11, 2007 2:53 pm    Post subject: Re: Call c++ function from c# with some difficult parameters Reply with quote

wrote in message @s12g2000prg.googlegroups.com...
> Hello together
>
> i just have a little bit tricky question about calling a function out
> of a compiled c++ dll (which do some special mathematic thing) from a
> c# application. The c# application works as cgi and should do, as
> said, some mathematical iterative things (the Levenberg-Marquardt
> method which try to reduce the value x^2 of a fit between a set of
> data points).
>
> For this, the c++ function in the dll is specified as follows
> (declaration by documentation):
>
> mrqmin(float x[], float y[], float sig[], int ndata, float a[], int
> ia[], int ma, float **covar, float **alpha, float *chisq, void (*funcs)
> (float, float [], float *, float [], int), float *alamda)
>
> (and really declared in source code:
> void NR::mrqmin(Vec_I_DP &x, Vec_I_DP &y, Vec_I_DP &sig, Vec_IO_DP &a,
> Vec_I_BOOL &ia, Mat_O_DP &covar, Mat_O_DP &alpha, DP &chisq, void
> funcs(const DP, Vec_I_DP &, DP &, Vec_O_DP &), DP &alamda))

You can't call native C++ member functions from C#. Use a C++/CLI wrapper
(best) or a COM wrapper.
Back to top
View user's profile Send private message
capiccc



Joined: 10 Dec 2007
Posts: 2

PostPosted: Thu Dec 13, 2007 12:48 pm    Post subject: Re: Call c++ function from c# with some difficult parameters Reply with quote

On 11 Dez., 16:53, "Ben Voigt [C++ MVP]" wrote:

> You can't call native C++ member functions from C#. Use a C++/CLI wrapper
> (best) or a COM wrapper.

At first, thanks to Serge and Ben for their posts Smile

Maybe my quote out of the source code was a little bit unlucky. The
function with is exported access within the dll to a C++ member
function, but the exported function is not part of a class structure
and a plain function only. Throughout the member function is called
inside the dll itself, I think I have nothing to do with the class
thing, this is done by the dll internal. So i don't think I need a
wrapper to the class structure.

Correctly (this was my fault i must admint) I should have quoted the
export of the function from the dll which is:

#ifdef NR_EXPORTS
#define NR_API __declspec(dllexport)
#else
#define NR_API __declspec(dllimport)
#endif

NR_API void mrqmin(Vec_I_DP &x, Vec_I_DP &y, Vec_I_DP &sig, Vec_IO_DP
&a,
Vec_I_BOOL &ia, Mat_O_DP &covar, Mat_O_DP &alpha, DP &chisq,
void funcs(const DP, Vec_I_DP &, DP &, Vec_O_DP &), DP &alamda);

I think that is the more relevant export, where the variable
declaration are defines (Vec_I_DP = float[] and so on).

Is a wrapper then needed, too ? I thought I could just import the
function from the DLL through the standard [DLLImport "foo.dll"
EntryPoint="XYZ"] Statement like other dll's ? (The only problem with
the pointers and pointer to function)

Andre
Back to top
View user's profile Send private message
Ben Voigt [C++ MVP]



Joined: 08 Aug 2007
Posts: 189

PostPosted: Thu Dec 13, 2007 6:07 pm    Post subject: Re: Call c++ function from c# with some difficult parameters Reply with quote

wrote in message @d4g2000prg.googlegroups.com...
> On 11 Dez., 16:53, "Ben Voigt [C++ MVP]" wrote:
>
>> You can't call native C++ member functions from C#. Use a C++/CLI
>> wrapper
>> (best) or a COM wrapper.
>
> At first, thanks to Serge and Ben for their posts Smile
>
> Maybe my quote out of the source code was a little bit unlucky. The
> function with is exported access within the dll to a C++ member
> function, but the exported function is not part of a class structure
> and a plain function only. Throughout the member function is called
> inside the dll itself, I think I have nothing to do with the class
> thing, this is done by the dll internal. So i don't think I need a
> wrapper to the class structure.
>
> Correctly (this was my fault i must admint) I should have quoted the
> export of the function from the dll which is:
>
> #ifdef NR_EXPORTS
> #define NR_API __declspec(dllexport)
> #else
> #define NR_API __declspec(dllimport)
> #endif
>
> NR_API void mrqmin(Vec_I_DP &x, Vec_I_DP &y, Vec_I_DP &sig, Vec_IO_DP
> &a,
> Vec_I_BOOL &ia, Mat_O_DP &covar, Mat_O_DP &alpha, DP &chisq,
> void funcs(const DP, Vec_I_DP &, DP &, Vec_O_DP &), DP &alamda);
>
> I think that is the more relevant export, where the variable
> declaration are defines (Vec_I_DP = float[] and so on).
>
> Is a wrapper then needed, too ? I thought I could just import the
> function from the DLL through the standard [DLLImport "foo.dll"
> EntryPoint="XYZ"] Statement like other dll's ? (The only problem with
> the pointers and pointer to function)

It's still using references, not pointers, which are a C++-specific feature
not usable by other languages. So a wrapper would be the solution.

Note that a particular compiler MAY implement a reference argument as
syntactic sugar for an automatically dereferenced const pointer, but I don't
think this is required.

>
> Andre

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Difficult issue with ITypedList and subclasses I am employing ITypedList to expose properties of contained classes and am having trouble with properties nested a few levels down. My class structure looks something like this: ' A collection of Employee objects, Implement

Parameters to "New" in VB Hello, I am building a user control (in VB .NET 2005). It will end up with two data sources. One of those will hold a single row which will populate a set of text boxes. The other data source will return a number of rows which will feed a DataGridView.

GetMethod with ref / out parameters When trying Type[]) on a known method that has the signature Method(ref typea, out DataSet), the method is never found. When enumerating through the known type, the parameter's types' full names come out with a "&" on the end of them, i

Parameters to monitor Hi all, I wish to know what are the common performance parameters that we monitor during development phase of an ASP.NET application. Pls. provide me a list of such parameters if anyone has them. Cheers!!! Nitin.

CCW and method parameters being classes????? Hello, I'm having a problem with CCW and method parameters. Some of the parameters are classes. It seems that these classes (or at least the properties inside the class) are not seen from my VB client Do I have to declare an interface for these classes (e
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> C Sharp All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group