Subversion Repositories DevTools

Rev

Rev 5659 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4192 dpurdie 1
/*============================================================================ 
6177 dpurdie 2
** COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
4192 dpurdie 3
**============================================================================
4
**
5
**  Project/Product : 
6
**  Filename        : checkReg.c
7
**  Author(s)       : dpurdie
8
**  Created         : 01-Apr-14
9
**
10
**  Description     : Windows program to
11
**                    Verify correct values in specified registry keys
12
** 
13
**                    Usage:
14
**                    checkReg <options>* Key Value
15
**                    Where options are:
16
**                    
17
**
18
**  Information     :
19
**   Compiler       : ANSI C++
20
**   Target         : 
21
**
22
***==========================================================================*/
23
 
24
 
25
#include <stdio.h>
26
#include <windows.h> 
27
 
28
VOID ErrorExit(LPTSTR);
29
 
30
/*----------------------------------------------------------------------------
31
** FUNCTION           : main
32
**
33
** DESCRIPTION        : Mainentry point
34
**
35
**
36
** INPUTS             : argc            - Number of args
37
**                      argv            - Address of args
38
**
39
** RETURNS            :
40
**
41
----------------------------------------------------------------------------*/
42
 
43
int main(int argc, char *argv[])
44
{
45
   int      argp;
46
   char     *key = NULL;
47
   char     *expected = NULL;
48
   int      verbose = 0;
49
 
50
   char     *ptrKey;
51
   char     *ptrValue;
52
   char     *ptr;
53
 
54
   HKEY     hKey;
55
   HKEY     hkeyDXVer;
56
   long     lResult;
57
   DWORD    dwType;
58
   char     szVersion[255];
59
   DWORD    dwDataSize = 255;
60
 
61
    /*
62
    **  Process command line arguments
63
    **  Options for this program will be located first
64
    */
65
    for ( argp = 1; argp < argc ; argp++ )
66
    {
67
        /*
68
        **  Scan until first non option argument
69
        */
70
        if ( *argv[argp] != '-' )
71
            break;
72
 
73
        if ( _strnicmp( argv[argp], "-key:", 5 ) == 0)
74
        {
75
            key = argv[argp] + 5;
76
        }
77
        else if ( _strnicmp( argv[argp], "-value:", 7 ) == 0)
78
        {
79
            expected = argv[argp] + 7;
80
        }
81
        else if ( _strnicmp( argv[argp], "-v", 2 ) == 0)
82
        {
83
            verbose++;
84
        }
85
        else
86
        {
87
            fprintf(stderr, "Unknown option: %s\n", argv[argp] );
88
            ErrorExit( "Bad Option");
89
        }
90
    }
91
 
92
   /*
93
   **  Need both a key and a value
94
   */
95
    if (key == NULL || expected == NULL)
96
    {
5659 dpurdie 97
        fprintf(stderr, "Both a key and a value must be specified\n" );
4192 dpurdie 98
        ErrorExit( "Bad Option");
99
    }
100
 
101
    /*
102
    ** Need to split the Key into bits 
103
    **  First bit to specify the registry
104
    **  Last bit is the final key
105
    */
106
 
107
    /*
108
    **  Scan for the first \ or /
109
    **  Replace it with a null - will have the first string
110
    */
111
    for (ptrKey = key; *ptrKey; ptrKey++)
112
    {
113
        if (*ptrKey == '/')
114
            *ptrKey = '\\';
115
 
116
        if (*ptrKey == '\\')
117
        {
118
            *ptrKey = 0;
119
            ptrKey++;
120
            break;
121
        }
122
    }
123
    if ( !*ptrKey)
124
    {
5659 dpurdie 125
        fprintf(stderr, "Could not split the key apart\n" );
4192 dpurdie 126
        ErrorExit( "Bad Key");
127
    }
128
 
129
    /*
130
    ** Scan for the last \ 
131
    ** Will have the tail part
132
    */
133
    ptrValue = NULL;
134
    for (ptr = ptrKey; *ptr; ptr++)
135
    {
136
        if (*ptr == '/')
137
            *ptr = '\\';
138
 
139
        if (*ptr == '\\')
140
        {
141
            ptrValue = ptr;
142
        }
143
    }
144
    if (ptrValue != NULL)
145
    {
146
        *ptrValue = 0;
147
        ptrValue++;
148
    }
149
 
150
    if ( ptrValue == NULL || !*ptrValue)
151
    {
5659 dpurdie 152
        fprintf(stderr, "Could not split the key end apart\n" );
4192 dpurdie 153
        ErrorExit( "Bad Key");
154
    }
155
 
156
    /*
157
    ** Convert Prefix into known key
158
    */
159
    if (strcmp (key, "HKLM") == 0)
160
    {
161
        hKey = HKEY_LOCAL_MACHINE;
162
    }
163
    else if (strcmp (key, "HKCU") == 0)
164
    {
165
        hKey = HKEY_CURRENT_USER;
166
    }
167
    else if (strcmp (key, "HKCR") == 0)
168
    {
169
        hKey = HKEY_CLASSES_ROOT;
170
    }
171
    else if (strcmp (key, "HKCC") == 0)
172
    {
173
        hKey = HKEY_CURRENT_CONFIG;
174
    }
175
    else if (strcmp (key, "HKU") == 0)
176
    {
177
        hKey = HKEY_USERS;
178
    }
179
    else
180
    {
181
        fprintf(stderr, "Unknown Registry Prefix: %s\n", key );
182
        ErrorExit( "Bad Prefix");
183
    }
184
 
185
    /*
186
    ** Open the Registry
187
    */
188
    lResult = RegOpenKeyEx(hKey, ptrKey, 0, KEY_READ, &hkeyDXVer);
189
    if (lResult != ERROR_SUCCESS)
190
    {
191
        if (verbose > 1)
192
            fprintf(stderr, "RegOpenKeyEx Result: %ld\n", lResult); 
193
        ErrorExit( "Cannot open Reg Key");
194
    }
195
 
196
    /*
197
    ** Extract the named value
198
    */
199
    memset(szVersion, 0, 255);
200
    lResult = RegQueryValueEx(hkeyDXVer, ptrValue, NULL, &dwType, (BYTE*)szVersion, &dwDataSize);
201
    if (lResult != ERROR_SUCCESS)
202
    {
203
        if (verbose > 1)
204
            fprintf(stderr, "RegQueryValueEx Result: %ld\n", lResult); 
205
        ErrorExit( "Cannot open Reg Value");
206
    }
207
 
208
    /*
209
    ** The moment of truth 
210
    ** Do they match 
211
    */
212
    if (strcmp(szVersion, expected) == 0)
213
    {
214
        if (verbose > 1)
215
        {
216
            fprintf(stderr, "checkReg: %s\n", "MATCH"); 
217
            fflush(stderr) ;
218
        }
219
        ExitProcess(0);
220
 
221
    }
222
 
223
    if (verbose)
224
        fprintf(stderr, "checkReg: Got: '%s', Expected: '%s'\n", szVersion, expected); 
5637 dpurdie 225
    ExitProcess(1);
4192 dpurdie 226
 }
227
 
228
/*----------------------------------------------------------------------------
229
** FUNCTION           : ErrorExit
230
**
231
** DESCRIPTION        : Error processing
232
**                      REport an error and terminate process
233
**
234
**
235
** INPUTS             : lpszMessage     - Message to display
236
**
237
** RETURNS            : Does't return
238
**                      Will exit with bad code
239
**
240
----------------------------------------------------------------------------*/
241
 
242
VOID ErrorExit (LPTSTR lpszMessage)
243
{ 
244
   fprintf(stderr, "checkReg: %s\n", lpszMessage);
245
   fflush(stderr) ;
246
   ExitProcess(1);
247
} 
248
 
249