简体   繁体   中英

Pass string from C++ to JS using emscripten

I am trying to send string from C++ to JS using emscripten but I'm not able to convert it appropriately in JS.

C++

    EMSCRIPTEN_KEEPALIVE const char* accessDetails()
    {
        return func().c_str();
    }

func returns std::string.

I'm getting some garbage value number . How can I get the string in JS?
Thanks in advance.

When calling a raw WebAssembly function like that you only basic types are supported. In this case you are returned a pointer which is just a number (pointer are number in JS just like they are in C/C++). You can use that pointer to read bytes out of WebAssembly memory and produce a JS string from them using, for example, UTF8ToString(number) , or you can use one of the higher level binding systems such as embind to take care of it for you.

See https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html for more information.

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