Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 托管代码和非托管代码之间的Int值未正确编组_.net_Interop - Fatal编程技术网

.net 托管代码和非托管代码之间的Int值未正确编组

.net 托管代码和非托管代码之间的Int值未正确编组,.net,interop,.net,Interop,我试图编写一个简单的函数,从托管代码调用非托管代码。数据类型为int(这将使其更加简单)。功能是这样的 extern "C" MYTEST_API int myTestFunction(int a, int b) { return a*b; } 托管代码看起来像 [DllImport("myTest.dll", CallingConvention = CallingConvention.Cdecl)] private static extern int myTestFu

我试图编写一个简单的函数,从托管代码调用非托管代码。数据类型为int(这将使其更加简单)。功能是这样的

extern "C" MYTEST_API int myTestFunction(int a, int b)
{
    return a*b;
}
托管代码看起来像

    [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);
当我运行程序时,结果返回为

C++ int: 12977616

所以,我不知道如何声明或传递int参数有什么问题。我尝试将参数作为“单一”传递,该版本有效。

认为它必须是简单且与修复此问题无关的东西。。。
即使在内容被指定为“复制总是”的情况下,VisualStudio中的C++也不会得到更新。因此,它调用了带有int C参数的C++代码的“浮点”版本。手动刷新的东西让它工作。< /P>正确检查你的C++代码。也许你错过了一些东西,因为映射是正确的。如果单为您工作,它可能是C++代码中的错误,因为单机和Int内存非常不同,不能一个一个地映射。