Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call C# dll function from C++/CLI

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?

like image 368
Bogdan Stojanovic Avatar asked Jan 27 '11 15:01

Bogdan Stojanovic


People also ask

What is call C?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

What is call by name in C?

Call by Reference : instead of the parameters, their addresses are passed and formal parameters are pointing to the actual parameters. Call by Name : like macros, the whole function definition replaces the function call and formal parameters are just another name for the actual parameters.

What does it mean to call a function in C?

Calling a Function When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.

What do we call & in C?

Master C and Embedded C Programming- Learn as you go The & symbol is used as an operator in C++. It is used in 2 different places, one as a bitwise and operator and one as a pointer address of operator.


1 Answers

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);   
}
like image 190
santiagoIT Avatar answered Sep 28 '22 02:09

santiagoIT