diablo_alex
diablo_alex

Reputation: 37

Migrate function C++ with pointers to C#

I have a function in C++ that returns pointer values​​:

fPosFirst( int &aId, char *aNname, char *aDirectory );

My syntax in c# is:

fPosFirst(ref int aId,  String aNname,  String aDirectory);

the function returns the id but not the string parameters that anyone knows?

Upvotes: 1

Views: 171

Answers (4)

dtb
dtb

Reputation: 217253

Assuming that the native function does not "return pointers" but writes characters to the memory locations specified by aNname and aDirectory, you should be able to pass a StringBuilder with a proper capacity to the native function:

void fPosFirst(ref int aId, StringBuilder aNname, StringBuilder aDirectory);

Usage:

var aId = 0;
var aNname = new StringBuilder(260);
var aDirectory = new StringBuilder(260);

fPosFirst(ref aId, aNname, aDirectory);

Upvotes: 1

Andrey Atapin
Andrey Atapin

Reputation: 7945

You need to use out keyword before parameters names (example). But actually this is not good practice in C#

Upvotes: 0

FishBasketGordo
FishBasketGordo

Reputation: 23132

You need to make the string parameters ref or out if you want to assign to them within the method and have those values available outside the method.

Upvotes: 0

Adam Ralph
Adam Ralph

Reputation: 29956

If you want the parameters to be used for returning values then mark them as ref or out.

E.g.

fPosFirst(ref int aId,  out string aNname,  out string aDirectory);

Upvotes: 2

Related Questions