First, I’ve found a bug in the implementation in modules/monkey/types.monkey2 line 217. ToCString should take two parameters, not one – as defined in the native .cpp file (I already submitted the bug report and fixed it/rebuild modules here locally)
However, I still can’t get it to work as expected and I get hit with a memory access violation.
Here is what I am trying to do:
I’m using an extern to a .c source file, which contains:
Literal c/c++ strings like this are stored in ‘read only’ memory and shouldn’t be written to.
Try this instead…
char dev_name[256];
dev_name will intially be ’empty’ but that’s OK because you’re using ToCString to initialize it before you use it (I hope).
The alternative is to pass the dev_name directly to native code, eg:
Monkey
1
2
3
4
5
6
7
8
9
10
11
12
//c++
voidopen_dev(constchar*name){
}
' mx2
Extern
Functionopen_dev(name:CString)
This will automagically convert name to a const char * for you when you call open_dev. However, you should NOT store ‘name’ anywhere inside open_dev for later use as the const char * is temporary and will disappear after open_dev returns.