question

Hao Zhou avatar image
0 Likes"
Hao Zhou asked Hao Zhou commented

Member function cannot be bound properly if passing string as parameter

I just noticed that class member function cannot be bound as FlexScript interface properly if passing string as parameter. For example, I create a member function as follows:

void printStr(string str)
{
	pt(str);
	pr();
}

And I bind it in function bindInterface() as follows:

bindMethod(printStr, IntVector, "void printStr(string str)");

When I call the function in FlexSim by:

IntVector iv = IntVector.reset(so());
iv.printStr("abc");

FlexSim either crashes or prints something weird. When I debug it, it shows that the string "abc" is not able to pass into member function properly. See the following images:

Source code is also attached here intvector.zip

Could you provide suggestions to fix it?

Thanks, Hao

FlexSim 17.1.1
module sdkcustom flexscript interface
1.png (5.1 KiB)
2.png (14.4 KiB)
intvector.zip (2.0 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
anthony.johnson avatar image
2 Likes"
anthony.johnson answered Hao Zhou commented

I would strongly suggest not passing strings across dll boundaries. If your dll is built using a different version of Visual Studio than the FlexSim engine, or using a different configuration like Debug (the engine you have is built in Release mode), then the definition of string will likely be different in your DLL vs FlexSim's definition. This causes exceptions and perhaps crashing behavior because the engine passes one thing, and the DLL thinks it's another thing. Instead, just have your methods take const char*'s, which will just be char*'s in the FlexScript-visible method signature. FlexScript can automatically convert from strings to char*'s.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Hao Zhou avatar image Hao Zhou commented ·

Thanks for the answer Anthony. This is really helpful.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.