Help with Strings...

Monkey Targets Forums/User Targets/Help with Strings...

SLotman(Posted 2015) [#1]
I'm trying here to create a DX9 target, and now I'm stuck :/

Trying to load a image into memory, I need to convert the string passed from Monkey into a LPCWSTR. What I'm doing, is basically what's done in lots of Monkey targets:

But it is always failing to find the file. Debugging this, when I start the function, path appears as monkey://data/mojo_font.png (and after that a bunch of random symbols)

After calling PathToFilePath, it turns into ./data/mojo_font.png (and still, lots of random symbols after - but apparently in both cases the string size is correct)

Then, when I try to convert it to LPCWSTR, everything breaks, and "filename" gets just a bunch of symbols.

Am I doing something incredible stupid? Can anyone help me with this?


SLotman(Posted 2015) [#2]
That is really strange. Even if I do:



filename is still a bunch of crazy symbols - but the weird part is that the file actually loads! O_o


SLotman(Posted 2015) [#3]
Ok, this makes NO sense, at all for me.



With bbPrint(path), it loads the texture! If I comment it out, it doesn't find the file...?!


ImmutableOctet(SKNG)(Posted 2015) [#4]
Oh, you're misunderstanding the current 'ToCString' setup. The standard 'String::CString' class is generally used as a stack-based container that wraps a separate heap allocation. Basically, when the 'CString' gets popped off the stack, it destructs, and the data it reallocated on the heap is destroyed. SKN3's old 'imagebuffer' module had this same problem. Basically, the current setup lets you use it like a raw C-string, then it gets rid of the temporary buffer automatically. This means you'll need to allocate a 'String::CString' on the stack (Or heap, but that's less favorable) first.

Basically, you need to use it as a temporary 'String::CString':



That should fix it.


SLotman(Posted 2015) [#5]
I think I understand it... the path variable was self destructing after I used ToCString, so I was actually passing garbage (or a NULL) to 'filename'.
Your fix worked indeed, thank you so much for helping out!!!