从C+调用C#dll函数+/CLI

从C+调用C#dll函数+/CLI,c#,dll,interop,c++-cli,dllimport,C#,Dll,Interop,C++ Cli,Dllimport,我有一个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; } pu

我有一个
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;

    //Call C++ function from dll
    Calculate calculate=new Calculate();
    rez=GetResult(arg1,arg2);
}
我得到了错误:“语法错误:标识符'Calculate'”。
有人能帮我解决这个可怕的错误吗?< /P> < P>你必须使用C++ CLI,否则你不能调用DLLIMPART。 如果是这种情况,您可以直接引用c#dll

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

using namespace Your::Namespace::Here;

#using <YourDll.dll>

YourManagedClass^ pInstance = gcnew YourManagedClass();

如果使用C++ CLI,为什么不直接引用C?dLimPurt是为了允许您从托管代码调用非托管DLL。我与VisualStudioC++有点混淆。您能建议我如何在VS2010 C++项目中正确地添加我的DLL吗?我尝试了Assembly.LoadFile,但没有成功。看起来你忘记接受答案了。非常感谢你的帮助和方便。我很高兴能帮上忙。如果它解决了你的问题,别忘了将回答标记为答案;-)谢谢!注意你的框架版本!这上面有一张大纸条。一些VisualC++项目希望默认为.NET 4,而您的DLL可能默认为.NET 4.5,当您尝试运行类似代码时,它会抱怨一个模糊的错误,比如“文件未找到”。那么,您需要卸载Visual C++项目,然后编辑项目文件的XML标记,以强制它编译.NET 4.5。那就行了。我花了一个小时来琢磨这个问题……哈哈:)用C++开发的C++/CLI项目参考库有什么性能缺陷吗?我正在开发一个类库,该类库应该用于C#和C++/CLI项目。我应该在非托管代码中单独编写C++/CLI版本吗?
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);   
}