在C#项目中使用本机头文件 [英] Using native headers in C# project

查看:83
本文介绍了在C#项目中使用本机头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



这是我的问题。我有一个包含许多标题和类的本机项目,我尝试使用c ++ / cli包装,以便有一个我可以在我的c#项目中引用的dll。



在某些时刻我只有头文件(没有相应的类)只包含数学函数,我通过将它们包含在我的项目中来使用它们。我的原生项目工作得非常好。



当我有标题和相应的类时,我能够包装它。但是,当我只有只有很多功能的标题时,我不知道如何包装它们。



包装这些头文件的方法是什么?



PS:这是我尝试用C ++ / CLI包装的标题示例



  #ifndef ASSIM_H 
#define ASSIM_H

#include et.h

// 更改给定的基于当前温度的模型参数。
// k25(25°C时的模型参数) ),q10(Q10值),
// lftemp(叶温,摄氏度) )
double Q10( double k25, double q10, double lftemp)
{
return (k25 * pow(q10,(lftemp - 25 0 )/ 10 0 < /跨度>));
}

// Rubisco容量率(umol m-2 s-1 )
// lftemp(leaf temperature,deg.C)
double Vcmax( double lftemp)
{
double const VCMAX = 200 0 ; // 最高费率
double dExp = 1 0 + exp( 0 128 *(lftemp - 40 0 < /跨度>));
return (Q10(VCMAX, 2 4 ,lftemp)/ dExp);
}

// CO2补偿点(umol mol-1)
// lftemp(leaf temperature,deg.C)
< span class =code-keyword> double
CO2CompensationPt( double lftemp)
{
double const TAU = 2600 0 ; // 特殊因素
double const OA = 210000 0 ; // O2浓度
double dTau = Q10(TAU, 0 57 ,lftemp);
return (OA * 0 5 / dTau);
}





我的尝试:



我尝试创建一个ref类来封装这些函数/头,但我需要使用一个指向本机类的指针,但关键是这不是一个类,只是一个执行计算的函数列表。 div class =h2_lin>解决方案

你无法避免在C#中编写所有声明的工作。看看我的文章致电所有电台



重点是将C函数声明为未修饰。


嗨Karstenk,



感谢您的支持答复。我的想法是构建一个C ++ / CLI包装器。我在几个小时前尝试了一些事情,看起来似乎无需重写c#中的所有声明。

这是我写的可能的解决方案(我不知道是否正确)。

所以我做的是以下内容:



对于我的本机标题(assim.h),在我的第一条消息中我定义了一个c ++ / cli上课时间:



1)assimWrapper.h



  //   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



< pre lang =c ++> #include Stdafx.h
// #includeetWrapper.h

#include assim.h
#include assimWrapper.h

命名空间包装器
{
/// < span class =code-comment>
///
double assimWrapper :: Q10Wrap ped( 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)
{
返回 LeafAssim(par,leafTemp);
}

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

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





看起来很有效,因为我可以在我的c#项目中访问我的原生函数。



您认为或其他人是正确的吗?



SD

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

#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.

解决方案

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.


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

// 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

#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


这篇关于在C#项目中使用本机头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆