// --perfinst.c-----------------------------------------------------------------
//
// Performance monitoring installation sample.
//
// Copyright (C) Microsoft Corp. 1986-1996. All Rights Reserved.
// -----------------------------------------------------------------------------
#include "edk.h"
//
// Names of Command Line Arguments
//
#define ARG_SERVICE "SERVICE"
#define ARG_CLASS "CLASS"
#define ARG_DLL "DLL"
#define ARG_OPEN "OPEN"
#define ARG_CLOSE "CLOSE"
#define ARG_COLLECT "COLLECT"
#define ARG_DELETE "DELETE"
#define ARG_HELP1"?"
#define ARG_HELP2"HELP"
//
// Table of Command Line Switches for _HrExpandCommandLineArgument()
//
static char * rgpszArgArray[] = {
ARG_SERVICE,
ARG_CLASS,
ARG_DLL,
ARG_OPEN,
ARG_CLOSE,
ARG_COLLECT,
ARG_DELETE,
ARG_HELP1,
ARG_HELP2,
};
#define ARGARRAYSIZE (sizeof(rgpszArgArray)/sizeof(rgpszArgArray[0]))
//
// Variables For Command Line Arguments
//
char szServiceName[MAX_PATH+1] = {0};
char szServiceClass[MAX_PATH+1] = {0};
char szPerfDllName[MAX_PATH+1] = {0};
char szOpenFunc[MAX_PATH+1] = {0};
char szCollectFunc[MAX_PATH+1] = {0};
char szCloseFunc[MAX_PATH+1] = {0};
BOOL fDisplayedHelp= FALSE;
BOOL fDeletePerform = FALSE;
BOOL fUnloadCounters = FALSE;
//
// Functions
//
//$--ShowUsage------------------------------------------------------------------
// Show usage information.
// -----------------------------------------------------------------------------
static VOID ShowUsage(void)// RETURNS: nothing
{
DEBUGPRIVATE("ShowUsage()\n");
printf("USAGE: PERFINST [Flags]\n\n");
printf(" [Flags] Enter PERFINST /? for details\n");
fDisplayedHelp = TRUE;
}
//$--ShowHelp-------------------------------------------------------------------
// Show help information.
// -----------------------------------------------------------------------------
static VOID ShowHelp(void)// RETURNS: nothing
{
DEBUGPRIVATE("ShowHelp()\n");
printf("Performance Monitoring Installation sample.\n\n");
printf("USAGE: PERFINST [Flags]\n\n");
printf(" Required Flags\n");
printf(" /SERVICE= Service name\n");
printf(" /CLASS= Service class\n");
printf(" /DLL= Performance DLL name\n");
printf(" /OPEN= Initializes performance monitoring\n");
printf(" /COLLECT= Reports performance data when requested\n");
printf(" /CLOSE= Closes performance monitoring\n");
printf("\n");
printf(" Optional Flags\n");
printf(" /DELETE Delete performance monitoring\n");
printf(" /HELP or /? Display help screen\n");
fDisplayedHelp = TRUE;
}
//$--HrConfig-------------------------------------------------------------------
// Read configuration from command line.
// -----------------------------------------------------------------------------
HRESULT HrConfig( // RETURNS: return code
IN int argc, // number of arguments on command line
IN char *argv[]) // array of command line arguments
{
HRESULThr= NOERROR;
HRESULThrT= NOERROR;
char *pszArgument= NULL;
char *pszValue= NULL;
inti= 0;
DEBUGPRIVATE("HrConfig()\n");
// If there are no command line arguments then show a usage message.
if (argc < 2)
{
ShowUsage();
hr = E_FAIL;
goto cleanup;
}
// Do an initial check for /? or /HELP. If found, show a help message
// and don't do any other parsing.
for (i = 1; i < argc; i++)
{
hr = _HrExpandCommandLineArgument(
argv[i], rgpszArgArray, ARGARRAYSIZE, NULL, &pszArgument, &pszValue);
if (SUCCEEDED(hr) && pszArgument &&
(!lstrcmpi(pszArgument,ARG_HELP1) ||
!lstrcmpi(pszArgument,ARG_HELP2)))
{
ShowHelp();
hr = E_FAIL;
goto cleanup;
}
}
// Loop through and parse all the command line arguments.
for (i = 1; i < argc; i++)
{
hrT = _HrExpandCommandLineArgument(
argv[i], rgpszArgArray, ARGARRAYSIZE, NULL, &pszArgument, &pszValue);
if (FAILED(hrT))
{
hr = hrT;
if (hr == EDK_E_NOT_FOUND)
{
fprintf(stderr, "ERROR: unknown command line flag: %s\n",
argv[i]);
continue;
}
else
{
fprintf(stderr, "ERROR: unable to parse command line.\n");
goto cleanup;
}
}
// Parse flag arguments that don't take a value.
else if (pszArgument != NULL && pszValue == NULL)
{
if (!lstrcmpi(pszArgument,ARG_DELETE))
{
fDeletePerform = TRUE;
}
// Other flag (must take a value).
else
{
fprintf(stderr, "ERROR: flag /%s requires a value\n",
pszArgument);
hr = HR_LOG(E_FAIL);
}
}
// Parse flag arguments that take a value.
else if (pszArgument != NULL && pszValue != NULL)
{
if (!lstrcmpi(pszArgument,ARG_SERVICE))
{
strncpy(szServiceName, pszValue, MAX_PATH);
}
else if (!lstrcmpi(pszArgument,ARG_CLASS))
{
strncpy(szServiceClass, pszValue, MAX_PATH);
}
else if (!lstrcmpi(pszArgument,ARG_DLL))
{
strncpy(szPerfDllName, pszValue, MAX_PATH);
}
else if (!lstrcmpi(pszArgument,ARG_OPEN))
{
strncpy(szOpenFunc, pszValue, MAX_PATH);
}
else if (!lstrcmpi(pszArgument,ARG_CLOSE))
{
strncpy(szCloseFunc, pszValue, MAX_PATH);
}
else if (!lstrcmpi(pszArgument,ARG_COLLECT))
{
strncpy(szCollectFunc, pszValue, MAX_PATH);
}
// Other flag (must not take a value).
else
{
fprintf(stderr, "ERROR: flag /%s does not take a value\n",
pszArgument);
hr = HR_LOG(E_FAIL);
}
}
// Catch unknown arguments.
else
{
fprintf(stderr, "ERROR: unknown argument %s\n", argv[i]);
hr = HR_LOG(E_FAIL);
}
}
// Make sure we have all the info we need.
if (SUCCEEDED(hr))
{
if (szServiceName[0] == 0)
{
fprintf(stderr, "ERROR: please specify /%s\n", ARG_SERVICE);
hr = HR_LOG(E_FAIL);
}
if (szServiceClass[0] == 0)
{
fprintf(stderr, "ERROR: please specify /%s\n", ARG_CLASS);
hr = HR_LOG(E_FAIL);
}
if(fDeletePerform == FALSE)
{
if (szPerfDllName[0] == 0)
{
fprintf(stderr, "ERROR: please specify /%s\n", ARG_DLL);
hr = HR_LOG(E_FAIL);
}
if (szOpenFunc[0] == 0)
{
fprintf(stderr, "ERROR: please specify /%s\n", ARG_OPEN);
hr = HR_LOG(E_FAIL);
}
if (szCollectFunc[0] == 0)
{
fprintf(stderr, "ERROR: please specify /%s\n", ARG_COLLECT);
hr = HR_LOG(E_FAIL);
}
if (szCloseFunc[0] == 0)
{
fprintf(stderr, "ERROR: please specify /%s\n", ARG_CLOSE);
hr = HR_LOG(E_FAIL);
}
}
}
cleanup:
RETURN(hr);
}
//$--main-----------------------------------------------------------------------
// Main function.
// -----------------------------------------------------------------------------
int main(
IN int argc,
IN char *argv[])
{
HRESULT hr = NOERROR;
BOOL fErrMsg = TRUE;
printf( "\n" );
//
// Get parameters from the command-line
//
hr = HrConfig(argc, argv);
if (FAILED(hr))
{
fErrMsg = FALSE;
goto cleanup;
}
if(fDeletePerform == FALSE)
{
hr = HrInstallServicePerfMon(
szServiceName,
szServiceClass,
szPerfDllName,
szOpenFunc,
szCollectFunc,
szCloseFunc);
}
else
{
hr = HrRemoveServicePerfMon(
szServiceName,
szServiceClass,
&fUnloadCounters);
if(fUnloadCounters == TRUE)
{
fprintf(
stderr,
"Performance counters can now be unloaded!\n");
}
}
cleanup:
// Successful completion.
if (fDisplayedHelp)
{
hr = NOERROR;
}
else if (SUCCEEDED(hr))
{
if(fDeletePerform == FALSE)
{
fprintf(
stderr,
"Performance monitoring installed successfully.\n");
}
else
{
fprintf(
stderr,
"Performance monitoring deleted successfully.\n");
}
hr = NOERROR;
}
// Error completion.
else
{
if(fErrMsg == TRUE)
{
fprintf(stderr, "ERROR: an error occurred [%08lx].\n",hr);
}
hr = HR_LOG(E_FAIL);
}
// return exit code
return _nEcFromHr(hr);
}