The Logger Code
As promised, Listing 7-2 shows the C source code for the spy type application we used earlier on the command line arguments of both C2.EXE and LINK.EXE. Note that a nearly equivalent Visual Basic version follows this application.
Listing 7-2 The OUTARGS logger application in C
/*******************************************************
Small 'C' applet used to replace Visual Basic 6 compiler
apps so as to gather their output and manipulate their
command-line switches.
See notes in main text for more details.
*******************************************************/
#include < stdio.h >
#include < string.h >
#include < time.h >
#include < windows.h >
int main
(
int argc // Number of command-line arguments.
,char * argv[] // The arguments themselves.
,char * env [] // Environment variables.
)
{
/**************************************
** General declares.
*/
#define BUFF 2048
auto FILE * stream; // File to write to.
auto struct tm * tt; // Time stuff for time of write.
auto time_t t; // ----- " " -----
auto char carBuff[255]; // Used for holding output
// file name.
auto char carArgs[BUFF]; // Holds command line args
// for display.
auto int nLoop; // Loop counter.
/* ***************
** Code starts ...
*/
// Change according to what real (renamed) application you
// want to start.
(void)strcpy(&carArgs[0], ".\\C3 ");
// Get the system time and convert it to ASCII string.
(void)time(&t);
tt = localtime(&t);
// Going to need to append to our exe name, so write
// to temp buffer.
(void)strcpy(&carBuff[0], argv[0]);
// Now append .OUT - should contain ???.OUT after this where ???
// could be APP.EXE or just APP, depending upon how this program
// is run.
(void)strcat(&carBuff[0], ".OUT");
// Write to EXEName.OUT file (append mode)...
if (NULL != (stream = fopen(&carBuff[0], "a")))
{
// Write out the time.
(void)fprintf(stream, "********** Run @ %s\n", asctime(tt));
// Output name of EXE file.
(void)fprintf(stream, "* EXE file...\n\n");
(void)fprintf(stream, "\t%s\n", argv[0]);
/* *****************************************************
** Output command line args (exclude our exe name argv[0]).
*/
(void)fprintf(stream, "\n* Command Line Arguments...\n\n");
for (nLoop = 1; nLoop < argc; nLoop++)
{
(void)fprintf(stream,"%d\t%s\n", nLoop, argv[nLoop]);
// Append to args buffer.
(void)strcat(&carArgs[0] , argv[nLoop]);
(void)strcat(&carArgs[0] , " ");
}
/* *****************************
** Output environment variables.
*/
(void)fprintf(stream, "\n* Environment Variables...\n\n");
for (nLoop = 0; NULL != env[nLoop]; nLoop++)
{
(void)fprintf(stream, "%d\t%s\n", nLoop, env[nLoop]);
}
/* ***************************************************
** Output name and args of other application to start.
*/
(void)fprintf(stream, "\n* 'Real' program and arguments...\n\n");
(void)fprintf(stream, "\t%s\n", &carArgs[0]);
(void)fprintf(stream, "\n********** Run End\n\n\n");
// All done so tidy up.
(void)fclose(stream);
(void)WinExec(&carArgs[0], 1);
}
return 0;
}
|
And the (nearly) equivalent Visual Basic application in Listing 7-3:
Listing 7-3 The OUTARGS logger application in Visual Basic
Sub Main()
If 0 <> Len(Command$) Then
Dim sRealAppName As String
sRealAppName = GetSetting(App.EXEName, "Startup", _
"RealAppName", "")
If 0 <> Len(sRealAppName) Then
Call Shell(sRealAppName & " " & Command$, vbHide)
Dim nFile As Integer
nFile = FreeFile
Open App.EXEName & ".out" For Append Access Write As nFile
Print #nFile, "****** Run at " & _
Format$(Date, "Short date") & _
" " & Format$(Time, "Long Time")
Print #nFile, sRealAppName & " " & Command$
Close nFile
End If
End If
End Sub
|