0

I am launching a c program from a Custom uRL Protocol.

Custom URL Protocols urlencode all args and combine them into one string. No way around this.

This is my Windows only attempt but it crashes. getopt_long doesn't like my newly created argv.

    bool main_init(int argc, char *argv[])
    {
        // if url protocol
        if (strstr(argv[1], "protocol:") != NULL)
        {
            //decode and remove protocol.
            char *decoded = url_decode(argv[1])+9;
            // wrap " around original argv[0] and prepend to argv[1]
            LPSTR buf[1024];
            strcpy(buf, "\"");
            strcat(buf, argv[0]);
            strcat(buf, "\" ");
            strcat(buf, decoded);
            // ANSi version of CommandLineToArgvW: http://alter.org.ua/docs/win/args/
            argv = CommandLineToArgvA(buf, &argc);
            // argv appears correct at this point
        }
// protocol causes crash here
        int c = getopt_long(argc, argv, optstring, opts, NULL);
    }

Thanks.

jenry
  • 1
  • 1
  • 2
    You need to ask a *question*. Right now all we see is a *problem*, with no apparent effort to actually *solve* it. – WhozCraig Jan 22 '17 at 07:58
  • 1
    What he/she wants is not possible anyway, as argv will not be large enough to hold the extra strings. – Freek Wiedijk Jan 22 '17 at 08:38
  • A pity you're on windows,. On an IXish system you could just have wrapped the call to your program into a script which had `sed`ed away all those `%20` by replacing them with a single blank. – alk Jan 22 '17 at 09:57
  • I updated my question and added my code attempt that fails. – jenry Jan 22 '17 at 18:41
  • 1
    I don't know whether this has anything to do with your problem, but shouldn't the second argument of `CommandLineToArgvW` be `&myargc`? Else the types are wrong. Do you compile with compiler warnings enabled? – Freek Wiedijk Jan 22 '17 at 20:01
  • Thanks. That got me most of the way there. Now I just need to convert LPWSTR * to char * – jenry Jan 22 '17 at 22:22
  • Freek Wiedijk your suggestion got me farther but it is still crashing. I edited my question with my progress. – jenry Jan 22 '17 at 23:20
  • The `CommandLineToArgvA` implementation looks totally broken. `argv = (PCHAR*)GlobalAlloc(GMEM_FIXED, i + (len+2)*sizeof(CHAR));` A rookie mistake. `argv` is an array of pointers, not of characters. I would steer clear of that piece of code. – n. m. could be an AI Jan 22 '17 at 23:50
  • I ended up starting over and just using strtok to split it instead of trying to use CommandLineToArgvW. It works for me. Thanks for the help. – jenry Jan 23 '17 at 00:08
  • The code from `http://alter.org.ua/docs/win/args/` is **not at all** correct, you can test it with, for example, `1 "\"23ab """ @"4 56`; it produces a different result from Microsoft's `CommandLineToArgvW`. – Ken Kin Jan 18 '18 at 11:10

0 Answers0