dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: ShellExecute problem

  1. #1
    Join Date
    Jan 2011
    Posts
    101

    ShellExecute problem

    I have some app that i need to call and pass some params to it. I tried this with cmd and it worked but somehow ShellExecute doesn't pass params to my app. I can't see where is my problem:

    Code:
    strExe = TEXT("C:\\Users\\borko1980\\Desktop\\app\\app.exe");
    params = TEXT("solve < \"C:\\Users\\borko1980\\Desktop\\TESTS\\file.in\" > \"C:\\Users\\borko1980\\Desktop\\TESTS\\file.out\"");
    if(32 >= (int)ShellExecute(hWnd, TEXT("open"), strExe.c_str(), params.c_str(), NULL, SW_SHOWNORMAL))
    {
        //handle error.
    }
    Path and file names are valid!!! I tried it with cmd and it works, but with this my app is started and expects inout but it should grab it from file as it was specified by params, but obviously not passed at all.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: ShellExecute problem

    Have you tried setting a breakpoint in the called application and check what you get?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jan 2011
    Posts
    101

    Re: ShellExecute problem

    Yes, i was doing that for the past couple of hours.
    At first i was reading path/file names from list-box control and passing that, and it wasn't working. So i deicided to hard-code this strings to confirm if i am crazy or not, but no avail.
    It's obviously something stupid but i can't see what?

    To repeat: this works in cmd prompt!!!

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: ShellExecute problem

    What do you get as params when you start the application using ShellExecute?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jan 2011
    Posts
    101

    Re: ShellExecute problem

    I don't have source for that console app, so i created my own to display params, and it works:
    Code:
    int main(int carg, char* args[])
    {
    	for(int i = 0; i < carg; ++i)
    	{
    		std::cout << args[i] << std::endl;
    	}
    
    	std::cout << "Press enter to exit...";
    	std::cin.ignore( std::numeric_limits< std::streamsize >::max(), '\n' );
    	return 0;
    }
    But now i am confused as why other app didn't run, but works with same params in cmd prompt?

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: ShellExecute problem

    That's what I suspected. Create a bat file and run that instead of running the application directly. The < > has to be resolved by the shell, not by the called application.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Jan 2011
    Posts
    101

    Re: ShellExecute problem

    Now i have another problem. Because i use tstring, and write "bat" file with:
    Code:
    #ifdef UNICODE
    #define tfopen _wfopen
    #else 
    #define tfopen fopen
    #endif
    ...
    typedef std::basic_string<TCHAR> tstring;
    ...
    FILE* f = tfopen(TEXT("command.bat"), TEXT("w"));
    tstring commands;
    commands = ....// fill this
    fwrite(commands.c_str(), sizeof(TCHAR), commands.size(), f);
    and it want work unless i re-saved it as ANSI text file. Do i have to write BOM? Does bat files even support Unicode chars when reading?

    Thanks for your time.

    EDIT: Tried to write BOM at begining ("\xfffe"), but it doesn't work either.
    Last edited by borko1980; February 11th, 2012 at 01:19 PM.

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: ShellExecute problem

    I don't think that the shell is unicode aware. Maybe power shell is but I would go for the safe solution and use ASCII (i.e. stay away from strange characters like the swedish &#229;&#228;&#246
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  9. #9
    Join Date
    Jan 2011
    Posts
    101

    Re: ShellExecute problem

    Quote Originally Posted by S_M_A View Post
    ...I would go for the safe solution and use ASCII
    But the problem is i must pass correct path/file names, witch other users might have on their computers with "funny" characters when they run my app.

    Thanks for your time.

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: ShellExecute problem

    If so I guess that those characters are supported by their shell. After all the shell codepage will reflect the language used in the OS. My shell codepage is 437 to enable swedish characters (even though I run an English OS. I can't stand the swedish version)
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  11. #11
    Join Date
    Jan 2011
    Posts
    101

    Re: ShellExecute problem

    Is there any function that you could recommend me to convert those strings into coresponding ANSI/ASCII version with proper code page and what not that it depends on (Unicode is HORROR) to do the right thing?
    Everybody should use english! :-)

    Thanks for your time.

  12. #12
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: ShellExecute problem

    You can use for instance MultiByteToWideChar or WideCharToMultiByte depending on what conversion you need. http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx & http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    GetConsoleCP might also be needed http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Last edited by S_M_A; February 11th, 2012 at 01:49 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  13. #13
    Join Date
    Jan 2011
    Posts
    101

    Re: ShellExecute problem

    Thanks. I used this function WideCharToMultiByte and it works now.

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,603

    Re: ShellExecute problem

    solve < \"C:\\Users\\borko1980\\Desktop\\TESTS\\file.in\" > \"C:\\Users\\borko1980\\Desktop\\TESTS\\file.out\"
    This kind of processing alternative input/output must be done by CreateProcess provided with STARTUPINFO properly set up.
    Code:
    typedef struct _STARTUPINFO {
      DWORD  cb;
      LPTSTR lpReserved;
      LPTSTR lpDesktop;
      LPTSTR lpTitle;
      DWORD  dwX;
      DWORD  dwY;
      DWORD  dwXSize;
      DWORD  dwYSize;
      DWORD  dwXCountChars;
      DWORD  dwYCountChars;
      DWORD  dwFillAttribute;
      DWORD  dwFlags;
      WORD   wShowWindow;
      WORD   cbReserved2;
      LPBYTE lpReserved2;
      HANDLE hStdInput;
      HANDLE hStdOutput;
      HANDLE hStdError;
    } STARTUPINFO, *LPSTARTUPINFO;
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)