We value your input!
Please participate in Archicad 28 Home Screen and Tooltips/Quick Tutorials survey

Archicad C++ API
About Archicad add-on development using the C++ API.
SOLVED!

std::string -> GS::uchar_t gdl parameter

Anonymous
Not applicable
Hi there!

I think i have a simple problem, but i couldn't get over it by myself....

I have an std::string parameter which is came from a csv file through ifstream::getline. And i have to change a library element string parameter, which is uchar_t *. But i couldn't get it converted...

so here is what i tried:

//these are predefined
   std::string line;
   Int32 id;
   char paramname[32];

API_ChangeParamType  chgParam;

BNZeroMemory(&chgParam, sizeof(API_ChangeParamType));

chgParam.index = id;
strcpy(chgParam.name, paramname);
GS::ucscpy(chgParam.uStrValue, GS::UniString(line.c_str()).ToUStr().Get()); //GSReporter crash

but it is crashing during ucscpy.
So how should i transfere my string to get it working? I always had issues with string especially unistrings/unichars.
1 ACCEPTED SOLUTION

Accepted Solutions
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Hi,

This should work:
GS::uchar_t bufferUStr[API_UAddParStrLen];
GS::ucsncpy (bufferUStr, GS::UniString (line.c_str()).ToUStr().Get(), API_UAddParStrLen - 1);
chgParam.uStrValue = bufferUStr;
Just make sure, that bufferUStr will live till you use the chgParam

A little explanation:
uStrValue is a simple pointer, it just points to somewhere. That's why you cannot write characters into it and you got crash report immediately. You have to set the pointer to an existing array of characters.

View solution in original post

2 REPLIES 2
Solution
Tibor Lorantfy
Graphisoft Alumni
Graphisoft Alumni
Hi,

This should work:
GS::uchar_t bufferUStr[API_UAddParStrLen];
GS::ucsncpy (bufferUStr, GS::UniString (line.c_str()).ToUStr().Get(), API_UAddParStrLen - 1);
chgParam.uStrValue = bufferUStr;
Just make sure, that bufferUStr will live till you use the chgParam

A little explanation:
uStrValue is a simple pointer, it just points to somewhere. That's why you cannot write characters into it and you got crash report immediately. You have to set the pointer to an existing array of characters.
Anonymous
Not applicable
Thank you very much this is helped a lot to understand pointers as well because i didn't used a lot c++.