Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Enigma with ShellExecute ? [modified]

Enigma with ShellExecute ? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpcollaborationquestionannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Souldrift
    wrote on last edited by
    #1

    Hi there, I got the strangest problem. I want to start an executable with ShellExecute(). Which usually works fine, but ... (this is the code)

    [edit]
    char buf[512]
    size = GetCurrentDirectoryA( 512, buf ); // get absolute path
    [/edit]

    // version 1
    char callString[1024];
    sprintf_s( callString, "%s\\%s", buf, file.c_str() );
    // version 2
    char buf[1024];
    sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );

    char id[32];
    sprintf_s( id, "%d", pid );

    long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));

    ... with version 2 things work fine, with version 1 I get ERROR_FILE_NOT_FOUND. callString = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe buf = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe (I copied those strings right out from the variable watch) Does anyone have an idea on this? Souldrift

    modified on Monday, October 5, 2009 5:00 AM

    C 1 Reply Last reply
    0
    • S Souldrift

      Hi there, I got the strangest problem. I want to start an executable with ShellExecute(). Which usually works fine, but ... (this is the code)

      [edit]
      char buf[512]
      size = GetCurrentDirectoryA( 512, buf ); // get absolute path
      [/edit]

      // version 1
      char callString[1024];
      sprintf_s( callString, "%s\\%s", buf, file.c_str() );
      // version 2
      char buf[1024];
      sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );

      char id[32];
      sprintf_s( id, "%d", pid );

      long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));

      ... with version 2 things work fine, with version 1 I get ERROR_FILE_NOT_FOUND. callString = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe buf = c:\Users\zwatschek\work\svn\incubation\ttsServer\Data\ttsWatchdog.exe (I copied those strings right out from the variable watch) Does anyone have an idea on this? Souldrift

      modified on Monday, October 5, 2009 5:00 AM

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      There's something wrong in the posted snippet:

      Souldrift wrote:

      // version 1 char callString[1024]; sprintf_s( callString, "%s\\%s", buf, file.c_str() );

      Here you're using a uninitialised buf variable (as far as I can tell from the posted code...).

      Souldrift wrote:

      har buf[1024]; sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" ); char id[32]; sprintf_s( id, "%d", pid ); long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));

      here you're copying (with a wrong call to sprintf_s - missing len parameter-) the file name to the buf variable, but, afterwards, you're not using buf in ShellExecute. Please check through the posted code. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      S 1 Reply Last reply
      0
      • C CPallini

        There's something wrong in the posted snippet:

        Souldrift wrote:

        // version 1 char callString[1024]; sprintf_s( callString, "%s\\%s", buf, file.c_str() );

        Here you're using a uninitialised buf variable (as far as I can tell from the posted code...).

        Souldrift wrote:

        har buf[1024]; sprintf_s( buf, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" ); char id[32]; sprintf_s( id, "%d", pid ); long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));

        here you're copying (with a wrong call to sprintf_s - missing len parameter-) the file name to the buf variable, but, afterwards, you're not using buf in ShellExecute. Please check through the posted code. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        S Offline
        S Offline
        Souldrift
        wrote on last edited by
        #3

        I edited the code block. buf was initialized, of course, further above. In ShellExecute() I tried 'callString' and 'buf' as parameter. But only 'buf' worked. 'buf' is in the example NOT in the ShellExecute(), since I tried last with 'callString'. hm ... I post the code differently ...

        char buf[512]
        size = GetCurrentDirectoryA( 512, buf ); // get absolute path

        char id[32];
        sprintf_s( id, "%d", pid );

        // version 1
        char callString[1024];
        sprintf_s( callString, "%s\\%s", buf, file.c_str() );
        long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));

        // version 2
        char callString2[1024];
        sprintf_s( callString2, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );
        res=long(ShellExecute(0, "open", callString2, id, 0, SW_SHOWNORMAL));

        And still, the contents of callString and callString2 are the same. Version 2 works, version 1 not. I´ll look into the len parameter of sprintf_s. But I don´t think that´s it. Thanks. Souldrift

        modified on Monday, October 5, 2009 5:26 AM

        C 1 Reply Last reply
        0
        • S Souldrift

          I edited the code block. buf was initialized, of course, further above. In ShellExecute() I tried 'callString' and 'buf' as parameter. But only 'buf' worked. 'buf' is in the example NOT in the ShellExecute(), since I tried last with 'callString'. hm ... I post the code differently ...

          char buf[512]
          size = GetCurrentDirectoryA( 512, buf ); // get absolute path

          char id[32];
          sprintf_s( id, "%d", pid );

          // version 1
          char callString[1024];
          sprintf_s( callString, "%s\\%s", buf, file.c_str() );
          long res=long(ShellExecute(0, "open", callString, id, 0, SW_SHOWNORMAL));

          // version 2
          char callString2[1024];
          sprintf_s( callString2, "c:\\Users\\zwatschek\\work\\svn\\incubation\\ttsServer\\Data\\ttsWatchdog.exe" );
          res=long(ShellExecute(0, "open", callString2, id, 0, SW_SHOWNORMAL));

          And still, the contents of callString and callString2 are the same. Version 2 works, version 1 not. I´ll look into the len parameter of sprintf_s. But I don´t think that´s it. Thanks. Souldrift

          modified on Monday, October 5, 2009 5:26 AM

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          I made a test. Both version 1 and version 2 work fine on my system. Of course I had to provide a value for the file std::str variable (and I used different path and executable). :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S 1 Reply Last reply
          0
          • C CPallini

            I made a test. Both version 1 and version 2 work fine on my system. Of course I had to provide a value for the file std::str variable (and I used different path and executable). :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            S Offline
            S Offline
            Souldrift
            wrote on last edited by
            #5

            God, how I hate programming :mad: Thanks again ...

            S 1 Reply Last reply
            0
            • S Souldrift

              God, how I hate programming :mad: Thanks again ...

              S Offline
              S Offline
              Souldrift
              wrote on last edited by
              #6

              I found it. What a bugger. I got my file name from an absolute path like this

              string path = wdPath.substr( 0, index+1 );
              string file = wdPath.substr( index+1 );

              And though 'file' was displayed correctly in the watch, it seems to have contained some invalid sign. Cause like this ...

              string path = wdPath.substr( 0, index+1 );
              string file = wdPath.substr( index+1, wdPath.size()-index-2 );

              ... it works. Cheers Souldrift

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups