如何將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