views:

84

answers:

2

I am trying to run a set of test cases through one of our internal applications. I need to be able to automate these tests and run them through the command line and log various things. The problem is that the existing code does not separate the view from the controller and throws MessageBoxes and Alerts everywhere that ask the user to click on a button (in my case just OK/CONTINUE). Currently, it has been decided to introduce state flags that will help determine the context from which the application is being run so one can decide whether to do a Console.WriteLine() or a MessageBox.Show(). But as you can imagine this has cascaded into a lot of changes and dirty if-else blocks.

Sadly, it doesn't look feasible to sit down and separate the logic from the view at this stage. So I was wondering if there was a way to detect the context in which the application is executing. I would like to replace each of the MessageBox() call with a Notify() call which itself can detect the context - whether to show the output on a command prompt or pop up a form.

EDIT_1: Any other suggestions for doing this are also welcome.

A: 

In my application, I parse the command line arguments and set some internal flags. If you add that type of parsing to the entry point of your application, the existance of the command line argument being set to "Y" for example - can set the appropriate flag in the application used to determine where the output is sent.

Personally, I capture all the "STDOUT" (console) output to a log file, then if not running in "scheduled" mode - I pop up a msgbox().

Ron

Ron Savage
A: 

In C using the Win32 API (off the top of my head, untested):

// requires Windows 2000 or later (for GetConsoleWindow)
int parent_owns_the_console()
{
 DWORD pid=0;
 GetWindowThreadProcessId(GetConsoleWindow(), &pid);
 return pid && pid != GetCurrentProcessId();
}
Hugh Allen