2011-06-06 37 views

回答

0
void your_function(int *); 
void your_function2(int &); 

void test(int %tmp) 
{ 
    int tmp2; 
    your_function(&tmp2); 
    your_function2(tmp2); 
    tmp=tmp2; 
} 
+1

這幾乎肯定會給出一個錯誤:編譯器不能從int轉換*爲int& – stijn 2011-06-06 09:24:41

+0

@stijn:這取決於「your_func」的簽名。看到我更改的示例。 – 2011-06-06 09:39:37

+0

工作正常!謝謝 – leon22 2011-06-06 10:56:21

1

只是嘗試這樣做,做工精細:

//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; 
} 
3

無論是現有的答案正確處理輸入/輸出參數,更何況任何先進的用例。

這應該對所有情況在哪裏工作other_func不守參考返回後:

void test(int %tmp) 
{ 
    pin_ptr<int> pinned_tmp = &tmp; 
    other_func(*pinned_tmp); 
}