CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2014
    Posts
    31

    ShellExec - view output

    I have this functions that executes DOS commands in the specified command line and git function in the specified command line.
    I want to view the the output of the commands however the windows DOS shell opens and close to fast for me to do that.

    I can't find a way to make the windows DOS shell window stay open.

    I can't find a way to redirect the command output to a text file that I can read outside my MFC app (DOS redirect >> does not work or at least I can't find the out file)

    And I can open a permanent DOS shell window but I can't find a way to re-direct all subsequent command lines to it rather than them being executed in their own individual DOS shells that open and close too fast.

    Any suggestions folks?

    Code:
    bool CCmdLine::ExecCon(CWnd* pParent, LPCSTR lpszCmdLine)
    {
    	HINSTANCE hResult = ShellExecute(pParent->GetSafeHwnd(), "open", "cmd.exe", lpszCmdLine + m_strRedir, m_strWorkingFolder, SW_SHOWNORMAL);
    
    	if ((int)hResult < 33)
    		AfxMessageBox(GetErrorMessage(hResult), MB_OK | MB_ICONERROR);
    	return (int)hResult > 32;
    }
    bool CCmdLine::ExecGit(CWnd* pParent, LPCSTR lpszCmdLine)
    {
    	HINSTANCE hResult = ShellExecute(pParent->GetSafeHwnd(), "open", "git.exe ", lpszCmdLine + m_strRedir, m_strWorkingFolder, SW_SHOWNORMAL);
    
    	if ((int)hResult < 33)
    		AfxMessageBox(GetErrorMessage(hResult), MB_OK | MB_ICONERROR);
    	return (int)hResult > 32;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: ShellExec - view output

    Code:
    lpszCmdLine + m_strRedir
    Assuming m_strRedir is of type char*, then you can't concatenate strings of this type like this. With c++, you need something like
    Code:
    auto cmdline = std::string(lpszCmdLine) + m_strRedir;
    
    HINSTANCE hResult = ShellExecute(pParent->GetSafeHwnd(), "open", "cmd.exe", cmdline.c_str(), m_strWorkingFolder, SW_SHOWNORMAL);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2014
    Posts
    31

    Re: ShellExec - view output

    Quote Originally Posted by 2kaud View Post
    m_strRedir is of type char*, then you can't concatenate strings of this type like this.
    m_strRedir is a CString - concatenates as expected.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: ShellExec - view output

    Also, if you want to execute a command at the command prompt using cmd.exe and keep the console open after, then you need to use the /K option for cmd which has to be specified with the command and not as part of cmd.exe.

    Consider
    Code:
    auto cmdline = "/K " + std::string(lpszCmdLine) + m_strRedir;
    
    HINSTANCE hResult = ShellExecute(pParent->GetSafeHwnd(), "open", "cmd.exe", cmdline.c_str(), m_strWorkingFolder, SW_SHOWNORMAL);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured