C# 系统::C+中的IntPtr到int*+/CLI 我试图用C++ /CLI互操作层从C语言应用程序调用现有的C++库。我有一个C++函数,看起来像这样: void doSomething(int* foo, int size); public ref class Wrapper { public: void run(System::IntPtr itn, int size); };

C# 系统::C+中的IntPtr到int*+/CLI 我试图用C++ /CLI互操作层从C语言应用程序调用现有的C++库。我有一个C++函数,看起来像这样: void doSomething(int* foo, int size); public ref class Wrapper { public: void run(System::IntPtr itn, int size); };,c#,c++,interop,c++-cli,C#,C++,Interop,C++ Cli,还有一个C#对象,它包含两个字段,一个IntPtr start和一个int size。我编写了一个类似C++的托管类: void doSomething(int* foo, int size); public ref class Wrapper { public: void run(System::IntPtr itn, int size); }; 现在我想在Wrapper::run中调用doSomething,让doSomething访问C#object指向的同一数据块。如果没有副

还有一个C#对象,它包含两个字段,一个IntPtr start和一个int size。我编写了一个类似C++的托管类:

void doSomething(int* foo, int size);
public ref class Wrapper {
public:
    void run(System::IntPtr itn, int size);
};
现在我想在Wrapper::run中调用doSomething,让doSomething访问C#object指向的同一数据块。如果没有副本,我想做的事情是否可能,如果可能,如何做?

要从Wrapper::run调用doSomething,请尝试强制转换返回,如下所示:

doSomething(reinterpret_cast<int*>(itn.ToPointer()), size);
doSomething(重新解释铸件(itn.ToPointer()),尺寸);

你肯定是想把它写成
无效运行(int%itn,int size)
?如果没有,那么内存需要由某人固定。不要忽视这个要求,最好用PINE-PTR在C++/CLI中完成,这样C++代码就可以简单地使用INT[]。对不起,我是CLI和C的新手,我最熟悉C++和C.,钉住内存有什么用?