I trying to write a simple function to call unmanaged code from managed code. The data types are int (which should make it even simplier). The function is this
extern "C" MYTEST_API int myTestFunction(int a, int b)
{
return a*b;
}
The managed code looks like
[DllImport("myTest.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int myTestFunction(int a, int b);
...
int p1 = 2;
int p2 = 3;
int result = myTestFunction(p1, p2);
Console.WriteLine("C++ int: " + result);
When I run the program, result returns as
C++ int: 12977616
So, I don’t know what’s wrong with how I’m declaring or passing the int parameters. I tried passing the parameters as “Single” and that version worked.
Figured it would have to be something simple and unrelated to fix this…
The C++ .dll wasn’t getting updated in Visual Studio even though the content was specified as “Copy Always”. So, it was calling the “float” version of the C++ code with int C# parameters. Manually refreshing things got it to work.