Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Migrate Console App to Windows 32 App C++  (Read 4948 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Migrate Console App to Windows 32 App C++
« on: August 27, 2013, 03:54:15 PM »
I have a program that I wrote up in C++ as a Console App using Bloodshed Dev C++ 4.9.9.2 and it works perfectly fine. But the interface is ugly as can be expected from the limitations of working in a Console App environment.

I looked on google for some good information on converting a Console App to Windows 32 App what little I found wasnt very helpful.

What I was thinking of doing is using Microsoft Visual C++ 2010 Express to make a nice GUI for this otherwise ugly looking program and add bottons to be selected to trigger events vs a user having to enter Y or N at prompts, and then run the program. BUT I also think that if I got this added as a button click routine that it might break the functionality of the program.

The program is basically a Disc to Disk Archive Creator. I have decided to purchase a 3TB external drive and take stacks of data CD's and DVD's and xcopy their contents to this massive external hard drive. But instead of doing it with a batch file, I decided to do it in C++ Console App programming instead.

This is how the program runs:

1.) I basically have my program asking for Source and Destination Drive info.
2.) Verifying that the info is correct Y/N ( If incorrect the loop sends them back through config process )
3.) Read in from a file that keeps track of last counter++ value
4.) Create a directory at the destination that is named (DestDriveLetter):\DiscX ( where X = the next available counter value ) through String Steam Concatonation passed to a system(); call.
5.) Concatonate via String Stream an XCOPY routine to copy the information from the source to the destination, and user variables as entered by the user from say D:\*.* to say E:\Disc32\
6.) Notify the user that the disc is complete and ask if they want to run another Y/N ( If YES, tell user to swap disc's and then enter C to continue to process the next disc at say E:\Disc33\ ... If NO, then write the next available counter value to counter text file and exit program.

Next time the program is run it will pick up at 33 if no Disc was processed as 33.

* Thinking of adding an override for the counter so that you dont have to edit the counter file back to 1 and save it to start fresh on another group of discs for a different Data Disc Archive.

But my concern lies in if you can actually pass a system(); call in a Windows 32 App which is not running from a Command Shell Console App environment, in which the Windows 32 App may not be that friendly with executing DOS instructions.

Anyone have any info on how to make this work? Found very little information on this matter, and maybe there is a better way than string streaming to system(); to achieve this since system(); calls are not a good practice as I have read scattered on the web. But I am not aware of any other way to interface with DOS / Command Shell without it in C++.

I can also post the source code here if it helps any. Its pretty basic. And works well as it is now. I just want to see if it can be added to a modern looking GUI interface by use of Microsoft Visual C++ 2010 Express or if its impossible to be done.

Linux711



    Mentor

    Thanked: 59
    • Yes
    • Programming Blog
  • Certifications: List
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: Migrate Console App to Windows 32 App C++
« Reply #1 on: September 01, 2013, 08:43:41 PM »
Even though it may not be elegant (from a programmers perspective), the easiest way I think you could do this would be to make the program take command line parameters as the input instead of asking the user directly for input. This way, you can control the functions of your console program through a GUI by executing the program with command line switches. This wouldn't work obviously if you don't have access to the source anymore though. If you want to do it right though, it shouldn't be too hard to modify to make a GUI version. I might be able to help you with it if you'd like.
YouTube

"Genius is persistence, not brain power." - Me

"Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Migrate Console App to Windows 32 App C++
« Reply #2 on: September 05, 2013, 01:26:13 PM »
Hello Linux711 ... sorry its been a couple days before getting back to you. Here is my source code to the program that I am interested in getting rid of the ugly ascii console 1980s look and make it look more modern. I put this up on the web at Github for free download and for people to build from it if anyone is interested under GPL license. https://github.com/AthlonX2/Disc2Disk/releases  ( Latest is at bottom of list on page. Havent figured out yet how to get the newest to the top and put oldest at bottom. )

Given its not very complicated and just a glorified batch type process wrapped around C++ assigning it a GPL is probably not necessary, but I did so anyways.

All the books I have on Visual C++ do not touch on command shell execution from windows and that is why I hit a wall with making this work using Visual C++ 2010 Express. I did some research earlier today on C# another language that I like to work with and found this http://stackoverflow.com/questions/2794386/system-to-c-sharp-without-calling-cmd-exe , but havent played around with it yet. I may be able to pass as an argument to cmd.exe the information that I currently string stream in C++ which would make this program run in a Windows Window App and then pop up a command shell window when it processes and then the command shell window disappears when completed in which the information on the Windows Program App of this program would then state that the "Process is complete, Please Insert another Disc and Press (the button action) for this action to run another or (the button action) to exit the program."

This program works with the attached LastValue.txt file which keeps track of the last Disc ID processed, so if you stop after processing a bunch of discs, and then continue later on it knows to start off as say Disc86 as the next available folder to create at the destination and continue on from there. This file just initializes the counter to the last available disk number as a passed integer from the file.

Code: [Select]
// Disc to Disk Archive Creator Version 2.1
// Programmed by Dave Lembke - 8/28/2013
// Released as FREEWARE under GPL

// Rev Notes:
// Found interface was lacking clarity from 2.0 such as it doesnt prompt user to swap Disc's
// Added color change to prompt process complete

#include <iostream>
#include <fstream>
#include <sstream>
 
using namespace std;
 
int main()
{
    int loop1=1;
    int loop2=1;
    int foldercounter=1;
    char junk;
    char junk2;
    char destination1;
    char source1;
   
    // Read in from text file the value for foldercounter variable
        ifstream myfile("LastValue.txt", ifstream::in);
        while(myfile >> foldercounter);
   
   
 while(loop1==1){
 system("color F0"); // change shell text color       
    cout<<"                     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n";
    cout<<"                     XX   Disc to Disk Archive Creator   XX\n";
    cout<<"                     XX -------------------------------- XX\n";
    cout<<"                     XX           Version 2.1            XX\n";
    cout<<"                     XX Programmed by Dave Lembke in C++ XX\n";
    cout<<"                     XX 8/28/2013 for use with Windows 7 XX\n";
    cout<<"                     XX & older Windows OS systems that  XX\n";
    cout<<"                     XX support the XCOPY /s/d/y Command XX\n";
    cout<<"                     XX -------------------------------- XX\n";
    cout<<"                     XX   Copyright 2013 - Dave Lembke   XX\n";
    cout<<"                     XX     This software is FREEWARE    XX\n";
    cout<<"                     XX -------------------------------- XX\n";
    cout<<"                     XX  USE AT OWN RISK ... PROGRAMMER  XX\n";
    cout<<"                     XX    NOT LIABLE FOR ANY DAMAGES    XX\n";
    cout<<"                     XX Close Window or CTRL + C to Exit XX\n";
    cout<<"                     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\n\n";

    cout<<"           Enter Drive Letter of Source Drive ( WITHOUT THE ':' COLON )\n\n";
    cin>>source1;
    cout<<"           Enter Drive Letter of Destination Drive ( WITHOUT THE ':' COLON )\n\n";
    cin>>destination1;

    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    system("color 4F"); // change shell text color
    cout<<"                           <<<   WARNING   >>>\n\n";
    cout<<"                    You will be Transferring Data from\n\n";
    cout<<"                          [ "<<source1<<": ] to [ "<<destination1<<":\\Disc"<<foldercounter<<" ]\n\n\n\n\n\n\n\n\n\n";
    cout<<"      <<< If this is correct Enter  ( Y ) for YES otherwise ( N ) for NO >>>\n";
    cin>>junk2;
    if(junk2=='Y'||junk2=='y'){
                 loop1=0;
                 cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                 }
                 else {
                 cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
                 }
}
    while(loop2==1){
system("color b0"); // change shell text color               
stringstream ss;
ss << "MD " << destination1 << ":\\Disc" <<foldercounter;
system( ss.str().c_str() );
cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout<<"        Disc Folder # [ "<<foldercounter<<" ] Created ... Starting Transfer Process...\n\n\n\n\n\n";

stringstream ss1;
ss1 << "xcopy " << source1 << ":\\*.* " <<destination1<<":\\Disc"<<foldercounter<<"\\*.* /s/d/y";
system( ss1.str().c_str() );

    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    system("color e0"); // change shell text color
    cout<<"                             XXXXXXXXXXXXXXXXXXXX\n";
    cout<<"                             X Process Complete X\n";
    cout<<"                             XXXXXXXXXXXXXXXXXXXX\n\n\n\n\n\n\n\n\n";
    cout<<"                         Replace the Disc with another\n";
    cout<<"                  and Enter ( Y or N ) to process another Disc\n";
    cout<<"                Note: Entering ( N ) for NO will Exit this program\n";
    cin>>junk;
    if(junk=='Y'||junk=='y'){
               loop2=1;

               //write value to pick up with next time program is run here
       std::ofstream write ("LastValue.txt", std::ofstream::out);
       write << foldercounter;
       write.close();
       foldercounter++; // Increment after write in case user aborts before telling program to end
                        // and avoid a gap from say 4 to 6 without 5 if they abort via ctrl + C or
                        // by closing the shell window via [x] at 4 just processed.
               }
               else{
       foldercounter++; // Increment to the next value for next time to start with
       std::ofstream write ("LastValue.txt", std::ofstream::out);
       write << foldercounter;
       write.close();
               return(0);
               }
               }
}

[recovering disk space, attachment deleted by admin]

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Migrate Console App to Windows 32 App C++
« Reply #3 on: September 06, 2013, 02:07:24 AM »
I would just rewrite it. Don't defer to Xcopy. Copy the file yourself.

If you really insist on doing so, you can use Process.Start() to start the xcopy but make it display hidden. eg:

Code: [Select]
    var psi = new ProcessStartInfo()
    {
        WindowStyle= ProcessWindowStyle.Hidden
    };
    Process xcopyprocess = Process.Start(psi);
}
(C#). <windows.h> has API functions that allow you to do this.

Personally, I think it would be a waste of time. First, you would want to know what goes wrong- if the xcopy fails or needs input for any reason, it just hangs. The window is hidden and invisible so nobody can ever interact with it. So the best you can do is manually use the ProcessStartInfo and redirect the child processes Standard Input and Standard Output, so now your program can interpret what is being written to the "console" and send output to it if desired. Now you need to detect all sorts of error conditions, and just being able to read the streams properly is going to take quite a bit of code on it's own, relatively speaking.

Much easier on your sanity to simply learn how to copy a file yourself, then how to copy a group of files, then you can add logic for date checking, etc.
I was trying to dereference Null Pointers before it was cool.