我有一个Blah.cs:
public unsafe static int Main()
{
int[] ai = {1, 2, 3, 4, 5};
UIntPtr stai = (UIntPtr) ai.Length;
CManagedStuff obj = new CManagedStuff();
obj.DoSomething(ai, stai);
}
接下来是ManagedStuff.cpp:
void CManagedStuff::DoSomething(int^ _ai, UIntPtr _stai)
{
// Here I should do something to marshal the int^ to an int*
pUnmanagedStuff->DoSomething(_ai, (size_t) _stai);
}
还有一个UnmanagedStuff.cpp文件:
void CUnmanagedStuff::DoSomething(int* _ai, size_t _stai)
{
// Walk and print the _stai ints in _ai
}
我该如何将Main中的
int[] ai
传递到ManagedStuff::DoSomething?由于所有涉及的代码都是托管的,因此在这个调用中没有任何编组。然后,我该如何编组
int^ _ai
以调用UnmanagedStuff::DoSomething中的内容?如果我有一个int[] _ai
,那么回答这个SO问题的代码可以帮助我(C#: Marshalling a "pointer to an int array" from a SendMessage() lParam)。或者,我如何避免使用C#、C++互操作、Microsoft和Windows,并停止世界痛苦?