Returning string from C++ user command

Returning string from C++ user command

mischa_spelt
Advisor Advisor
36 Views
3 Replies
Message 1 of 4

Returning string from C++ user command

mischa_spelt
Advisor
Advisor

[ FlexSim 17.0.0 ]

I'm importing an old (7.x) DLL to 17.0 and running into some problems with user commands returning a string. We used to write functions like

visible double DLL_GetDateTimeString( FLEXSIMINTERFACE )
{
  stringreturn( "Hello world!" );
  return 0;
}

but that does not seem to work anymore. The documentation states that as of FlexSim 2016 one can "simply" return a string from the user command, but I tried a couple of options such as

visible std::string DLL_GetDateTimeString( FLEXSIMINTERFACE )
{
  return std::string("Hello world!");
}

and

visible char* DLL_GetDateTimeString( FLEXSIMINTERFACE )
{
  std::string result = "Hello world!";
  return apchar( result );
}

or

visible const char* DLL_GetDateTimeString( FLEXSIMINTERFACE )
{
  std::string result = "Hello world!";
  return result.c_str();
}

but they break the stack or give me nonsense output, respectively.

Could you provide an example of how to return strings from a DLL function in version 2017?

Accepted solutions (1)
37 Views
3 Replies
Replies (3)
Message 2 of 4

philboboADSK
Autodesk
Autodesk
Accepted solution

Change your function definition to return a Variant:

__declspec(dllexport) Variant DLL_GetDateTimeString(FLEXSIMINTERFACE)
{
  return std::string("Hello world!");
}

4398-return-string.jpg



Phil BoBo
Sr. Manager, Software Development
Message 3 of 4

mischa_spelt
Advisor
Advisor

Thanks @phil.bobo, tried that but then I get an exception as soon as I try to use the parameters. Turns out that the definition of the visible macro in basicmacros.h that we were using is still defined as

#define visible extern "C" __declspec(dllexport)

which is appropriate for functions returning double, but when returning Variant the function needs to have C++ linkage.

Replacing "visible" with "__declspec(dllexport)" as per your example solved the problem.

Message 4 of 4

anthony_johnsonT83CM
Autodesk
Autodesk

Mischa, you are correct and this is by design. Dll functions that use the old method of returning a double should always be declared as visible double MyFunc(FLEXSIMINTERFACE) whereas functions that use the new method of returning a Variant should use __declspec(dllexport) Variant MyFunc(FLEXSIMINTERFACE). FlexSim detects whether the function is exported as a name-mangled c++ function (__declspec(dllexport)) or as a simple c function (visible). If it is the former, FlexSim assumes the function returns a Variant. If it is the latter, FlexSim assumes the function returns a double.