A community member has associated this post with a similar question:
What are my options? Calls from C++ C# DLL functions, how?
Only moderators can edit this content.
C++, MFC with C++ Wrapper CLI and C#DLL
Hello!
I have big problems. Why is not working. What am I doing wrong?
C++/MFC EXE App --- CPPWrapper_CLI.DLL ---- C#DLL
How do I have to include the DLL in the exe, reference? How does that work?
Where can the cause be found? Is there a complete example so that I can see how it should work?
Wrapper Header
// CSharpCalcWrapper.h
#pragma once
#include "atlstr.h"
#ifdef CALCWRAP_EXPORTS
#define CALCWRAP_API __declspec(dllexport)
#else
#define CALCWRAP_API __declspec(dllimport)
#endif
extern "C" CALCWRAP_API int CallSetupDialog();
extern "C" CALCWRAP_API CString CallGetOrder(CString orderId);
extern "C" CALCWRAP_API int Calc_Add(int a, int b);
Wrapper CPP
/ This is the main DLL file.
#include "stdafx.h"
#include "CSharpCalcWrapper.h"
#include "atlstr.h" // ** for CString
#include <string>
#include <iostream>
//#include "CSharpCalcWrapper.h"
using namespace System;
using namespace std;
using namespace CSharpCalculate;
CALCWRAP_API CString CallGetOrder(CString orderId )
{
String^ orderIdMES = gcnew String(orderId);
System::String^ managed = StaticCalc::GetOrder(orderIdMES);
//String^ resMES = gcnew String(res);
//System::String^ str = "Hello World";
//std::string result = msclr::interop::marshal_as<std::string>(str);
//System::String^ managed = "test";
//std::string unmanaged2 = msclr::interop::marshal_as<std::string>(managed);
char cStr[5096] = { 0 };
String^ clrString = managed;
if (clrString->Length < sizeof(cStr))
sprintf(cStr, "%s", clrString);
std::string stlString(cStr);
CString ret = cStr;
//return unmanaged2;
//std::string jjj = StaticCalc::GetOrder();
//return ;
return ret;
}
CALCWRAP_API int CallSetupDialog()
{
return StaticCalc::CallSetupDialog();
}
CALCWRAP_API int Calc_Add(int a, int b)
{
//Calc obj;
//return obj.Add(a, b);
return StaticCalc::Add(a, b); // if static!
}
CPP, MFC EXE.
How do I have to include the DLL in the exe, reference? How does that work?
Picture:

C#DLL
static public class StaticCalc
{
....
static public int CallSetupDialog()
{
SetupDlg = new frmSetup();
SetupDlg.ShowDialog();
return 0;
}
static public string GetOrder(string orderID)
{
string res = "[ORDER]|0932342|42424|1000|R5E";
return res;
}
static public int Add(int a, int b)
{
return a +b;
}
extern "C" CALCWRAP_API int CallSetupDialog();
extern "C" CALCWRAP_API CString CallGetOrder(CString orderId);
extern "C" CALCWRAP_API int Calc_Add(int a, int b);
extern "C" CALCWRAP_API OrderProduct GetOrderProduct(string order, double price);
// Is it possible?
// To get a JSON object as return value an then I can fill a C++ class/object?