I use jl_new_bits
to box the pointer in the correct type:
jl_value_t* wchar_dt = jl_get_global(jl_base_module, jl_symbol("Cwchar_t"));
jl_value_t* ptr_dt = jl_apply_type((jl_value_t*)jl_pointer_type, jl_svec1(wchar_dt));
jl_value_t* ptr = jl_new_bits(ptr_dt, (void*)&x); // Boxed Ptr{Cwchar_t} to pass to Julia, x is the C pointer
This is pieced together a bit from different places around CxxWrap.jl, so not tested in the form above (and is missing GC rooting, and is v0.5-only). The objective is also different, since this code is used to make wrapping C++ functions returning or consuming std::wstrings, so if you’re using the embedding interface directly I fully support @stevengj 's advice to do as much as possible in Julia (obviously way simpler, too).