Question: I am learning emscripten, and I can't even get the most basic string manipulation working, when passing strings between C++ and JS.

For example, I would like to write a string length function. In C++:

```
extern "C" int stringLen(std::string p)
{
    return p.length();
}

```

Called from javascript as:

```
var len = _stringLen("hi.");

```

This yields `0` for me. How do I make this work as expected? Which string type should I use here? `char const*`? `std::wstring`? `std::string`? None seem to work; I always get pretty random values.

This is only the beginning... How do I then *return* a string from C++ like this?

```
extern "C" char *stringTest()
{
    return "...";
}

```

And in JS:

```
var str = _stringTest();

```

Again, I cannot find a way to make this work; I always get garbage in JS.

So my question is clearly: How do I marshal string types between JS and C++ via Emscripten?

Answer: 
If you use extern "C" with a function, you cannot use C++ types in it's signature.

So if you want to use std::string, then you can use "Embind" or "WebIDL Binder". Refer [here](https://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#binding-c-and-javascript-webidl-binder-and-embind) 

I prefer embind, so this is a sample code for your problem.

P.S I am not sure how to pass variables by reference here, so doing it by value.

```
// This is your routine C++ code
size_t MyStrLen(std::string inStr) {
    return inStr.length();
}

// This is the extra code you need to write to expose your function to JS
EMSCRIPTEN_BINDINGS(my_module) {
    function("MyStrLen", &MyStrLen);
}

```

Now in JS all you need to do is:

```
var myStr = "TestString";
Module.MyStrLen(myStr);

```

Make sure you pass the flag 

> 
> --bind
> 
> 
> 

when calling emcc.

There is [another approach](https://github.com/kripken/emscripten/issues/4131) where you can do a Malloc on the C++ heap from JS and then do manipulations, but the above approach should be easier.
A few thoughts:

1. The only way I have called a method is by using `crwap` or
`ccall`?  
`var length = Module.ccall('stringLen', ['string'], 'number');`
2. Are you including `stringLen` and `stringTest` in your `EXPORTED_FUNCTIONS` parameter?  
`emcc hello_world.cpp ... -s EXPORTED_FUNCTIONS=['_stringLen','_stringTest']`

Take a look here for more details:  

<http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html>

Or my hello\_world tutorial:  

<http://www.brightdigit.com/hello-emscripten/>

Hopefully that helps.