Passing string from D to c++

Adam D. Ruppe destructionator at gmail.com
Tue Jul 30 13:23:29 PDT 2013


On Tuesday, 30 July 2013 at 20:09:01 UTC, Milvakili wrote:
> So I need to pass them as char*, I can not pass them as string?

I would pass it as a char* and a size_t for length.

extern(C++) void cpp_func(char* strptr, size_t strlen);


void main() {
    string foo = "test";
    cpp_func(foo.ptr, foo.length);
}


It'd be important on the C++ side to remember that it is NOT zero 
terminated, and always use that length parameter instead of 
normal C functions.

But that'd give the most efficiency, since that's using the same 
representation as D itself.


More information about the Digitalmars-d mailing list