my question is clear from the topic, i need to send a string from c# to c++ function and vice versa (terminology may be not right i am a noob) so i watched this tutorial :https://www.youtube.com/watch?v=hwmRtnJag4A&index=1&list=LLTUeeaXdALOpRBhDbqiFWlQ .Howewer I could not get it working, after running the debugger in c# i got this error:
Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\Viktor\Documents\Visual Studio 2015\Projects\DLL_test\DLL_test\bin\x86\Debug\DLL_test.vshost.exe'.
here is my code: main.cpp
#include"Header.h"
#include<iostream>
void main()
{
myClass sumTwoClass(10, 5);
double result = sumTwoClass.sumX_Y();
}
body.cpp
myClass::myClass(double var_x, double var_y)
{
x = var_x;
y = var_y;
}
double myClass::sumX_Y()
{
return x + y;
}
header.h
#pragma once
class myClass
{
public:
myClass(double var_x, double var_y);
double sumX_Y();
private:
double x;
double y;
};
CppClassDll main.cpp
#include"C:\\Users\\Viktor\\Documents\\Visual Studio 2015\\Projects\\MyClassCpp\\MyClassCpp\\Header.h"
#include"C:\\Users\\Viktor\\Documents\\Visual Studio 2015\\Projects\\MyClassCpp\\MyClassCpp\\body.cpp"
extern "C" __declspec(dllexport) double sumTwo(double var_x, double var_y)
{
myClass MC(var_x, var_y);
return MC.sumX_Y();
}
Dll_test Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace DLL_test
{
class Program
{
[DllImport("C:\\Users\\Viktor\\Documents\\Visual Studio 2015\\Projects\\CppClassDll\\Debug\\CppClassDll.dll")]
public static extern double sumTwo(double var_x, double var_y);
static void Main(string[] args)
{
double var_x = 5;
double var_y = 30;
double result = sumTwo(var_x,var_y);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
Now the interesting thing is that when i run this i should get the result (35) in console but got the error i described above ...however today i ran the code (changed nothing) and the console showed up with the value 35, i was like what? i tried to run the program again and got the same error. Am i cursed or what?
At debugging i got 0 errors, debugger set to x86.