0

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.

Viktor Bendik
  • 97
  • 3
  • 11
  • Okay so the reason that i got that correct value is that i continued the debugging after it threw the error. Then it works as it supposed to. So when i run the debugger it throws the error, i cancel it then everything works fine ... i think there will be something with the DllImport – Viktor Bendik Mar 29 '16 at 12:13

1 Answers1

0
  • Right click on the project and select properties

    • Switch to the build tab

      whichever is selected for the platform ?

levent
  • 3,464
  • 1
  • 12
  • 22
  • The selected platform is x86 if i change it to any it does the same thing . There is a column to not break when this error occur so now its working without error. Don't know if this is a permanent fix or just temporary. I read something about it here : http://stackoverflow.com/questions/3506796/pinvokestackimbalance-how-can-i-fix-this-or-turn-it-off – Viktor Bendik Mar 29 '16 at 12:39