Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everybody,

Here is my question. I have a native project with many headers and class that i try to wrap using c++/cli in order to have a dll that i could reference in my c# project.

In some moment i only have headers file (without the corresponding class) that contains only mathematical functions and i use them by including them in my project. My native project work very well.

When i have the header and the corresponding class i am able to wrap this. But when i only have the header with only many function i don't know how to wrap these.

What is the way for wrap these headers file ?

P.S : here is an example of headers that i try to wrap in C++/CLI

C++
#ifndef ASSIM_H
#define ASSIM_H

#include "et.h"

// Changes the given model parameter based on current temperature.
//   k25 (model parameter at 25 deg. C), q10 (Q10 value),
//   lftemp (leaf temperature, deg. C)
double Q10(double k25, double q10, double lftemp)
{
   return (k25 * pow(q10, (lftemp - 25.0) / 10.0));
}

// Rubisco capacity rate (umol m-2 s-1)
//   lftemp (leaf temperature, deg. C)
double Vcmax(double lftemp)
{
   double const VCMAX = 200.0;   // max rate
   double dExp = 1.0 + exp(0.128 * (lftemp - 40.0));
   return (Q10(VCMAX, 2.4, lftemp) / dExp);
}

// CO2 compensation point (umol mol-1)
//   lftemp (leaf temperature, deg. C)
double CO2CompensationPt(double lftemp)
{
   double const TAU = 2600.0;    // specificity factor
   double const OA = 210000.0;   // O2 concentration
   double dTau = Q10(TAU, 0.57, lftemp);
   return (OA * 0.5 / dTau);
}


What I have tried:

I try to create a ref class to encapsulate these functions/headers but i need use a pointer to the native class but the point is that this is not a class but only a list of functions that perform calculations.
Posted
Updated 27-Jul-16 4:08am
Comments
[no name] 27-Jul-16 9:21am    
I never used it but maybe it can assist you: PInvoker - C#/VB.NET PInvoke Interface Generation Tools[^]

You cant avoid the work to write all declarations in C#. Take a look at my article Calling all Stations.

The point is to declare the C functions as undecorated.
 
Share this answer
 
Comments
naecotor 27-Jul-16 13:56pm    
Hi karstenk,

Thanks for your reply. My idea is build a C++/CLI wrapper. I try something some hours ago that it seems work without having to rewrite all declarations in c#.
Here is the "possible" solution i wrote (i don't know if it is correct).
So what i did is the following in the solution i propose. Is not a solution it seems that i made mistake for reply to your comment.

SD
Hi Karstenk,

Thanks for your reply. My idea is build a C++/CLI wrapper. I try something some hours ago that it seems work without having to rewrite all declarations in c#.
Here is the "possible" solution i wrote (i don't know if is correct).
So what i did is the following :

For my native header (assim.h) that is in my first message i defined a c++/cli class with :

1) assimWrapper.h

C++
// PaSimWrapper.h
#pragma once

#include "Stdafx.h"

namespace Wrapper{
	
	public ref class assimWrapper
	{
	public:
		double Q10Wrapped(double k25, double q10, double leafTemp);
		double CO2CompensationPtWrapped(double leafTemp);
		double VcmaxWrapped(double leafTemp);
		double LightLimitedWrapped(double par, double leafTemp, double ci);
		double RubiscoLimitedWrapped(double leafTemp, double ci);
		double SinkLimitedWrapped(double leafTemp);
		double LeafAssimWrapped(double par, double leafTemp);
		double HourCanopyAssimWrapped(double th, double leafTemp);
		double DayCanopyAssimWrapped();

	};
}


2) assimWrapper.cpp

C++
#include "Stdafx.h"
//#include "etWrapper.h"

#include "assim.h"
#include "assimWrapper.h"

namespace Wrapper
{
	///
	///
	double assimWrapper::Q10Wrapped(double k25, double q10, double leafTemp)
	{
		return Q10(k25, q10, leafTemp);
	}

	///
	///
	double assimWrapper::CO2CompensationPtWrapped(double leafTemp)
	{
		return CO2CompensationPt(leafTemp);
	}

	///
	///
	double assimWrapper::VcmaxWrapped(double leafTemp)
	{
		return Vcmax(leafTemp);
	}

	///
	///
	double assimWrapper::LightLimitedWrapped(double par, double leafTemp, double ci)
	{
		return LightLimited(par, leafTemp, ci);
	}

	///
	///
	double assimWrapper::RubiscoLimitedWrapped(double leafTemp, double ci)
	{
		return RubiscoLimited(leafTemp, ci);
	}

	///
	///
	double assimWrapper::SinkLimitedWrapped(double leafTemp)
	{
		return SinkLimited(leafTemp);
	}

	///
	///
	double assimWrapper::LeafAssimWrapped(double par, double leafTemp)
	{
		return LeafAssim(par, leafTemp);
	}

	///
	///
	double assimWrapper::HourCanopyAssimWrapped(double th, double leafTemp)
	{
		return HourCanopyAssimWrapped(th, leafTemp);
	}

	///
	///
	double assimWrapper::DayCanopyAssimWrapped()
	{
		return DayCanopyAssim();
	}
};


It seems working because i have access to my native functions in my c# project.

Do you think or other people that is correct ?

SD
 
Share this answer
 
Comments
Matt T Heffron 27-Jul-16 12:57pm    
You should not post a Solution to reply to a comment.
You should use the "Have a Question or Comment?" button for basic comments.
That way the person to whom you are responding gets an email notification of your comment.
(Comments do not allow for code markup.)

If you want to add anything more substantial, like explanatory code, then use the "Improve question" that is at the bottom of your original question. Keep the original question intact, but add the addition(s) below.
[no name] 27-Jul-16 19:25pm    
This solution looks good to me. It has the option of static linking the C code to your C++/CLI assembly - which makes it better than KarstenK's solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900