How to formulate a pointer to a string constant in a C++ service

Last Updated: Release 4.2

Issue/Question

What is the correct way to formulate a pointer to a string constant using the PureWeb C++ API?

Below is the relevant snippet from my code, but it seems a little clunky to loop over a string to extract the characters one by one.

Is there a simpler way to do this?

void OnDialogValueChanged1(CSI::ValueChangedEventArgs args)
{
    CSI::Nullable<CSI::String> const& newValue = args.NewValue();
    if (newValue.HasValue())
    {
        CSI::String const& value = newValue.ValueByRef();
        CSI::SizeType size = value.Length();
        char* str = new char[size+1];
        for (int i=0; i<size; i++) str[i]=value[i];
        str[size]=0;
        PW_AcceptXML("Dialog", str);
        delete[] str; str = 0;
    }
}

Solution

To get a pointer to a constant character, you can do this:

someCSIString.ToAscii().begin();

Using the code you provided, it would look something like this:

void OnDialogValueChanged1(CSI::ValueChangedEventArgs args)
{
    CSI::Nullable<CSI::String> const& newValue = args.NewValue();
    if (newValue.HasValue())
    {
        CSI::String const& value = newValue.ValueByRef(); 
        const char* str = value.ToAscii().begin(); 
        PW_AcceptXML("Dialog", str); 
    }
}

 

March 21, 2016