Hi all,

I have a console application, and I want to run it though a GUI. Its a simple GUI. Say there is a button called "Run", when I click the button need to execute the application do the task. Can you guys guide me to do it. I'm really new for C++ GUI developing.

Thanks,

Hi all,

I want to learn how to develop C++ GUI from the basis. So what are the resources(books, tutorials, etc). I use Microsoft Visual Studio .Net 2003.

Thanks.

Dani AI

Generated

For : a practical path you can use immediately in VS .NET 2003 is to have the GUI start the console EXE and (optionally) capture its text output. Native C++ commonly uses the Windows API (CreatePipe + CreateProcess) so you get full control; managed approaches expose simpler helpers (useful if you adopt managed C++ later), and cross‑platform toolkits like Qt provide their own easy wrappers. , and already pointed to those general options — below is a concrete, minimal native pattern you can drop into a button handler and then run on a worker thread so the UI stays responsive.

#include <windows.h>
#include <string>
#include <vector>

std::string RunConsoleAndCapture(const char* cmdline)
{
    SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, TRUE };
    HANDLE hRead = NULL, hWrite = NULL;
    CreatePipe(&hRead, &hWrite, &sa, 0);
    SetHandleInformation(hRead, HANDLE_FLAG_INHERIT, 0); // child inherits only hWrite

    STARTUPINFOA si = {0}; si.cb = sizeof(si);
    si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.hStdOutput = hWrite; si.hStdError = hWrite;
    si.wShowWindow = SW_HIDE;

    PROCESS_INFORMATION pi = {0};
    std::vector<char> cmd(cmdline, cmdline + strlen(cmdline) + 1);
    if (!CreateProcessA(NULL, cmd.data(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
        CloseHandle(hWrite); CloseHandle(hRead); return "";
    }
    CloseHandle(hWrite);

    std::string output; CHAR buf[4096]; DWORD n;
    while (ReadFile(hRead, buf, sizeof(buf), &n, NULL) && n > 0)
        output.append(buf, buf + n);

    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess); CloseHandle(pi.hThread); CloseHandle(hRead);
    return output;
}

Notes and quick troubleshooting:

  • Run the capture function on a worker thread (or use asynchronous I/O); doing it on the UI thread will freeze the window.
  • If the child expects stdin, create and write to an input pipe too.
  • Remember to pass a writable buffer to CreateProcess (it may modify the command line).
  • To just launch and forget (no capture), a simpler call is suitable; no pipes needed.
  • If you later move to managed code, the .NET process helpers simplify redirection; Qt’s QProcess does the same for cross‑platform apps.

This gives you a working, explicit approach for VS.NET 2003 native C++. Use it to prototype, then pick the higher‑level API (managed Process or a toolkit) if you want simpler code.

Recommended Answers

All 4 Replies

To reply to the "tutorial" suggestion... um, I wouldn't suggest "plain WIN32 API" anymore as a good way to learn/develop GUIs. It's outdated and clunky to use, albeit its a good "low-level" way of understanding GUIs.

Visual C++ 2005 is free and easy to use. It combines the visual ease of development of "visual-basic-style" development with the underlying power and speed of C++. I don't know if that holds true in VC2003.


For the "run a console application" question, if you use the .net framework ; there is a helper object that may prove useful:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.process_members.aspx

You build such an object, initialize it with the application name you want to launch, then you read the output or the error messages.

but... do that kind of trick only when your basic GUI framework is built and you have more experience with objects.

Thanks for all replays. Actually I'm looking for Visual Studio .Net related materials.

i don't know how easy it to do this with Visual Studio and MFC...{from posting in this forum i am guessing that you don't do c++/cli...}

I had a simular problem for a university project and i ended up using qt framework... They have a free version and it is very easy to do what you asked i.e. run a console application through a gui interface...

PS:: qt also has a very supporting community check out this forum and an excellent tutorial...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.