Home arrow ASP arrow Page 2 - Executing And Tracking A Process With VB And ASP
ASP

Executing And Tracking A Process With VB And ASP


If you've ever used the Windows API, then you'll know that it has certain functions to execute and monitor shell applications. In this article Joe shows us how to execute and monitor a C++ program as a separate process using API's through a Visual Basic ActiveX DLL and ASP.

Author Info:
By: Joe O'Donnell
Rating: 5 stars5 stars5 stars5 stars5 stars / 35
January 26, 2002
TABLE OF CONTENTS:
  1. · Executing And Tracking A Process With VB And ASP
  2. · Creating the legacy app
  3. · Creating the ActiveX DLL

print this article
SEARCH DEVARTICLES

Executing And Tracking A Process With VB And ASP - Creating the legacy app
(Page 2 of 3 )

Back in the 1970's when mainframe computers dominated the world, companies like IBM were busy creating applications that could process and distribute company financials, clients contact credentials, etc. Because these applications were so powerful and contained huge amounts of manually entered data, many companies didn't see the need to update their mainframe systems or software as time went by.

Today, the applications and databases that still run on mainframes are known as legacy applications, and there are literally thousands of companies who still use mainframe computers to store important data. In just a second we're going to create a "dummy" legacy application that we will use to mimic the implementation of an important mainframe function: backing up a database. We won’t actually implement any of the C++ code to back up a database, instead we will implement a timer that will cause the application to "sleep" for a specific amount of time and then return control to the operating system.

Fire up Visual C++ and create a new, empty console project named "LegacyApp". Add a new class to the project and call it "main". Switch to the file view tab and delete all of the code in both main.h and main.cpp. Enter the following code into main.cpp:

#include <windows.h>

#include <iomanip>

using namespace std;

// Default sleep time in seconds

const long DEFAULT_SLEEP_TIME = 5000;

int main(int argc, char **argv)

{

long sleepTime = 0;



if(argc < 2)

{

sleepTime = DEFAULT_SLEEP_TIME;

}

else

{

try

{

sleepTime = atol(*(argv+1));

}

catch(...)

{

sleepTime = DEFAULT_SLEEP_TIME;

}

}

// Imitate work

Sleep(sleepTime);

return 0;

}


Let's run through the code step by step.

#include <windows.h>

#include <iomanip>

using namespace std;


Our C++ console application will run under Windows. C++ includes a number of header files that wrap API calls, and windows.h is one of them. We include windows.h to get access to its Sleep() function, which we will discuss shortly. We also include the iomanip header file and tell the C++ compiler that we want access to all methods and members of the std namespace without explicitly prepending them with "std::".

// Default sleep time in seconds

const long DEFAULT_SLEEP_TIME = 5000;

int main(int argc, char **argv)


As mentioned earlier, our dummy legacy app won't actually perform any work; it will just sleep for a specific amount of time. The amount of time to sleep can be passed in as a command-line argument. The constant long integer DEFAULT_SLEEP_TIME will be used as the number of milliseconds to sleep if a value isn't passed-in from the command line.

long sleepTime = 0;



if(argc < 2)

{

sleepTime = DEFAULT_SLEEP_TIME;

}


The long integer variable, sleepTime, will store the number of milliseconds that our app will sleep. It is initialised to zero. Argc is the first argument in our main() function and is automatically supplied by the operating system. It's an integer that counts the number of arguments passed in from the command line (each argument is stored in the argv pointer-to-character array).

The thing to remember here is that argc is the number of command line arguments plus one, because the first index of the argv array is always the name of the application. If no command-line argument is passed-in, then the value of sleepTime is set to the constant long integer DEFAULT_SLEEP_TIME, which is 5000 milliseconds (5 seconds).

else

{

try

{

sleepTime = atol(*(argv+1));

}

catch(...)

{

sleepTime = DEFAULT_SLEEP_TIME;

}

}


On the other hand, if the user has passed a command line argument with the call to our app, then we use a try…catch block to try and retrieve its value as a long integer. The atol (Alpha To Long) function is declared in the iomanip header file and tries to converts a string of characters to a long integer.

If the conversion fails, then the code within the following catch block is executed. Notice how the catch block contains an ellipsis within the brackets? This tells C++ that if any errors at all occur within the try block to stop execution and immediately jump into the catch block. The catch block sets the value of sleepTime to DEFAULT_SLEEP_TIME.

// Imitate work

Sleep(sleepTime);

return 0;

}


Lastly, we call the Sleep function, as defined in the windows.h header file. The Sleep function halts program execution for a specified amount of milliseconds (in our case, sleepTime milliseconds). The signature of the Sleep function looks like this:

VOID Sleep(DWORD dwMilliseconds);

If you've never worked with C++ in windows, then don't get confused by the DWORD data type. It's simply a typedef for the long integer variable type unsigned long.

Use the build menu to compile your console application. It should be named LegacyApp.exe. Create a new directory named c:\legacy and move LegacyApp.exe into that directory. Before we continue, try running the executable from the command line. Try specifying a value of 2,000 milliseconds with this command: "legacyapp.exe 2000". Also try running it without any arguments. It should sleep for two seconds and then five seconds respectively.
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials