Escaping a double Quotes in ShellExecute

In Below code I am using ShellExecute(), I need to pass a URL into it but the two GET param of URL like RangeFromDate and RangeToDate values should be in Double quotes.

I need to escape the double quotes. I am using "/ RangeFromDate "/ but its not working.

_SECTION_BEGIN("Volume");

bardate = DateTimeToStr( SelectedValue(DateTime()), 4) ;

RangeFromDate = DateTimeToStr( BeginValue(DateTime()), 4) ;
RangeToDate = DateTimeToStr( EndValue(DateTime()),  4) ;



GuiButton( "Analyse", 1, 2, 17, 100, 22, 1 );
GuiSetColors( 1, 1, 0 ); // default (system) look
id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );
if  ( id == 1 && event == 1 )
{
ShellExecute("firefox.exe", "example.com?datefrom=" + RangeFromDate + "/"&rangeto="/" + RangeToDate, "");
}

_SECTION_END();

What mistake am I doing?

You have to use backslash for excaping.

_SECTION_BEGIN("Volume");
EnableTextOutput( 0 );

dt = DateTime();

bardate = DateTimeToStr( SelectedValue(dt), mode = 4);
RangeFromDate = DateTimeToStr( BeginValue(dt), mode);
RangeToDate = DateTimeToStr( EndValue(dt), mode);

GuiButton( "Analyse", 1, 2, 17, 100, 22, 1 );
GuiSetColors( 1, 1, 0 ); // default (system) look
id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );

url = "example.com?datefrom=\"" + RangeFromDate + "\"&rangeto=\"" + RangeToDate + "\"";
printf( url );

if( id == 1 && event == 1 ) 
{
    ShellExecute( "firefox.exe", url, "" );
}
_SECTION_END();

Is this one the url you are looking for?

3 Likes

The above web page doesn’t seem to be working at the moment (even http://example.com/ isn’t working right now), but I think the string being displayed in the @fxshrat’s interpretation window is exactly what @Neil81 was asking for.

Some additional information about “backslash for escaping”:

Wikipedia
Softwareengineering
Stackoverflow

@fxshrat @Milosz Thankyou Seniors!!!

When the URL opens in the firefox browser I do not see the Double Quotes being passed on to the browser. Here is what I see:

The issue here is you can't use quotation marks in a URL, you need to send %22 instead.

Ref: https://bytes.com/topic/html-css/answers/96296-double-quotation-mark-within-url

However, there's still one more issue. Since, to get the % mark into the text, you need to escape it with another % mark, eg:

url = "example.com?datefrom=%%22" + RangeFromDate + "%%22&rangeto=%%22" + RangeToDate + "%%22";

which produces the correct text:

However, when it gets to the browser it has already converted the %22 to a quotation mark, and left the leading % in.

Anyone else know how to avoid that implicit translation?

Well, it's easy... simply "fool" AFL engine later one via StrReplace().

_SECTION_BEGIN("Volume");
EnableTextOutput( 0 );

dt = DateTime();

bardate = DateTimeToStr( SelectedValue(dt), mode = 4);
RangeFromDate = DateTimeToStr( BeginValue(dt), mode);
RangeToDate = DateTimeToStr( EndValue(dt), mode);

GuiButton( "Analyse", 1, 2, 17, 100, 22, 1 );
GuiSetColors( 1, 1, 0 ); // default (system) look
id = GuiGetEvent( 0, 0 );
event = GuiGetEvent( 0, 1 );

url = "example.com?datefrom=%%22" + RangeFromDate + "%%22&rangeto=%%22" + RangeToDate + "%%22";
printf( url );

if( id == 1 && event == 1 ) 
{
    ShellExecute( "firefox.exe", StrReplace(url, "%%", "%"), "" );
}
_SECTION_END();

Result in Internet browser

5 Likes

It is not really the case. You do NOT need to escape % in normal string. You only need to escape % in the first argument of StrFormat()/printf(), because there and only there % is treated as field formatting sequence. But in normal string, it is not.

You were having wrong impression because you have used this:

printf( url ); // first arg is a formatting sequence, % has special meaning

And then first parameter is used as formatting sequence.
But if you used:

printf( "%s", url ); // here url is NOT 1st arg and % does not have special meaning

You would not need to do anything with % signs in the url. Note: %s sequence used above is supported only in versions 6.17 or higher.

So all you really need to do is this:

RangeFromDate = "2015-01-02";
RangeToDate = "2017-02-03";

// proper web URL begins with http(s)://
url = "http://example.com?datefrom=%22" +
       RangeFromDate + 
       "%22&rangeto=%22" + 
       RangeToDate + 
       "%22";
printf( "%s", url ); // for debugging only (can be removed)

ShellExecute( url, "", "" ); // don't need to specify browser if http is used

No StrReplace() to handle % is needed.

9 Likes

Thank you all the Seniors.

@Tomasz @Milosz @fxshrat @HelixTrader

2 Likes

4 posts were split to a new topic: Using range selector and BeginValue/EndValue