Query on wxShell and wxExecute

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Query on wxShell and wxExecute

Post by Priya »

Hi

I am using wxExecute. This is not working for system commands. so i tried with wxShell, it is working but the shell pop up is flashing and i cant redirect the output.

in wx/utils.h for wxShell two options are there, which can store the output in wxArrayString. when i use this am getting link error. How to solve this.

can i use wxExecute for system commands like cd, dir, mkdir.. ect??

if i can use wxShell means is it possible to use hiding the console and redirect the output in wxTextcttrl??

Code: Select all

wxString input;
wxArrayString output;
wxShell(input, output1);
Please help.

Thanks and Regards,
Priya
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Query on wxShell and wxExecute

Post by PB »

Priya wrote:can i use wxExecute for system commands like cd, dir, mkdir.. ect??
I don't think so, this is for actual executables that exist as files. The commands you listed are internal shell commands

I am not sure what you are trying to achieve. Assuming you cannot use the wxWidgets-provided equivalents of those shell commands (e.g. wxFileName::MkDir()), I would probably always generate a .bat file with shell command(s) required and captured the result of its execution with wxExecute(mybatch.bat,....).
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Re: Query on wxShell and wxExecute

Post by Priya »

Hi,

I actually wanted a wxTextCtrl to replicate a shell window. User should use this replication as shell window(cmd.exe).

using wxStreamToTextRedirector i can achieve some commands like ipconfig passing thru wxExecute am able to get the output in wxArrayString and able to redirect to wxTextCtrl.

My problem is some system commands are not working.

so can i use wxShell(const wxString& command, wxArrayString& output) for with out original console(black window) flashing and redirecting the output to wxTextCtrl ??

Regards,
Priya
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Query on wxShell and wxExecute

Post by PB »

Did you actually read my previous messasge? Do you understand the difference between programs that can be created as processes and wxExecute()d (such as ipconfig.exe) and shell commands (such as dir or cd)?

Where did you find declaration of wxShell(const wxString& command, wxArrayString& output)? I cannot find such thing, seeing only bool wxShell (const wxString &).

As I wrote above, I believe that for shell commands you can do something like this (all error checking missing here)

Code: Select all

#include <wx/wx.h>
#include <wx/filename.h>
#include <wx/stdpaths.h>
#include <wx/ffile.h>


class MyFrame : public wxFrame
{
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, "Test")
    {
        wxLog::SetActiveTarget(new wxLogTextCtrl(new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2)));
        wxLog::SetTimestamp("");

        wxFileName fileName(wxStandardPaths::Get().GetTempDir(), "mybatch.bat");
        wxFFile file(fileName.GetFullPath(), "w+t");        

        file.Write("dir c:\\*.* /oN");        
        file.Close();

        wxArrayString output;
        long resultCode = wxExecute(fileName.GetFullPath(), output);

        wxLogMessage("'%s' finished with code %d:", fileName.GetFullPath(), resultCode);
        for ( size_t i = 0; i < output.size(); i++ )
            wxLogMessage(output[i]);

        wxRemoveFile(fileName.GetFullPath());
    }
};


class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {     
        (new MyFrame())->Show();        
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);
But I may be wrong...
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Re: Query on wxShell and wxExecute

Post by Priya »

Thank you PB

I read the post and understood.. even i am using the same way how u said.
But my doubt is,
If i am using wxShell - a black window pops and displays the result and get close automatically.
I dont want this behaviour.

I wanted to implement - command should execute and the result should be captured in output string array, which i can redirect to my control.
is that achievable using wxShell?? this is my doubt.

I found that declaration in
\include\wx\utils.h - am using wxwidgets 3.1.0 version
in the above file i can see the following declarations

Code: Select all

// Execute a command in an interactive shell window (always synchronously)
// If no command then just the shell
WXDLLIMPEXP_BASE bool wxShell(const wxString& command = wxEmptyString);

// As wxShell(), but must give a (non interactive) command and its output will
// be returned in output array
WXDLLIMPEXP_BASE bool wxShell(const wxString& command, wxArrayString& output);
But the second declaration if i use am getting a link error. Why i am getting this link error. Is that declaration is not valid??

Regards,
Priya
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Query on wxShell and wxExecute

Post by PB »

Priya wrote:I found that declaration in
\include\wx\utils.h - am using wxwidgets 3.1.0 version
in the above file i can see the following declarations

Code: Select all

// Execute a command in an interactive shell window (always synchronously)
// If no command then just the shell
WXDLLIMPEXP_BASE bool wxShell(const wxString& command = wxEmptyString);

// As wxShell(), but must give a (non interactive) command and its output will
// be returned in output array
WXDLLIMPEXP_BASE bool wxShell(const wxString& command, wxArrayString& output);
But the second declaration if i use am getting a link error. Why i am getting this link error. Is that declaration is not valid??

Regards,
Priya
The second declaration is not documented. The reason you get a link error is because it is declared for all platforms but not defined for MSW (src/msw/util.cpp), it seems to be defined for unix (src/unix/utilsunx.cpp), perhaps it easy to implement there due to different nature of the Unix shell?

BTW, you can execute cmd.exe with a /c parameter which executes the command and quits (there are also few more useful switches, see cmd /?). You can e.g. call

Code: Select all

wxExecute("cmd.exe /cdir c:\\ /oN", output)
to capture the output of dir c:\ /oN.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Query on wxShell and wxExecute

Post by ONEEYEMAN »

Hi,
What is an exact command you want to pass to wxExecute/wxShell?

A lot of thing has their equivalent in wxWidgets code and it means you don't need to re-invent the wheel.

Thank you.
Priya
Knows some wx things
Knows some wx things
Posts: 43
Joined: Fri Apr 07, 2017 8:38 am

Re: Query on wxShell and wxExecute

Post by Priya »

Thank you PB

Code: Select all

wxExecute("cmd.exe /cdir c:\\ /oN", output)
I tried that, i am able to redirect to my control. I will try for other user commands also with this approach.
For linux can i use the wxShell declaration??

@ ONEEYEMAN

Actually i am trying to replicate cmd.exe(in windows)/ Terminal(in Linux) in a UI based application as a feature. From my application window itself user should able to use send command and receive result. Am using a wxTextCtrl and wxStreamToTextRedirector to redirect the console output to the text control.

So my basic aim is if my app is running, user need not to launch a cmd.exe or go to terminal to send command (all commands like system commands For example : dir, mkdir, cd folderName, del fileName .. ect) and what ever output comes i need to redirect to my control.

is this possible?? :?:

Please help me

Regards,
Priya
Post Reply