Proper use of `CastCharStar.fromString`

Hi all,

I am trying to get a char * equivalent from Haxe and I have found in the past that CastCharStar.fromString generally works. However, I’m not sure when this changed, but in some version of Haxe/hxcpp the generated code is now const HX_CHAR * even when using the above code.

Here is an example:

var value = "";
folder = CastCharStar.fromString(value);

You would expect this to work considering folder is type CastCharStar but it doesn’t. Instead, I get the following compilation error: 'initializing': cannot convert from 'const HX_CHAR *' to 'char *'

Does anyone know what method is best to use?

Haxe version is 3.4.4 and hxcpp version is 4.0.4.

Thanks.

Hi,
you probably need to use ConstCharStar intead of CastCharStar.
Or maybe Star<Char> if you really need char *.

Well, I’m trying to use the linc_imgui library and I am having trouble with the function call for inputText. I looked at the extern definition and it was saying it required Array<Char> and then takes the length of the first part of the Array which made no sense to me. I also couldn’t find a way to convert a Haxe string to a Array<Char> without making wrapper functions to convert them. I tried that, but it didn’t work as I thought it wouldn’t.

I changed the function definition to take a CastCharStar because that’s the only class I know that takes the char * equivalent and has the functionality to convert from a Haxe string to itself. I’ve looked at the definition for Star<Char> but that’s a typedef to the type itself.

Just so you know, the InputText function takes a char * as it’s a reference, and ConstCharStar will therefore not work.

I would be more than interested to see if there is a way to make Array<Char> work properly with native Haxe strings and I will revert all the changes I made to the library.

Thanks.

If this is the library you need it does indeed take a Array<Char> which is an haxe array of cpp.Char which is just an int.

The implementation simply takes the address of the first element which is the same as the address of a char[] in c++, and also pass the array length as usual for c code.

So something like ImGui.inputText("label", "hello world".split("").map(function (e) return e.charCodeAt(0)) should work. Might need a cast to Char somewhere since array variance.

edit: you can make an helper function and use using to add it to strings Try Haxe !

I don’t think, that externs a written properly. At least it is not worked for me too with Array<cpp.Char>. Nothing showed, nothing can be inputted.
Here is a workaround which can be implemented as an extern or as some kind of wrapper:

    private var inputString:String = "";
    private function drawTestWindow() {
        ImGui.begin("Window title");

        untyped __cpp__('
            static char testBuffer[{0}] = {};
            ImGui::InputText("test", testBuffer, sizeof(testBuffer) );
            {1} = (String)testBuffer;
        ', 512, inputString);
        
        //lets print it:
        ImGui.text(inputString); 
        ImGui.end();
    }

Hello, library author here!
The intended use is to pre-allocate an array for use with the imgui text input controls. For example this:

var buf : Array<cpp.Char> = [ for (i in 0...16) 0x0 ];

Will create an array of 16 null characters which will allow you to type up to 16 characters into the text box. You would then pass buf to the input text function and it should work.
For my own projects I have a little text buffer class which handles converting to and from haxe strings and character arrays.

Maybe a little more info on the top in this issue comment.

2 Likes