从C ++ / CLI调用C#dll函数 [英] Call C# dll function from C++/CLI

查看:175
本文介绍了从C ++ / CLI调用C#dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# dll。代码如下:

  public class Calculate 
{
public static int GetResult(int arg1,int arg2)
{
return arg1 + arg2;
}

public static string GetResult(string arg1,string arg2)
{
return arg1 ++ arg2;
}

public static float GetResult(float arg1,float arg2)
{
return arg1 + arg2;
}

public Calculate()
{
}
}

现在,我打算以这种方式从 C ++ 调用此dll。

  [DllImport(CalculationC.dll,EntryPoint =Calculate,CallingConvention = CallingConvention :: ThisCall)] 
extern void Calculate();

[DllImport(CalculationC.dll,EntryPoint =GetResult,CallingConvention = CallingConvention :: ThisCall)]
extern int GetResult(int arg1,int arg2);

这里是函数,其中调用GetResult

  private:System :: Void CalculateResult(int arg1,int arg2)
{
int rez = 0;

//从dll调用C ++函数
计算calculate = new Calculate();
rez = GetResult(arg1,arg2);
}

我得到错误:syntax error:identifier'Calculate'
有人可以帮我解决这个可怕的错误吗?

解决方案

你必须使用c ++ CLI,否则不能调用DllImport。
如果是这种情况,你只需要引用c#dll。



在c ++ CLI中,你可以这样做:

 使用命名空间Your :: Namespace :: Here; 

#using< YourDll.dll>

YourManagedClass ^ pInstance = gcnew YourManagedClass();

其中'YourManagedClass'是在c#项目中定义的输出程序集'YourDll.dll' p>

** EDIT **
添加您的示例。



这是您的示例需要的样子在CLI中(为了清楚起见,我假设G
etResult不是静态函数,否则你只需调用Calculate :: GetResult(...)

  private:System :: Void CalculateResult(int arg1,int arg2)
{
int rez = 0;
//从dll调用C ++函数
计算^ calculate = gcnew Calculate();
rez = calculate-> GetResult(arg1,arg2);
}


I have a C# dll. The code is below:

public class Calculate
{
    public static  int GetResult(int arg1, int arg2)
    {
        return arg1 + arg2;
    }

    public static  string GetResult(string arg1, string arg2)
    {
        return arg1 + " " + arg2;
    }

    public static   float GetResult(float arg1, float arg2)
    {
        return arg1 + arg2;
    }

    public Calculate()
    {
    }
}

Now, I am planning to call this dll from C++ on this way.

[DllImport("CalculationC.dll",EntryPoint="Calculate", CallingConvention=CallingConvention::ThisCall)]
extern void Calculate();

[DllImport("CalculationC.dll",EntryPoint="GetResult", CallingConvention=CallingConvention::ThisCall)]
extern int GetResult(int arg1, int arg2);

Here is function where is called GetResult

private: System::Void CalculateResult(int arg1, int arg2)
{
    int rez=0;

    //Call C++ function from dll
    Calculate calculate=new Calculate();
    rez=GetResult(arg1,arg2);
}

I got the error : "syntax error : identifier 'Calculate'". Can someone help me with this terrible error?

解决方案

You must be using c++ CLI, otherwise you could not call DllImport. If that is the case you can just reference the c# dll.

In c++ CLI you can just do as follows:

using namespace Your::Namespace::Here;

#using <YourDll.dll>

YourManagedClass^ pInstance = gcnew YourManagedClass();

where 'YourManagedClass' is defined in the c# project with output assembly 'YourDll.dll'.

** EDIT ** Added your example.

This is how your example needs to look like in CLI (for clarity I am assuming that G etResult is not a static function, otherwise you would just call Calculate::GetResult(...)

private: System::Void CalculateResult(int arg1, int arg2)
{
    int rez=0;
    //Call C++ function from dll
    Calculate^ calculate= gcnew Calculate();
    rez=calculate->GetResult(arg1,arg2);   
}

这篇关于从C ++ / CLI调用C#dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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