如何将C++/CLI int %tmp
转换为本机C++ int &tmp
?将.Net ref(%)转换为本地(&)
void test(int %tmp)
{
// here I need int &tmp2 for another pure C++ function call
}
如何将C++/CLI int %tmp
转换为本机C++ int &tmp
?将.Net ref(%)转换为本地(&)
void test(int %tmp)
{
// here I need int &tmp2 for another pure C++ function call
}
void your_function(int *);
void your_function2(int &);
void test(int %tmp)
{
int tmp2;
your_function(&tmp2);
your_function2(tmp2);
tmp=tmp2;
}
只是尝试这样做,做工精细:
//in the C++ dll
void testFunc(int& n)
{
n = 5;
}
//in the CLI app
[DllImport("my.dll", EntryPoint = "?exported_name_here",
CallingConvention = CallingConvention::StdCall)]
void TestFunc(int&);
void test(int% tmp)
{
int n;
TestFunc(n);
tmp = n;
}
无论是现有的答案正确处理输入/输出参数,更何况任何先进的用例。
这应该对所有情况在哪里工作other_func
不守参考返回后:
void test(int %tmp)
{
pin_ptr<int> pinned_tmp = &tmp;
other_func(*pinned_tmp);
}
这几乎肯定会给出一个错误:编译器不能从int转换*为int& – stijn 2011-06-06 09:24:41
@stijn:这取决于“your_func”的签名。看到我更改的示例。 – 2011-06-06 09:39:37
工作正常!谢谢 – leon22 2011-06-06 10:56:21