Hello Sniffer Folks,
The WeightGate utility I posted was done in a real hurry, so I
thought I'd post source here just in case anybody wants to review
it and in case of any trouble ;-) Also for any educational value
it may have for others interested in writing similar kinds of
utilities.
Source attached in HTML form.
Thanks,
_M
--
Pete McNeil
Chief Scientist,
Arm Research Labs, LLC.Title: C:\m\Projects\SNF\WeightGate\main.cpp
// WeightGate.cpp
// (C) 2006 ARM Research Labs, LLC
//
// This tiny command line utility accepts a set of parameters, the first
// 3 are a low weight number, the actual weight, and then a high weight
// number. The remainder of the parameters are a program and it's parameters
// which should be launched IF the actual weight number is in the range
// [low,high]
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
// callExternalApp()
// Launches an external application and returns it's exit code.
int callExternalApp(int argc, char* argv[]) { // How to call an external app.
// Create fundamentals for the call...
STARTUPINFO si; // Startup and process info
PROCESS_INFORMATION pi; // holders for CreateProcess()
ZeroMemory(&si, sizeof(si)); // Initialize si to null
si.cb = sizeof(si); // and set the cb.
ZeroMemory(&pi, sizeof(pi)); // Initialize pi to null.
// Create a command line string from argv[]
string CommandLine(argv[0]); // Start with the command name arg0.
for(int i = 1; i < argc ; i++) { // For each argument
CommandLine += " "; // add a space add then the arg.
if(NULL == strchr(argv[i],' ')) { // If it does not contain a space
CommandLine.append(argv[i]); // just add the arg to the string.
} else { // If the arg does contain a space
CommandLine.append("\""); // we enclose it in quotes.
CommandLine.append(argv[i]);
CommandLine.append("\"");
}
}
char* CommandLineBuffer = new char[CommandLine.size()+1]; // Create a bfr for CreateProcess()
strcpy(CommandLineBuffer, CommandLine.c_str()); // and copy the command line.
// Launch the app and wait.
int CallResult; // We need to return a call result.
if(false != CreateProcess(
NULL, // No Double Name use command line.
CommandLineBuffer, // sz Command Line
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
false, // Set handle inheritance to false.
CREATE_DEFAULT_ERROR_MODE, // Default Error Mode.
NULL, // Use parrent's environment.
NULL, // Use parrent's starting directory.
&si, // Startup Info to use.
&pi // Process Info to use.
)){ // If we launched successfully...
// Process Creation Was Successful...
WaitForSingleObject( pi.hProcess, INFINITE ); // Wait for the program to exit.
DWORD CR;
if(0 != GetExitCodeProcess( pi.hProcess, &CR)) { // Get the exit code.
CallResult = CR; // On success record our result.
} else { // On error, our result will be
CallResult = GetLastError(); // the trouble we had.
}
// Cleanup
CloseHandle( pi.hProcess ); // Close the process handle
CloseHandle( pi.hThread ); // and main thread handle.
} else { // If we could not launch...
// Process Creation Failed...
CallResult = GetLastError(); // We will return the error.
}
// End of task cleanup...
delete[] CommandLineBuffer; // Free our buffer.
CommandLineBuffer = NULL; // and null it's pointer.
return CallResult; // Return the call result.
}
// End callExternalApp()
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// printErrorMessage()
string SHOW_ME_PATH = "c:\\ShowMe.log"; // Show Me Path
void showMe(int argc, char* argv[]) { // Show Me function...
ofstream log(SHOW_ME_PATH.c_str(),ios::out|ios::app); // Open the log file...
log << "ShowMe:" << endl; // Append the ShowMe: header...
for(int i = 0; i < argc; i++) { // For each argument we have,
log << "arg[" << i << "] = " << argv[i] << endl; // spit out a line with the data.
}
log.close(); // When we're done close the file.
}
void printErrorMessage(int argc, char* argv[]) { // Tell folks what's going on.
cout << "WeightGate.exe " << endl
<< "(C) 2006 ARM Research Labs, LLC." << endl
<< endl
<< "This program is distributed AS-IS, with no warranty of any kind." << endl
<< "You are welcome to use this program on your own systems or those" << endl
<< "that you directly support. Please do not redistribute this program" << endl
<< "except as noted above, however feel free to recommend this program" << endl
<< "to others if you wish and direct them to our web site where they" << endl
<< "can download it for themselves. Thanks! www.armresearch.com." << endl
<< endl
<< "This program is most commonly used to control the activation of" << endl
<< "external test programs from within Declude (www.declude.com) based" << endl
<< "on the weigth that has been calculated thus far for a given message." << endl
<< endl
<< "As an added feature, if you rename this program to ShowMe.exe then" << endl
<< "it will emit all of the command line arguments as it sees" << endl
<< "them to a file called " << SHOW_ME_PATH << " so that you can use it" << endl
<< "as a debugging aid." << endl
<< endl
<< "If you are seeing this message, you have used this program" << endl
<< "incorrectly. The correct invocation for this program is: " << endl
<< endl
<< "WeightGate <low> <weight> <hight> <program> <arg 1>, <arg 2>,... <arg n>" << endl
<< endl
<< "Where:" << endl
<< " <low> = a number representing the lowest weight to run <progra>." << endl
<< " <weight> = a number representing the actual weight to evaluate." << endl
<< " <high> = a number representing the highest weight to run <program>." << endl
<< " <program> = the program to be activated if <weight> is in range." << endl
<< " <arg 1>, <arg 2, ... <arg n> = arguments for <program>." << endl
<< endl
<< "If <weight> is in the range [<low>,<high>] then WeightGate will run" << endl
<< "<program> and pass all of <arg 1>, <arg 2>,... <arg n> to it. Then" << endl
<< "WeightGate will collect the exit code of <program> and return it as" << endl
<< "WeightGate's exit code." << endl
<< endl
<< "If WeightGate gets the wrong number of parameters it will display" << endl
<< "this message and return FAIL_SAFE (zero) as it's exit code." << endl
<< endl
<< "If <weight> is not in range (less than <low> or greater than <high>)" << endl
<< "then WeightGate will NOT launch <program> and will return FAIL_SAFE" << endl
<< "(zero) as it's exit code." << endl
<< endl
<< "As a deubgging aid, I was called with the following arguments:" << endl
<< endl;
cout << "arg[0] <me> = " << argv[0] << endl;
if(1 < argc) { cout << "arg[1] <low> = " << argv[1] << endl; }
if(2 < argc) { cout << "arg[2] <weight> = " << argv[2] << endl; }
if(3 < argc) { cout << "arg[3] <high> = " << argv[3] << endl; }
if(4 < argc) { cout << "arg[4] <program> = " << argv[4] << endl; }
for(int i = 5; i < argc; i++) {
cout << "arg[" << i << "] <arg " << (i-4) << "> = " << argv[i] << endl;
}
}
// End printErrorMessage()
////////////////////////////////////////////////////////////////////////////////
int MINIMUM_ARGS_COUNT = 5; // Minimum number of arguments.
int LOW_ARG_INDEX = 1; // The low weight argument index.
int WEIGHT_ARG_INDEX = 2; // Current weight argument index.
int HIGH_ARG_INDEX = 3; // High weight argument index.
int ARGS_USED_BY_US = 4; // Arguments used by this program.
int GATED_APP_ARGS_INDEX = ARGS_USED_BY_US; // Index of first arg for the gated app.
int FAIL_SAFE = 0; // Return this (zero) if we fail.
int main(int argc, char* argv[]) {
if(string::npos != string(argv[0]).find("ShowMe.exe")) { // If we find showme.exe in our name,
showMe(argc, argv); // log the arguments we were called with
return FAIL_SAFE; // then exit with our FAIL_SAFE code.
}
if(MINIMUM_ARGS_COUNT > argc) { // If the count of arguments is wrong
printErrorMessage(argc, argv); // print the error message and
return FAIL_SAFE; // exit with our FAIL_SAFE code.
}
if( // Check our arguments...
( !isdigit(argv[LOW_ARG_INDEX][0]) && argv[LOW_ARG_INDEX][0]!='-' ) || // <low> must be a number
( !isdigit(argv[WEIGHT_ARG_INDEX][0]) && argv[WEIGHT_ARG_INDEX][0]!='-' ) || // <weight> must be a number
( !isdigit(argv[HIGH_ARG_INDEX][0]) && argv[HIGH_ARG_INDEX][0]!='-' ) // <high> must be a number
) { // If any of these are wrong
printErrorMessage(argc, argv); // then print the error message
return FAIL_SAFE; // and return FAIL_SAFE;
}
int low = atoi(argv[LOW_ARG_INDEX]); // With the right argument count convert
int weight = atoi(argv[WEIGHT_ARG_INDEX]); // each of the weight arguments to
int high = atoi(argv[HIGH_ARG_INDEX]); // an integer.
if(low <= weight && weight <= high) { // If the weight is in range then
return callExternalApp( // call the gated application
(argc - ARGS_USED_BY_US), // starting with it's name/path and
&argv[GATED_APP_ARGS_INDEX] // including all of it's parameters.
);
}
return FAIL_SAFE;
}
#############################################################
This message is sent to you because you are subscribed to
the mailing list <[email protected]>.
To unsubscribe, E-mail to: <[EMAIL PROTECTED]>
To switch to the DIGEST mode, E-mail to <[EMAIL PROTECTED]>
To switch to the INDEX mode, E-mail to <[EMAIL PROTECTED]>
Send administrative queries to <[EMAIL PROTECTED]>