2

I have a C# DLL with the following code:

namespace Csharplib`{
  public class Calculate
  {
      public static int Sum(String value1, String value2)
      {
          int res = int.Parse(value1) + int.Parse(value2);
          return res;
      }
  }
}`

I have a CLI/CLR C++ Application where I have added the reference of my C# DLL. Following is the code of my CLI/CLR C++ Application:

using namespace System;
using namespace System::Reflection;

namespace CplusManaged {
  public ref class DoWork
  {
    public:int DoSum(System::String ^value1, System::String ^value2)
    {
      return Csharplib::Calculate::Sum(value1, value2);
    }
  };
}

__declspec(dllexport) int ShowSUM(System::String ^value1, System::String ^value2)`{
  CplusManaged::DoWork work;
  return work.DoSum(value1,value2);
}`

When I build my application, I get the following error:

'ShowSUM': __declspec(dllexport) cannot be applied to a function with the __clrcall calling convention

I am a C# developer and I have no experience in C++ whatsoever. Is there any way I can solve this problem?

DPM
  • 1,960
  • 3
  • 26
  • 49
Uzair Raza
  • 55
  • 4
  • You are going to have to find somebody that *does* know C++ so you can test your library. Some odds that this somebody will tell you "Oh, just use the Unmanaged Exports utility". – Hans Passant Dec 27 '17 at 13:33
  • Please suggest me a way to solve this problem. I will be very grateful to you. – Uzair Raza Dec 28 '17 at 04:53

1 Answers1

0

Use wchar_t* as parameter type, and convert to String ^ using gcnew String() like this:

__declspec(dllexport) int ShowSUM(wchar_t * inValue1, wchar_t * inValue2)
    {
        String ^ value1 = gcnew String(inValue1);
        String ^ value2 = gcnew String(inValue2);

        CplusManaged::DoWork work;
        return work.DoSum(value1, value2);
    }
Aurel Havetta
  • 455
  • 4
  • 9