4

How can I convert a C++/CLI int %tmp to native C++ int &tmp?

void test(int %tmp)
{
    // here I need int &tmp2 for another pure C++ function call
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
leon22
  • 5,280
  • 19
  • 62
  • 100

3 Answers3

3

Neither of the existing answers properly handle in/out parameters, let alone any advanced use cases.

This should work for all cases where other_func does not keep the reference after it returns:

void test(int %tmp)
{
    pin_ptr<int> pinned_tmp = &tmp;
    other_func(*pinned_tmp);
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

Just tried this, works fine:

  //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;
}
stijn
  • 34,664
  • 13
  • 111
  • 163
0
void your_function(int *);
void your_function2(int &);

void test(int %tmp)
{
    int tmp2;
    your_function(&tmp2);
    your_function2(tmp2);
    tmp=tmp2;
}
Doc Brown
  • 19,739
  • 7
  • 52
  • 88