dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Converting Windows App to Console application

  1. #1
    Join Date
    Jul 2007
    Posts
    7

    Converting Windows App to Console application

    I have a c++ program with a gui that I would like to be able to use from a command line with only a select few of the gui functions available at any time when I run it from the command line.

    the app is currently written to totally set up the environment and sense all it has to do, and then wait for one of several buttons to be pushed, it seems it should be possible to either convert it to a console app by adding a setting to devine the _CONSOLE and a tmain function and have that entry taken rather than the normal application entry, init the windows app, and then calling the function to do whatever I want according to parameters, rather than the gui.

    I'd rather not have the gui pop up, but can live with that, since i'll have the completion event exit the application, rather than remain in the app.

    I could mess with the main bits and rig something from the winmain, but would rather not do that.

    any experience in this?

    I recall that older visual studios could create windows applications, console applications, and console applications that could call windows functions, and pop up dialog boxes. I'm looking to sort of do a project similar to the latter, but can't find that in the newfangled visual studio 5 as of yet.

    I can probably scrape out all the crap that does this app and paste it into a console app, but there are threads and all sorts of excess crap, that would be better served if I modified a copy of the windows app, rather than trying to paste it into a project which was created as a console application.

    I saw that the console methods have to be set up and inited, which I assume is the main task to do to get it going.

    thanks for any help.

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Converting Windows App to Console application

    Quote Originally Posted by jws
    I have a c++ program with a gui
    What sort of GUI ? A windows GUI ?
    use from a command line with only a select few of the gui functions available at any time when I run it from the command line.
    Perhaps a little more details on this would help.
    • When you mean a command line support, are you intending to launch the application with different command line parameters ?
    • Does your app have to output data to the console window ?
    • Does your app have to accept input from the console ?

    Which of the above questions are true ?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Converting Windows App to Console application

    Quote Originally Posted by jws
    I have a c++ program with a gui that I would like to be able to use from a command line with only a select few of the gui functions available at any time when I run it from the command line.
    It may be too late for you, but the unwritten rule is to design programs as if their console apps, and then just add the GUI as a wrapper to aid users in using the program.

    In other words, seperate the user interface from the internal logic. For example, the internal functionality of the program shouldn't be tied to the button press logic, or a menu invocation. The button press should call some internal function that executes whatever needs to be executed -- that function shouldn't know it's being called from a button press.
    it seems it should be possible to either convert it to a console app by adding a setting to devine the _CONSOLE and a tmain function and have that entry taken rather than the normal application entry, init the windows app, and then calling the function to do whatever I want according to parameters, rather than the gui.
    I don't think this going to do anything. You can create windows, menus, dialog boxes, etc. in a console app, and you can start a console in a GUI app.

    For example, here is an int main()
    Code:
    #include <windows.h>
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World";
       MessageBox(NULL, "Hello World", "Hello World", MB_OK);
    }
    If you create a console project, you still see the message box. So if you have code that opens a window, setting your app to "console" isn't going to do anything different if those lines that create and display a window will be executed. The only difference that you will see is that a console window will pop up automatically at program startup.
    I could mess with the main bits and rig something from the winmain, but would rather not do that.
    That is what you should do, so that you seperate the interface from the program's function.
    I recall that older visual studios could create windows applications, console applications, and console applications that could call windows functions, and pop up dialog boxes.
    That isn't Visual Studio, that is how Windows works. See my previous example. You can do anything in a GUI app as you can do in a console app, and vice-versa.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jul 2007
    Posts
    7

    Re: Converting Windows App to Console application

    I am taking a program that was developed with a windows gui interface and no way of performing any automation from a manufacturer which controls a camera system. I want to be able to trip the camera, which the gui application already does, but it will be actuated from another application executing a system command.

    The manufacturer did not provide an api which I can link into a program that just does a "trip camera". They supplied a very nice low level interface, but a number of things have to be done to perform the operation, and the gui application already does this.

    I posed the question as a question about converting to a console application because the other programs and tools I have strung together are run by a scripting program, and a console app with a few parameters can do all that need be done by the app. No output need go to the console, so that probably is not a good way to approach it.

    This is a full on windows app which launches the gui, and then waits for the events resulting from a gui button push to actuate the actions, and i actually guess the question is how to best go about dumping the gui (dont care about it) and interacting with an already developed app to do a few simple actions.

    I realize there are a lot of ways that I can architect what it should do that would be better, but I'm trying to dodge the bullet of having to write something I'm not that familiar with and then integrate what this app already does into it. I have to be able to substitute a lot of other devices into this slot in the script or system, and the testing may actually show this product is not what we want, so I'm trying quick and dirty.

    So short answer is, don't need to do any output to the console, can get by w/o any input from the console, just want the program to launch, don't want or possibly even need gui, just do an action, then bail out on each run.

    I used the console app as an example because I'm most familiar with generating one of those with MS Visual studio that will do the the run quick and bail sort of approach, but the console app may not be the way to go, since there is no input or output.

    thanks very much for your replies

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Converting Windows App to Console application

    Quote Originally Posted by jws
    So short answer is, don't need to do any output to the console, can get by w/o any input from the console, just want the program to launch, don't want or possibly even need gui, just do an action, then bail out on each run.
    I am assuming you have something like this . Button1 press-> Perform action 1, Button2 press -> perform action 2.
    Also, I am assuming that , you want to perform action 1 and action 2 without the button press, but by running the program silently.

    If the above is true, what you can do is this:
    Your application should have an entry point which actually does the showing of the GUI. Instead of doing this unconditionally, you would do it based on certain command line parameters passed.

    So, you decide on specific command line parameters , say action1 , action2.
    If the app is launched with these like below:
    YourApp.exe action1 , your application code would first interpret the command lines passed and if it is action1, execute the action 1 code directly and not show the GUI.

    Is this what you are looking to do ?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,764

    Re: Converting Windows App to Console application

    You can use AllocConsole to create a console window if you need some text display of stdoutput or stderror.

    The allocated console window will work in parallel with the windows app, so you need to modify the app to take cmdLine params so it knows to display the console, hide the main window or both, or show the window. A quick way is to show the console and hide the main window of the app.

    You'll also need to redirect the stderror or stdoutput. I don't have a code example handy but I can get you one if interested.

    Btw, I've used this technique in conjunction with a Windows Service to help debug the service. I had a debug trace class that would spew output to the console window. I could start the service with a flag that displayed the console window.
    Last edited by Arjay; July 24th, 2007 at 02:56 PM.

  7. #7
    Join Date
    Jul 2007
    Posts
    7

    Re: Converting Windows App to Console application

    Quote Originally Posted by kirants
    I am assuming you have something like this . Button1 press-> Perform action 1, Button2 press -> perform action 2.
    Also, I am assuming that , you want to perform action 1 and action 2 without the button press, but by running the program silently.

    <snip>

    Is this what you are looking to do ?
    It would work just fine, along with the suggestion to get rid of the gui display conditionally. I am looking at the windows application structure more with an eye to a modification such as you suggested. It looks like there is an InitInstance that does the setup, then punts, so I can probably rig something to be set up there. It has all the code to detect the camera device, etc., so it has determined that the camera is functional. I can probably just trigger the actions needed after that, and suppress display of the gui if it is to be automatic.

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Converting Windows App to Console application

    You mention InitInstance. Is it an MFC application, by any chance ?

  9. #9
    Join Date
    Jul 2007
    Posts
    7

    Re: Converting Windows App to Console application

    Quote Originally Posted by Arjay
    You can use AllocConsole to create a console window if you need some text display of stdoutput or stderror.
    <snip>
    .
    Arjay,
    I have wanted to have a clean way to do this in the past, rather than having to dump a lot of junk in a file log. If you have a snippet that does that, I'd be interested. I really don't need it for the question at hand, but I think posting a question with your answer in another thread would be very helpful, or point me at a thread that already does this.

    thanks for the suggestion.

  10. #10
    Join Date
    Jul 2007
    Posts
    7

    Re: Converting Windows App to Console application

    Quote Originally Posted by kirants
    You mention InitInstance. Is it an MFC application, by any chance ?
    Yes it is. I am using Visual Studio 2005 on a project that was converted to the 2005 format by VS from what was supplied from the original code as a project. It is now an MFC "solution". I didn't develop it in VS2005, it has been around for a while.

  11. #11
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Converting Windows App to Console application


  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,764

    Re: Converting Windows App to Console application

    Quote Originally Posted by jws
    Arjay,
    I have wanted to have a clean way to do this in the past, rather than having to dump a lot of junk in a file log. If you have a snippet that does that, I'd be interested. I really don't need it for the question at hand, but I think posting a question with your answer in another thread would be very helpful, or point me at a thread that already does this.

    thanks for the suggestion.
    Here you go:

    Code:
    HRESULT CLogConsole::Open( )
    {
      HRESULT hr = S_OK;
     
      // allocate a console
    if( FALSE == AllocConsole( ) )
      {
    	return HRESULT_FROM_WIN32( ::GetLastError( ) );
      }
     
      // Redirect the standard output to the console
      if( NULL == _tfreopen( _T("CONOUT$"), _T("wta+"), stdout ) )
    if( NULL == freopen( "CONOUT$", "wta+", stdout ) )
      {
    	return HRESULT_FROM_WIN32( ::GetLastError() );
      }
     
      m_bConsoleAllocated = TRUE;
     
      return hr;
    }
     
    // Close the console
    void CLogConsole::Close( )
    {
      if( TRUE == m_bConsoleAllocated )
      {
    	FreeConsole( );
      }
    }


    I used this code about 4 years ago on .Net 2003. If I recall this was compiled for UNICODE. Before then on VC6, I compiled it as ANSI. Anyways, looks like I tried using the TCHAR _tfreopen version, but went back to the original freopen version (which I believe will work on ANSI and UNICODE).

  13. #13
    Join Date
    Jul 2007
    Posts
    7

    Re: Converting Windows App to Console application

    Quote Originally Posted by kirants
    I had to make a couple of changes to get it to work with the VS5 maybe due to compiler changes or problems.

    I had to make the constructor public by adding a public: to the definition, and I had to change the ParseCommandLine parameter to remove reference.

    ///////////////////////////////
    class CCustomCommandLineInfo : public CCommandLineInfo
    {
    public:
    CCustomCommandLineInfo()
    {
    m_bExport = m_bOpen = m_bWhatever = FALSE;
    }
    ///////////////////////////////

    and removed "&"

    ///////////////////////////////

    CCustomCommandLineInfo oInfo;
    ParseCommandLine(oInfo);


    ///////////////////////////////

    havent executed it yet, but it compiles.

    thanks

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Converting Windows App to Console application

    Quote Originally Posted by jws
    ...but it compiles.
    If I had a nickel for each time I've heard that one!
    That is one of the oldest excuses in the book!
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  15. #15
    Join Date
    Jul 2007
    Posts
    7

    Re: Converting Windows App to Console application

    Quote Originally Posted by VladimirF
    If I had a nickel for each time I've heard that one!
    That is one of the oldest excuses in the book!
    I do wish I had a nickel as well. The good news is that it is working. Now to try the console suggestion above, and repost.

    Anyone want to comment on whether the faq should be updated in this case?

    Jim

Page 1 of 2 12 LastLast

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)