C# and C++ Communication

Question:

How can I make C# code call a variable that is in a C++ file for example. Knowing that I'm using Visual Studio.

C++ code:

#include "stdafx.h"
#include <iostream>


int main()
{
    int test = 10;
    return 0;
}

but how can i access this variable in c#

Answer:

You need to do interop. Basically you will generate a dll in c++ exposing an interface to be consumed in c#. In the C# application you will import this dll and then call the native code within the managed code. There's a great series on MSDN talking about using Pinvoke.

Keywords for search: Pinvoke, Interop, dllimporter

Scroll to Top