00001
#include <stdio.h>
00002
#include <stdlib.h>
00003
#include <string.h>
00004
#include <memory.h>
00005
#include <wchar.h>
00006
#include "windows.h"
00007
#include "winreg.h"
00008
00009 #define TEST_STRING "Test String"
00010 #define TEST_STRING_W L"Test String"
00011
00012
00013
00014 BOOLEAN
00015 AdjustPrivilege(
00016 PSTR SecurityNameString
00017 )
00018 {
00019 HANDLE TokenHandle;
00020 LUID_AND_ATTRIBUTES LuidAndAttributes;
00021
00022
00023 TOKEN_PRIVILEGES TokenPrivileges;
00024 TOKEN_PRIVILEGES PreviousTokenPrivileges;
00025
DWORD ReturnLength;
00026
00027
if( !OpenProcessToken( GetCurrentProcess(),
00028 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
00029 &TokenHandle ) ) {
00030 printf(
"OpenProcessToken failed \n" );
00031
return(
FALSE );
00032 }
00033
00034
00035
00036
if( !LookupPrivilegeValue(
NULL,
00037 SecurityNameString,
00038 &( LuidAndAttributes.Luid ) ) ) {
00039 printf(
"LookupPrivilegeValue failed, Error = %#x \n", GetLastError() );
00040
return(
FALSE );
00041 }
00042
00043 LuidAndAttributes.Attributes = SE_PRIVILEGE_ENABLED;
00044 TokenPrivileges.PrivilegeCount = 1;
00045 TokenPrivileges.Privileges[0] = LuidAndAttributes;
00046
00047
if( !AdjustTokenPrivileges( TokenHandle,
00048
FALSE,
00049 &TokenPrivileges,
00050 0,
00051
NULL,
00052
NULL ) ) {
00053 printf(
"AdjustTokenPrivileges failed, Error = %#x \n", GetLastError() );
00054
return(
FALSE );
00055 }
00056
00057
00058
if( GetLastError() != NO_ERROR ) {
00059
return(
FALSE );
00060 }
00061
00062
return(
TRUE );
00063 }
00064
00065
00066
00067 #define ENVIRONMENT_NAME L"Environment"
00068 #define TESTKEY_NAME L"TestKey"
00069 #define TESTKEY_FULL_NAME L"Environment\\TestKey"
00070 #define KEY1_NAME L"Key1"
00071 #define KEY1_FULL_NAME L"Environment\\TestKey\\Key1"
00072 #define KEY2_NAME L"Key2"
00073 #define KEY2_FULL_NAME L"Environment\\TestKey\\Key2"
00074 #define VALUE_NAME L"123"
00075 #define VALUE_DATA L"This is a string"
00076
00077
00078
00079
INT __cdecl
00080 main()
00081 {
00082
DWORD Status;
00083
00084 HKEY TestKeyHandle;
00085 HKEY Key1Handle;
00086 HKEY Key2Handle;
00087 HKEY EnvironmentHandle;
00088
00089 WCHAR ValueData[] =
VALUE_DATA;
00090
00091 WCHAR BufferForKeyName[100];
00092 WCHAR BufferForKeyClass[100];
00093 WCHAR BufferForValueEntryName[100];
00094
BYTE BufferForValueEntryData[100];
00095
00096
00097
DWORD DataType;
00098
DWORD DataSize;
00099
DWORD NameSize;
00100
DWORD ClassSize;
00101
00102
00103
DWORD cSubKeys;
00104
DWORD cbMaxSubkey;
00105
DWORD cbMaxClass;
00106
DWORD cValues;
00107
DWORD vbMaxValueName;
00108
DWORD cbMaxValueData;
00109
DWORD cbSecurityDescriptor;
00110 FILETIME ftLastWriteTime;
00111
00112
00113
BYTE BufferForSecurityDescriptor[2048];
00114
00115 HANDLE NotificationEvent;
00116
00117 PWSTR File1 =
L"d:\\File1";
00118 PWSTR File2 =
L"d:\\File2";
00119
DWORD Disposition;
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 NotificationEvent = CreateEvent(
NULL,
00144
FALSE,
00145
FALSE,
00146
NULL );
00147
00148
if( NotificationEvent ==
NULL ) {
00149 printf(
"CreateEvent failed, ErrorCode = %d \n", GetLastError() );
00150 }
00151
00152
00153
00154
Status = RegOpenKeyExW( HKEY_CURRENT_USER,
00155
TESTKEY_FULL_NAME,
00156 0,
00157 MAXIMUM_ALLOWED,
00158 &TestKeyHandle );
00159
00160
if(
Status != 0 ) {
00161 printf(
"RegOpenKeyExW failed, Status = %d \n",
Status );
00162 }
else {
00163 printf(
"RegOpenKeyExW succeeded \n" );
00164 }
00165
00166
00167
Status = RegOpenKeyW( HKEY_CURRENT_USER,
00168
ENVIRONMENT_NAME,
00169 &EnvironmentHandle );
00170
00171
if(
Status != 0 ) {
00172 printf(
"RegOpenKeyW failed, Status = %d \n",
Status );
00173 }
else {
00174 printf(
"RegOpenKeyW succeeded \n" );
00175 }
00176
00177
00178
00179
00180
00181
00182
Status = RegCreateKeyExW( TestKeyHandle,
00183
KEY1_NAME,
00184
NULL,
00185
NULL,
00186 REG_OPTION_NON_VOLATILE,
00187 MAXIMUM_ALLOWED,
00188
NULL,
00189 &Key1Handle,
00190 &Disposition );
00191
00192
if(
Status != 0 ) {
00193 printf(
"RegCreateKeyExW failed, Status = %d \n",
Status );
00194 }
else {
00195 printf(
"RegCreateKeyExW succeeded \n" );
00196 }
00197
00198
00199
00200
Status = RegCreateKeyW( TestKeyHandle,
00201
KEY2_NAME,
00202 &Key2Handle );
00203
00204
00205
if(
Status != 0 ) {
00206 printf(
"RegCreateKeyW failed, Status = %d \n",
Status );
00207 }
else {
00208 printf(
"RegCreateKeyW succeeded \n" );
00209 }
00210
00211
00212
Status = RegSetValueExW( Key1Handle,
00213
VALUE_NAME,
00214
NULL,
00215 REG_SZ,
00216 ValueData,
00217
sizeof( ValueData ) );
00218
00219
if(
Status != 0 ) {
00220 printf(
"RegSetValueExW failed, Status = %d \n",
Status );
00221 }
else {
00222 printf(
"RegSetValueExW succeeded \n" );
00223 }
00224
00225
00226
Status = RegSetValueW( Key1Handle,
00227
NULL,
00228 REG_SZ,
00229 ValueData,
00230
sizeof( ValueData ) );
00231
00232
if(
Status != 0 ) {
00233 printf(
"RegSetValueW failed, Status = %d \n",
Status );
00234 }
else {
00235 printf(
"RegSetValueW succeeded \n" );
00236 }
00237
00238
00239
00240
Status = RegFlushKey( Key1Handle );
00241
if(
Status != 0 ) {
00242 printf(
"RegFlushKey failed, Status = %d \n",
Status );
00243 }
else {
00244 printf(
"RegFlushKey succeeded \n" );
00245 }
00246
00247
00248
00249
00250 DataSize =
sizeof( BufferForValueEntryData );
00251 memset( BufferForValueEntryData,
'\0', DataSize );
00252
Status = RegQueryValueExW( Key1Handle,
00253
VALUE_NAME,
00254
NULL,
00255 &DataType,
00256 BufferForValueEntryData,
00257 &DataSize );
00258
00259
if(
Status != 0 ) {
00260 printf(
"RegQueryValueExW failed, Status = %d \n",
Status );
00261 }
else {
00262 printf(
"RegQueryValueExW succeeded \n" );
00263 }
00264
00265
00266
00267 DataSize =
sizeof( BufferForValueEntryData );
00268 memset( BufferForValueEntryData,
'\0', DataSize );
00269
Status = RegQueryValueW( Key1Handle,
00270
NULL,
00271 BufferForValueEntryData,
00272 &DataSize );
00273
00274
00275
if(
Status != 0 ) {
00276 printf(
"RegQueryValueW failed, Status = %d \n",
Status );
00277 }
else {
00278 printf(
"RegQueryValueW succeeded \n" );
00279 }
00280
00281
00282 DataSize =
sizeof( BufferForValueEntryData );
00283 memset( BufferForValueEntryData,
'X', DataSize );
00284 NameSize =
sizeof( BufferForValueEntryName );
00285 memset( BufferForValueEntryName,
'X', NameSize );
00286
00287
Status = RegEnumValueW( Key1Handle,
00288 0,
00289 BufferForValueEntryName,
00290 &NameSize,
00291
NULL,
00292 &DataType,
00293 BufferForValueEntryData,
00294 &DataSize );
00295
00296
00297
if(
Status != 0 ) {
00298 printf(
"RegEnumValueW failed, Status = %d \n",
Status );
00299 }
else {
00300 printf(
"RegEnumValueW succeeded \n" );
00301 }
00302
00303
00304 NameSize =
sizeof( BufferForKeyName );
00305 ClassSize =
sizeof( BufferForKeyClass );
00306
Status = RegEnumKeyExW( TestKeyHandle,
00307 0,
00308 BufferForKeyName,
00309 &NameSize,
00310
NULL,
00311 BufferForKeyClass,
00312 &ClassSize,
00313 &ftLastWriteTime );
00314
00315
if(
Status != 0 ) {
00316 printf(
"RegEnumKeyExW failed, Status = %d \n",
Status );
00317 }
else {
00318 printf(
"RegEnumKeyExW succeeded \n" );
00319 }
00320
00321
00322 NameSize =
sizeof( BufferForKeyName );
00323
Status = RegEnumKeyW( TestKeyHandle,
00324 0,
00325 BufferForKeyName,
00326 &NameSize );
00327
00328
if(
Status != 0 ) {
00329 printf(
"RegEnumKeyW failed, Status = %d \n",
Status );
00330 }
else {
00331 printf(
"RegEnumKeyW succeeded \n" );
00332 }
00333
00334
00335 ClassSize =
sizeof( BufferForKeyClass );
00336
Status = RegQueryInfoKeyW( Key1Handle,
00337 BufferForKeyClass,
00338 &ClassSize,
00339
NULL,
00340 &cSubKeys,
00341 &cbMaxSubkey,
00342 &cbMaxClass,
00343 &cValues,
00344 &vbMaxValueName,
00345 &cbMaxValueData,
00346 &cbSecurityDescriptor,
00347 &ftLastWriteTime );
00348
00349
00350
00351
if(
Status != 0 ) {
00352 printf(
"RegQueryInfoKeyW failed, Status = %d \n",
Status );
00353 }
else {
00354 printf(
"RegQueryInfoKeyW succeeded \n" );
00355 }
00356
00357
00358
Status = RegGetKeySecurity( Key1Handle,
00359 DACL_SECURITY_INFORMATION,
00360 ( PSECURITY_DESCRIPTOR )BufferForSecurityDescriptor,
00361 &cbSecurityDescriptor );
00362
00363
if(
Status != 0 ) {
00364 printf(
"RegGetKeySecurity failed, Status = %d \n",
Status );
00365 }
else {
00366 printf(
"RegGetKeySecurity succeeded \n" );
00367 }
00368
00369
00370
00371
Status = RegSetKeySecurity( Key1Handle,
00372 DACL_SECURITY_INFORMATION,
00373 ( PSECURITY_DESCRIPTOR )BufferForSecurityDescriptor );
00374
00375
if(
Status != 0 ) {
00376 printf(
"RegSetKeySecurity failed, Status = %d \n",
Status );
00377 }
else {
00378 printf(
"RegSetKeySecurity succeeded \n" );
00379 }
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
Status = RegDeleteValueW( Key1Handle,
00408
VALUE_NAME );
00409
00410
00411
if(
Status != 0 ) {
00412 printf(
"RegDeleteValueW failed, Status = %d \n",
Status );
00413 }
else {
00414 printf(
"RegDeleteValueW succeeded \n" );
00415 }
00416
00417
00418
Status = RegCloseKey( Key1Handle );
00419
00420
if(
Status != 0 ) {
00421 printf(
"RegCloseKey failed, Status = %d \n",
Status );
00422 }
else {
00423 printf(
"RegCloseKey succeeded \n" );
00424 }
00425
00426
Status = RegDeleteKeyW( TestKeyHandle,
00427
KEY1_NAME );
00428
00429
if(
Status != 0 ) {
00430 printf(
"RegCloseKey failed, Status = %d \n",
Status );
00431 }
else {
00432 printf(
"RegCloseKey succeeded \n" );
00433 }
00434
00435
00436
Status = RegNotifyChangeKeyValue( HKEY_CURRENT_USER,
00437
TRUE,
00438 REG_NOTIFY_CHANGE_NAME |
00439 REG_NOTIFY_CHANGE_ATTRIBUTES |
00440 REG_NOTIFY_CHANGE_LAST_SET |
00441 REG_NOTIFY_CHANGE_SECURITY,
00442 NotificationEvent,
00443
TRUE );
00444
00445
if(
Status != 0 ) {
00446 printf(
"RegNotifyChangeKeyValue failed, Status = %d \n",
Status );
00447 }
else {
00448 printf(
"RegNotifyChangeKeyValue succeeded \n" );
00449 }
00450
00451
00452
00453
00454
00455
00456 CloseHandle( NotificationEvent );
00457
00458 RegCloseKey( Key2Handle );
00459 RegDeleteKeyW( TestKeyHandle,
00460
KEY2_NAME );
00461
00462
00463 }