Subversion Repositories DevTools

Rev

Rev 5637 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*============================================================================ 
** Copyright (c) VIX TECHNOLOGY (AUST) LTD
**============================================================================
**
**  Project/Product : 
**  Filename        : checkReg.c
**  Author(s)       : dpurdie
**  Created         : 01-Apr-14
**
**  Description     : Windows program to
**                    Verify correct values in specified registry keys
** 
**                    Usage:
**                    checkReg <options>* Key Value
**                    Where options are:
**                    
**
**  Information     :
**   Compiler       : ANSI C++
**   Target         : 
**
***==========================================================================*/


#include <stdio.h>
#include <windows.h> 
 
VOID ErrorExit(LPTSTR);

/*----------------------------------------------------------------------------
** FUNCTION           : main
**
** DESCRIPTION        : Mainentry point
**
**
** INPUTS             : argc            - Number of args
**                      argv            - Address of args
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
   int      argp;
   char     *key = NULL;
   char     *expected = NULL;
   int      verbose = 0;

   char     *ptrKey;
   char     *ptrValue;
   char     *ptr;

   HKEY     hKey;
   HKEY     hkeyDXVer;
   long     lResult;
   DWORD    dwType;
   char     szVersion[255];
   DWORD    dwDataSize = 255;

    /*
    **  Process command line arguments
    **  Options for this program will be located first
    */
    for ( argp = 1; argp < argc ; argp++ )
    {
        /*
        **  Scan until first non option argument
        */
        if ( *argv[argp] != '-' )
            break;

        if ( _strnicmp( argv[argp], "-key:", 5 ) == 0)
        {
            key = argv[argp] + 5;
        }
        else if ( _strnicmp( argv[argp], "-value:", 7 ) == 0)
        {
            expected = argv[argp] + 7;
        }
        else if ( _strnicmp( argv[argp], "-v", 2 ) == 0)
        {
            verbose++;
        }
        else
        {
            fprintf(stderr, "Unknown option: %s\n", argv[argp] );
            ErrorExit( "Bad Option");
        }
    }

   /*
   **  Need both a key and a value
   */
    if (key == NULL || expected == NULL)
    {
        fprintf(stderr, "Both a key and a value must be specified\n" );
        ErrorExit( "Bad Option");
    }

    /*
    ** Need to split the Key into bits 
    **  First bit to specify the registry
    **  Last bit is the final key
    */

    /*
    **  Scan for the first \ or /
    **  Replace it with a null - will have the first string
    */
    for (ptrKey = key; *ptrKey; ptrKey++)
    {
        if (*ptrKey == '/')
            *ptrKey = '\\';

        if (*ptrKey == '\\')
        {
            *ptrKey = 0;
            ptrKey++;
            break;
        }
    }
    if ( !*ptrKey)
    {
        fprintf(stderr, "Could not split the key apart\n" );
        ErrorExit( "Bad Key");
    }

    /*
    ** Scan for the last \ 
    ** Will have the tail part
    */
    ptrValue = NULL;
    for (ptr = ptrKey; *ptr; ptr++)
    {
        if (*ptr == '/')
            *ptr = '\\';

        if (*ptr == '\\')
        {
            ptrValue = ptr;
        }
    }
    if (ptrValue != NULL)
    {
        *ptrValue = 0;
        ptrValue++;
    }

    if ( ptrValue == NULL || !*ptrValue)
    {
        fprintf(stderr, "Could not split the key end apart\n" );
        ErrorExit( "Bad Key");
    }

    /*
    ** Convert Prefix into known key
    */
    if (strcmp (key, "HKLM") == 0)
    {
        hKey = HKEY_LOCAL_MACHINE;
    }
    else if (strcmp (key, "HKCU") == 0)
    {
        hKey = HKEY_CURRENT_USER;
    }
    else if (strcmp (key, "HKCR") == 0)
    {
        hKey = HKEY_CLASSES_ROOT;
    }
    else if (strcmp (key, "HKCC") == 0)
    {
        hKey = HKEY_CURRENT_CONFIG;
    }
    else if (strcmp (key, "HKU") == 0)
    {
        hKey = HKEY_USERS;
    }
    else
    {
        fprintf(stderr, "Unknown Registry Prefix: %s\n", key );
        ErrorExit( "Bad Prefix");
    }

    /*
    ** Open the Registry
    */
    lResult = RegOpenKeyEx(hKey, ptrKey, 0, KEY_READ, &hkeyDXVer);
    if (lResult != ERROR_SUCCESS)
    {
        if (verbose > 1)
            fprintf(stderr, "RegOpenKeyEx Result: %ld\n", lResult); 
        ErrorExit( "Cannot open Reg Key");
    }

    /*
    ** Extract the named value
    */
    memset(szVersion, 0, 255);
    lResult = RegQueryValueEx(hkeyDXVer, ptrValue, NULL, &dwType, (BYTE*)szVersion, &dwDataSize);
    if (lResult != ERROR_SUCCESS)
    {
        if (verbose > 1)
            fprintf(stderr, "RegQueryValueEx Result: %ld\n", lResult); 
        ErrorExit( "Cannot open Reg Value");
    }

    /*
    ** The moment of truth 
    ** Do they match 
    */
    if (strcmp(szVersion, expected) == 0)
    {
        if (verbose > 1)
        {
            fprintf(stderr, "checkReg: %s\n", "MATCH"); 
            fflush(stderr) ;
        }
        ExitProcess(0);

    }

    if (verbose)
        fprintf(stderr, "checkReg: Got: '%s', Expected: '%s'\n", szVersion, expected); 
    ExitProcess(1);
 }

/*----------------------------------------------------------------------------
** FUNCTION           : ErrorExit
**
** DESCRIPTION        : Error processing
**                      REport an error and terminate process
**
**
** INPUTS             : lpszMessage     - Message to display
**
** RETURNS            : Does't return
**                      Will exit with bad code
**
----------------------------------------------------------------------------*/

VOID ErrorExit (LPTSTR lpszMessage)
{ 
   fprintf(stderr, "checkReg: %s\n", lpszMessage);
   fflush(stderr) ;
   ExitProcess(1);
}