15

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?

2
  • 6
    If you are using c++ CLI why not just reference the c# assembly directly? DllImport is meant to allow you to call unmanaged dll's from managed code. Commented Jan 27, 2011 at 15:18
  • I am bit confused with Visual Studio C++. Can you suggest me how to add reference to my dll correctly in VS2010 C++ project. I tried with Assembly.LoadFile without any success. Commented Jan 27, 2011 at 15:24

1 Answer 1

27

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);   
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you a lot for you help and convenience.
Am happy it helped. If it solved your problem don't forget to mark the reply as answer ;-) Thx!
WATCH YOUR FRAMEWORK VERSIONS! Just a huge note on this. Some Visual C++ projects want to default to .NET 4, whereas your DLL will probably default to .NET 4.5, and when you try to run similar code it can complain with an obfuscated error like, "File not found." Well, you'll need to unload your Visual C++ project and then edit the project file's XML markup to enforce that it compiles against .NET 4.5. Then it will work. Spent a good hour scratching my head on this one...LOL :)
Is there any performance drawback to reference library developed in C# to a C++/CLI project? I am developing a class library which is supposed to be used in C# and C++/CLI project. Should I code C++/CLI version separately in unmanaged code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.