将int *编组为C# [英] Marshalling int* to C#
问题描述
我在C ++中有一个函数
I've a function in C++ void someFunc(char* arg1, int* arg2)
which I want to marshal the parameters as I use this function in C# (after importing the DLL)..
您能告诉我我在这里感到困惑时应该如何进行编组吗?
Can you pls tell me how I should marshall as I'm confused here.
[DllImport(Dllname)]
extern void someFunc([MarshallAsAttribute(UnmanagedType,LPStr)] string arg1, IntPtr arg2);
我应该在这里使用IntPtr吗?我无法通过C#传递任何int变量的地址,以便它可以在C ++中的指针中找到?
Should I use an IntPtr here? I cant pass the address of any int variable from C# so that it would land up in the pointer in C++?
推荐答案
问题是 int *
可以用于C语言中的许多不同场景. int * arg2
旨在表示.
The problem is int*
can be used for a lot of different scenarios in C. How you marshal this depends a bit on what the int* arg2
is meant to represent.
例如,如果仅设置int的值,则可以将其编组为 ref int
.但是,如果 int *
表示一个数组,则需要传递一个数组(但是,这不太可能,因为没有长度项,这在通过指针使用数组时很常见.).
For example, if it's just setting a value of an int, you can marshal this as ref int
. However, if the int*
is representing an array, you'll want to pass an array (this is unlikely, however, as there is no length term, which is common when using an array via a pointer).
这篇关于将int *编组为C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!