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 retur开发者_开发问答ns 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.
E.g.
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);
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.
You need to use out
keyword before parameters names (example). But actually this is not good practice in C#
精彩评论