How to pass C ++ short * to a C # managed assembly in C ++ / CLI

I am having trouble passing an argument from C ++ / CLI code to .NET C #.

In C ++, I have something similar to the following:

void SomeFunction(short *id)
{
   CSharpClass::StaticClassInstance->SetValue(id);
}

On the C # side, the function is declared with the ref argument as:

public void SetValue(ref short id)
{
  id = this.internalIdField;
}

The compiler error that I get when calling SetValue (id), "cannot convert parameter 1 from" short * "to" short% ".

I found out that the tracking link (%) is equivalent to C # ref, but I don’t know how to use it with the short * parameter that I am trying to pass.

Thanks in advance.

+3
source share
2 answers

Logical signature C ++ / CLI CSharpClass::SetValue-

void SetValue(short% id);

If you know C ++ (unlike C ++ / CLI), the answer here is exactly the same as if you had a C ++ signature

void SetValue(short& id);

I.e., :

void SomeFunction(short *id)
{
    CSharpClass::StaticClassInstance->SetValue(*id);
}
+3

, , id, ...

short %s = *id;

SetValue (s);

, . * %, %...

SetValue (*id);
+1

All Articles