简体   繁体   中英

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?

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

Eg

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

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);

如果要在方法内分配给它们,并使这些值在方法外可用out则需要使字符串参数为refout

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM