2011-06-06 36 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); 
}