You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
261 lines
8.2 KiB
261 lines
8.2 KiB
/****************************** Module Header ******************************\
|
|
* Module Name: reg.c
|
|
*
|
|
* Copyright (c) 1985-95, Microsoft Corporation
|
|
*
|
|
* History:
|
|
* 01-02-96 a-jimhar Created based on reg.c from access351.exe
|
|
\***************************************************************************/
|
|
|
|
/*
|
|
1. on startup, check to see if we're administrator
|
|
a) use RegOpenKeyEx on HKEY_USERS\.DEFAULT\Software with read/write
|
|
access writes. if it fails, we're not administrator
|
|
b) if not, grey menu option
|
|
2. on startup
|
|
a) use RegOpenKeyEx on HKEY_CURRENTUSER\Software...
|
|
b) if it fails, create these keys with default values.
|
|
3. creating keys
|
|
a) RegCreateKeyEx
|
|
b) RegSetValue
|
|
c) RegCloseKey
|
|
|
|
*/
|
|
#include "TCHAR.h"
|
|
#include <nt.h>
|
|
#include <ntrtl.h>
|
|
#include <nturtl.h>
|
|
|
|
#include "Access.h"
|
|
|
|
BOOL DoAccessRegEntriesExist( HKEY hkeyRoot );
|
|
BOOL CheckRegEntry( HKEY hkeyRoot, LPSTR lpsz, REGSAM sam );
|
|
BOOL SetRegString( HKEY hkey, LPSTR lpszEntry, LPSTR lpszValue );
|
|
|
|
DWORD CopyKey( HKEY hkeySrc, HKEY hkeyDst, LPSTR szKey );
|
|
|
|
|
|
char szAccessRegPath[] = "Control Panel\\Accessibility";
|
|
char szHcColorRegPath[] = "Control Panel\\Colors";
|
|
char szHcDeskRegPath[] = "Control Panel\\Desktop";
|
|
|
|
/********************************************************************/
|
|
//
|
|
BOOL IsDefaultWritable( void )
|
|
{
|
|
return CheckRegEntry( HKEY_USERS, ".Default", KEY_ALL_ACCESS );
|
|
}
|
|
|
|
/********************************************************************/
|
|
BOOL DoAccessRegEntriesExist( HKEY hkeyRoot )
|
|
{
|
|
char sz[128];
|
|
strcpy( sz, szAccessRegPath );
|
|
strcat( sz, "\\StickyKeys" );
|
|
return CheckRegEntry( hkeyRoot, sz, KEY_READ ); // execute means readonly
|
|
}
|
|
|
|
/********************************************************************/
|
|
BOOL CheckRegEntry( HKEY hkeyRoot, LPSTR lpsz, REGSAM sam )
|
|
{
|
|
HKEY hkey;
|
|
BOOL fOk = (ERROR_SUCCESS == RegOpenKeyExA( hkeyRoot, lpsz, 0, sam, &hkey ));
|
|
|
|
if(fOk)
|
|
{
|
|
RegCloseKey(hkey);
|
|
}
|
|
|
|
return fOk;
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************/
|
|
BOOL SetRegString( HKEY hkey, LPSTR lpszEntry, LPSTR lpszValue )
|
|
{
|
|
DWORD dwResult;
|
|
dwResult = RegSetValueExA( hkey,
|
|
lpszEntry,
|
|
0,
|
|
REG_SZ,
|
|
lpszValue,
|
|
strlen( lpszValue ) + sizeof( TCHAR ) );
|
|
if( dwResult != ERROR_SUCCESS )
|
|
{
|
|
; // should do something like print a message
|
|
return FALSE;
|
|
}
|
|
else
|
|
return TRUE;
|
|
}
|
|
|
|
/***********************************************************************/
|
|
#define TEMP_PROFILE "Temp profile (access.cpl)"
|
|
|
|
typedef BOOL (*PFNGETDEFAULTUSERPROFILEDIRECTORYA)(LPSTR lpProfile, LPDWORD dwSize);
|
|
|
|
DWORD SaveDefaultSettings( BOOL saveL, BOOL saveU )
|
|
{
|
|
NTSTATUS Status;
|
|
DWORD iStatus = ERROR_SUCCESS;
|
|
DWORD dwSize;
|
|
HKEY hkeyDst;
|
|
BOOLEAN WasEnabled;
|
|
char acFile[MAX_PATH];
|
|
HANDLE hInstDll;
|
|
PFNGETDEFAULTUSERPROFILEDIRECTORYA pfnGetDefaultUserProfileDirectory;
|
|
|
|
// If save to Logon
|
|
if ( saveL )
|
|
{
|
|
iStatus = RegOpenKeyExA( HKEY_USERS, ".DEFAULT", 0, KEY_WRITE |
|
|
KEY_ENUMERATE_SUB_KEYS,
|
|
&hkeyDst );
|
|
if( iStatus != ERROR_SUCCESS )
|
|
return iStatus;
|
|
iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szAccessRegPath );
|
|
|
|
// a-anilk
|
|
// Now copy the colors and Desktop to .Default required for HighContrast setting
|
|
iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcColorRegPath );
|
|
iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcDeskRegPath );
|
|
|
|
RegCloseKey( hkeyDst );
|
|
}
|
|
|
|
if ( saveU )
|
|
{
|
|
hInstDll = LoadLibrary (TEXT("userenv.dll"));
|
|
|
|
if (!hInstDll) {
|
|
return (GetLastError());
|
|
}
|
|
pfnGetDefaultUserProfileDirectory = (PFNGETDEFAULTUSERPROFILEDIRECTORYA)GetProcAddress (hInstDll,
|
|
"GetDefaultUserProfileDirectoryA");
|
|
|
|
if (!pfnGetDefaultUserProfileDirectory) {
|
|
FreeLibrary (hInstDll);
|
|
return (GetLastError());
|
|
}
|
|
|
|
dwSize = MAX_PATH;
|
|
if (!pfnGetDefaultUserProfileDirectory(acFile, &dwSize)) {
|
|
FreeLibrary (hInstDll);
|
|
return (GetLastError());
|
|
}
|
|
|
|
FreeLibrary (hInstDll);
|
|
|
|
Status = RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, TRUE, FALSE, &WasEnabled);
|
|
|
|
if (!NT_SUCCESS(Status)) return iStatus;
|
|
|
|
strcat(acFile,"\\ntuser.dat");
|
|
iStatus = RegLoadKeyA(HKEY_USERS, TEMP_PROFILE, acFile);
|
|
|
|
if (iStatus == ERROR_SUCCESS) {
|
|
|
|
iStatus = RegOpenKeyExA( HKEY_USERS, TEMP_PROFILE, 0, KEY_WRITE |
|
|
KEY_ENUMERATE_SUB_KEYS, &hkeyDst );
|
|
if( iStatus == ERROR_SUCCESS ) {
|
|
|
|
iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szAccessRegPath );
|
|
// a-anilk
|
|
// Now copy the colors and Desktop to .Default required for HighContrast setting
|
|
iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcColorRegPath );
|
|
iStatus = CopyKey( HKEY_CURRENT_USER, hkeyDst, szHcDeskRegPath );
|
|
|
|
RegCloseKey( hkeyDst );
|
|
}
|
|
|
|
RegUnLoadKeyA(HKEY_USERS, TEMP_PROFILE);
|
|
}
|
|
RtlAdjustPrivilege(SE_RESTORE_PRIVILEGE, WasEnabled, FALSE, &WasEnabled);
|
|
}
|
|
return iStatus;
|
|
}
|
|
|
|
/***********************************************************************/
|
|
// CopyKey( hKey, hKeyDst, name )
|
|
// create the destination key
|
|
// for each value
|
|
// CopyValue
|
|
// for each subkey
|
|
// CopyKey
|
|
|
|
DWORD CopyKey( HKEY hkeySrc, HKEY hkeyDst, LPSTR szKey )
|
|
{
|
|
HKEY hkeyOld, hkeyNew;
|
|
char szValue[128];
|
|
char szData[128];
|
|
char szBuffer[128];
|
|
DWORD iStatus;
|
|
UINT nValue, nKey;
|
|
UINT iValueLen, iDataLen;
|
|
DWORD dwType;
|
|
|
|
iStatus = RegOpenKeyExA( hkeySrc, szKey, 0, KEY_WRITE |
|
|
KEY_ENUMERATE_SUB_KEYS, &hkeyOld );
|
|
if( iStatus != ERROR_SUCCESS )
|
|
return iStatus;
|
|
iStatus = RegOpenKeyExA( hkeyDst, szKey, 0, KEY_WRITE |
|
|
KEY_ENUMERATE_SUB_KEYS, &hkeyNew );
|
|
if( iStatus != ERROR_SUCCESS )
|
|
{
|
|
iStatus = RegCreateKeyExA( hkeyDst, szKey, 0, "", 0, KEY_WRITE |
|
|
KEY_ENUMERATE_SUB_KEYS, NULL, &hkeyNew, NULL );
|
|
if( iStatus != ERROR_SUCCESS )
|
|
{
|
|
RegCloseKey( hkeyOld );
|
|
return iStatus;
|
|
}
|
|
}
|
|
//*********** copy the values **************** //
|
|
|
|
for( nValue = 0, iValueLen=sizeof szValue, iDataLen=sizeof szValue;
|
|
ERROR_SUCCESS == (iStatus = RegEnumValueA(hkeyOld,
|
|
nValue,
|
|
szValue,
|
|
&iValueLen,
|
|
NULL, // reserved
|
|
&dwType, // don't need type
|
|
szData,
|
|
&iDataLen ) );
|
|
nValue ++, iValueLen=sizeof szValue, iDataLen=sizeof szValue )
|
|
{
|
|
iStatus = RegSetValueExA( hkeyNew,
|
|
szValue,
|
|
0, // reserved
|
|
dwType,
|
|
szData,
|
|
iDataLen);
|
|
}
|
|
if( iStatus != ERROR_NO_MORE_ITEMS )
|
|
{
|
|
RegCloseKey( hkeyOld );
|
|
RegCloseKey( hkeyNew );
|
|
return iStatus;
|
|
}
|
|
|
|
//*********** copy the subtrees ************** //
|
|
|
|
for( nKey = 0;
|
|
ERROR_SUCCESS == (iStatus = RegEnumKeyA(hkeyOld,nKey,szBuffer,sizeof(szBuffer)));
|
|
nKey ++ )
|
|
{
|
|
iStatus = CopyKey( hkeyOld, hkeyNew, szBuffer );
|
|
if( iStatus != ERROR_NO_MORE_ITEMS && iStatus != ERROR_SUCCESS )
|
|
{
|
|
RegCloseKey( hkeyOld );
|
|
RegCloseKey( hkeyNew );
|
|
return iStatus;
|
|
}
|
|
}
|
|
RegCloseKey( hkeyOld );
|
|
RegCloseKey( hkeyNew );
|
|
if( iStatus == ERROR_NO_MORE_ITEMS )
|
|
return ERROR_SUCCESS;
|
|
else
|
|
return iStatus;
|
|
}
|