Пример #1
0
LONG reg_save_and_restore_tst(HKEY hKey,char crl_c)
{
	
	LONG Return_Info;
	DWORD dwFlags;
	LPCTSTR lpFile="C:\\gh.hive";
	if(crl_c == 'S')
	{
	//LPSECURITY_ATTRIBUTES lpSecurityAttributes=NULL;
	Return_Info=	RegSaveKey(__in      hKey,__in      lpFile,NULL);
		if(Return_Info == ERROR_SUCCESS)
		{
			printf("save is [ok]\n");
		}
		else
		{
			printf("Save Key is %d die\n",Return_Info);
		}

		return Return_Info;
	}

	if(crl_c=='R')
	{
		dwFlags=REG_FORCE_RESTORE;// REG_FORCE_RESTORE or REG_WHOLE_HIVE_VOLATILE



		Return_Info=RegRestoreKey(__in  hKey,__in  lpFile,__in  dwFlags);
		return Return_Info;
	}
	Return_Info=!ERROR_SUCCESS;
	return Return_Info;
}
Пример #2
0
// Restores a saved registry tree from the specified file to the specified key position
bool RegKey::RestoreRegistry(LPCTSTR pszFileName, LPCTSTR pszKeyName, HKEY hBaseKey /* = HKEY_CURRENT_USER */, LPCTSTR pszMachineName /* = NULL */)
{
	if (pszFileName == NULL || pszKeyName == NULL)
	{
		iLastErrorCode_ = ERROR_BAD_ARGUMENTS;
		return false;
	}

	CloseKey(); // Close current active key and base key if remote

	// Open the key
	if (!OpenKey(pszKeyName, true, hBaseKey, pszMachineName))
		return false;

	LONG lRetValue = RegRestoreKey(hTheKey_, pszFileName, REG_OPTION_NON_VOLATILE);
	if (lRetValue == ERROR_CALL_NOT_IMPLEMENTED) // NT ONLY
	{
		iLastErrorCode_ = ERROR_CALL_NOT_IMPLEMENTED;
		return false;
	}

	if (lRetValue != ERROR_SUCCESS)
	{
		iLastErrorCode_ = GetLastError();
		return false;
	}

	iLastErrorCode_ = ERROR_SUCCESS;
	return true;
}
Пример #3
0
BOOL CRegistry::RestoreKey(LPCTSTR lpFileName)
{
	ASSERT(m_hKey);
	ASSERT(lpFileName);

	long lReturn = RegRestoreKey(m_hKey, lpFileName, REG_WHOLE_HIVE_VOLATILE);

	if (lReturn == ERROR_SUCCESS)
		return TRUE;

	return FALSE;
}
Пример #4
0
BOOL RegistryOp::RestoreKey(LPWSTR lpFileName)
{
	assert(m_hKey);
	assert(lpFileName);
	long lReturn = RegRestoreKey(m_hKey, lpFileName, REG_WHOLE_HIVE_VOLATILE);
	if (lReturn == ERROR_SUCCESS)
	{
		return TRUE;
	}
	DOLOG("RegRestoreKey ERROR return:" + lReturn);
	return FALSE;
}
Пример #5
0
//恢复
bool CGameGoMemory::RestoreKey(TCHAR * szFileName)
{
	long int lReturn=RegRestoreKey(m_hRegKey,szFileName,REG_WHOLE_HIVE_VOLATILE);
	return (lReturn==ERROR_SUCCESS);
}
Пример #6
0
static BOOL ImportRegistryFile(HWND hWnd)
{
    BOOL bRet = FALSE;
    OPENFILENAME ofn;
    WCHAR Caption[128], szTitle[512], szText[512];
    HKEY hKeyRoot;
    LPCWSTR pszKeyPath;

    /* Figure out in which key path we are importing */
    pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);

    InitOpenFileName(hWnd, &ofn);
    LoadStringW(hInst, IDS_IMPORT_REG_FILE, Caption, COUNT_OF(Caption));
    ofn.lpstrTitle = Caption;
    ofn.Flags |= OFN_ENABLESIZING;
    /*    ofn.lCustData = ;*/
    if (GetOpenFileName(&ofn))
    {
        /* Look at the extension of the file to determine its type */
        if (ofn.nFileExtension >= 1 &&
            _wcsicmp(ofn.lpstrFile + ofn.nFileExtension, L"reg") == 0) /* REGEDIT4 or Windows Registry Editor Version 5.00 */
        {
            /* Open the file */
            FILE* fp = _wfopen(ofn.lpstrFile, L"r");

            /* Import it */
            if (fp == NULL || !import_registry_file(fp))
            {
                /* Error opening the file */
                LoadStringW(hInst, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle));
                LoadStringW(hInst, IDS_IMPORT_ERROR, szText, COUNT_OF(szText));
                InfoMessageBox(hWnd, MB_OK | MB_ICONERROR, szTitle, szText, ofn.lpstrFile);
                bRet = FALSE;
            }
            else
            {
                /* Show successful import */
                LoadStringW(hInst, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle));
                LoadStringW(hInst, IDS_IMPORT_OK, szText, COUNT_OF(szText));
                InfoMessageBox(hWnd, MB_OK | MB_ICONINFORMATION, szTitle, szText, ofn.lpstrFile);
                bRet = TRUE;
            }

            /* Close the file */
            if (fp) fclose(fp);
        }
        else /* Registry Hive Files */
        {
            LoadStringW(hInst, IDS_QUERY_IMPORT_HIVE_CAPTION, szTitle, COUNT_OF(szTitle));
            LoadStringW(hInst, IDS_QUERY_IMPORT_HIVE_MSG, szText, COUNT_OF(szText));

            /* Display a confirmation message */
            if (MessageBoxW(g_pChildWnd->hWnd, szText, szTitle, MB_ICONWARNING | MB_YESNO) == IDYES)
            {
                LONG lResult;
                HKEY hSubKey;

                /* Open the subkey */
                lResult = RegOpenKeyExW(hKeyRoot, pszKeyPath, 0, KEY_WRITE, &hSubKey);
                if (lResult == ERROR_SUCCESS)
                {
                    /* Enable the 'restore' privilege, restore the hive then disable the privilege */
                    EnablePrivilege(SE_RESTORE_NAME, NULL, TRUE);
                    lResult = RegRestoreKey(hSubKey, ofn.lpstrFile, REG_FORCE_RESTORE);
                    EnablePrivilege(SE_RESTORE_NAME, NULL, FALSE);

                    /* Flush the subkey and close it */
                    RegFlushKey(hSubKey);
                    RegCloseKey(hSubKey);
                }

                /* Set the return value */
                bRet = (lResult == ERROR_SUCCESS);

                /* Display error, if any */
                if (!bRet) ErrorMessageBox(hWnd, Caption, lResult);
            }
        }
    }
    else
    {
        CheckCommDlgError(hWnd);
    }

    /* refresh tree and list views */
    RefreshTreeView(g_pChildWnd->hTreeWnd);
    pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hKeyRoot);
    RefreshListView(g_pChildWnd->hListWnd, hKeyRoot, pszKeyPath);

    return bRet;
}
Пример #7
0
BOOL RestoreSettings(HWND hWnd)
{	
	// Check whether locate32 is running
	HWND hLocateSTWindow=FindWindow("LOCATEAPPST",NULL);
	if (hLocateSTWindow!=NULL)
	{
		char szText[100];
		LoadString(hInst,IDS_LOCATE32RUNNING,szText,100);
		if (MessageBox(hWnd,szText,NULL,MB_OKCANCEL|MB_ICONINFORMATION))
			return FALSE;
	}



	char szPath[MAX_PATH]="";
	char szTitle[100],szFilter[200];
	OSVERSIONINFO ve;
	ve.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
	if (GetVersionEx(&ve))
	{
		if (ve.dwPlatformId==VER_PLATFORM_WIN32_NT)
			LoadString(hInst,IDS_RESTOREFILTER,szFilter,200);
		else
			LoadString(hInst,IDS_RESTOREFILTER9x,szFilter,200);
	}
	else
		LoadString(hInst,IDS_RESTOREFILTER,szFilter,200);
	
	LoadString(hInst,IDS_RESTORESETTINGS,szTitle,100);

	for (int i=0;szFilter[i]!='\0';i++)
	{
		if (szFilter[i]=='|')
			szFilter[i]='\0';
	}

	OPENFILENAME ofn;
	ZeroMemory(&ofn,sizeof(OPENFILENAME));
	ofn.lStructSize=OPENFILENAME_SIZE_VERSION_400;
	ofn.hwndOwner=hWnd;
	ofn.hInstance=hInst;
	ofn.lpstrFilter=szFilter;
	ofn.lpstrFile=szPath;
	ofn.nMaxFile=MAX_PATH;
	ofn.lpstrTitle=szTitle;
	ofn.Flags=OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_LONGNAMES|OFN_HIDEREADONLY;
	ofn.lpstrDefExt="*.reg";

	if (!GetOpenFileName(&ofn))
		return FALSE;
	
	int nDotIndex;
	for (nDotIndex=(int)strlen(szPath)-1;nDotIndex>=0 && szPath[nDotIndex]!='.';nDotIndex--);

	if (nDotIndex>=0 && _stricmp(szPath+nDotIndex+1,"reg")==0)
	{
		char szBackup[MAX_PATH];
		CopyMemory(szBackup,szPath,nDotIndex+1);
		strcpy_s(szBackup+nDotIndex+1,MAX_PATH-nDotIndex-1,"old.reg");
				
		// Backing up
		char szCommand[2000];
		sprintf_s(szCommand,2000,"regedit /ea \"%s\" HKEY_CURRENT_USER\\Software\\Update",szBackup);

		PROCESS_INFORMATION pi;
		STARTUPINFO si; // Ansi and Unicode versions are same
		ZeroMemory(&si,sizeof(STARTUPINFO));
		si.cb=sizeof(STARTUPINFO);
		
		if (CreateProcess(NULL,szCommand,NULL,
			NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS,
			NULL,NULL,&si,&pi))
		{
			WaitForSingleObject(pi.hProcess,2000);
			CloseHandle(pi.hThread);
			CloseHandle(pi.hProcess);	
		}
		else
			ShowError(hWnd,IDS_ERRORCANNOTRUNREGEDIT,GetLastError());

		
		
		// Restore key
		DeleteSubKey(HKEY_CURRENT_USER,"SOFTWARE\\Update");
		sprintf_s(szCommand,2000,"regedit /s \"%s\"",szPath);

		ZeroMemory(&si,sizeof(STARTUPINFO));
		si.cb=sizeof(STARTUPINFO);
		
		if (CreateProcess(NULL,szCommand,NULL,
			NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS,
			NULL,NULL,&si,&pi))
		{
			CloseHandle(pi.hThread);
			CloseHandle(pi.hProcess);	
		}
		else
			ShowError(hWnd,IDS_ERRORCANNOTRUNREGEDIT,GetLastError());


		return TRUE;		
	}
	
	// First, check that we can restore key
	HKEY hRegKey;
	LONG lRet=RegCreateKeyEx(HKEY_CURRENT_USER,"SOFTWARE\\Update_tmpTmp",
		0,NULL,REG_OPTION_BACKUP_RESTORE,KEY_ALL_ACCESS,NULL,&hRegKey,NULL);
	if (lRet!=ERROR_SUCCESS)
	{
		ShowError(hWnd,IDS_ERRORCANNOTCREATEKEY,lRet);
		return FALSE;
	}
	lRet=RegRestoreKey(hRegKey,szPath,0);
	RegCloseKey(hRegKey);		
	DeleteSubKey(HKEY_CURRENT_USER,"SOFTWARE\\Update_tmpTmp");

	if (lRet!=ERROR_SUCCESS)
	{
		ShowError(hWnd,IDS_ERRORCANNOTRESTOREKEY,lRet);
		return FALSE;
	}

	
	
	// Clear existing key
	DeleteSubKey(HKEY_CURRENT_USER,"SOFTWARE\\Update");
	
	// Restore key
	lRet=RegCreateKeyEx(HKEY_CURRENT_USER,"SOFTWARE\\Update",
		0,NULL,REG_OPTION_BACKUP_RESTORE,KEY_ALL_ACCESS,NULL,&hRegKey,NULL);

	if (lRet!=ERROR_SUCCESS)
	{
		ShowError(hWnd,IDS_ERRORCANNOTCREATEKEY,lRet);
		return FALSE;
	}
	
	
	lRet=RegRestoreKey(hRegKey,szPath,0);
	RegCloseKey(hRegKey);		
	if (lRet!=ERROR_SUCCESS)
	{
		ShowError(hWnd,IDS_ERRORCANNOTRESTOREKEY,lRet);
		return FALSE;
	}
	return TRUE;
}
Пример #8
0
BOOL LoadSettingsFromFile(LPCSTR szKey,LPCSTR szFile,BYTE bFileIsReg)
{
	HKEY hKey;
	LONG lRet=RegOpenKeyEx(HKEY_CURRENT_USER,szKey,
		0,KEY_READ,&hKey);
	
	// Check wheter key exists
	if (lRet!=ERROR_FILE_NOT_FOUND)
	{
		// Key exists, using it
		RegCloseKey(hKey);
		return TRUE;
	}

	if (bFileIsReg)
	{
				
		// Restore key
		char szCommand[2000];
		sprintf_s(szCommand,2000,"regedit /s \"%s\"",szFile);

		PROCESS_INFORMATION pi;
		STARTUPINFO si; // Ansi and Unicode versions are same
		ZeroMemory(&si,sizeof(STARTUPINFO));
		si.cb=sizeof(STARTUPINFO);
		
		if (CreateProcess(NULL,szCommand,NULL,
			NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS,
			NULL,NULL,&si,&pi))
		{
			WaitForSingleObject(pi.hProcess,2000);
			CloseHandle(pi.hThread);
			CloseHandle(pi.hProcess);	
		}
		else
			return FALSE;

		return TRUE;
	}

	// Acquiring required privileges	
	HANDLE hToken;
	if (OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
	{
		PTOKEN_PRIVILEGES ns=(PTOKEN_PRIVILEGES)new BYTE[sizeof(DWORD)+sizeof(LUID_AND_ATTRIBUTES)+2];
		if (LookupPrivilegeValue(NULL,SE_BACKUP_NAME,&(ns->Privileges[0].Luid)))
		{
			ns->PrivilegeCount=1;
			ns->Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
			if (!AdjustTokenPrivileges(hToken,FALSE,ns,0,NULL,NULL))
			{
				//ShowError(NULL,IDS_ERRORCANNOTENABLEPRIVILEGE,GetLastError());
			}
		}
		if (LookupPrivilegeValue(NULL,SE_RESTORE_NAME,&(ns->Privileges[0].Luid)))
		{
			ns->PrivilegeCount=1;
			ns->Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
			if (!AdjustTokenPrivileges(hToken,FALSE,ns,0,NULL,NULL))
			{
				//ShowError(NULL,IDS_ERRORCANNOTENABLEPRIVILEGE,GetLastError());
			}

		}
		delete[] (BYTE*)ns;
		CloseHandle(hToken);
	}

	// First, check that we can restore key
	
	lRet=RegCreateKeyEx(HKEY_CURRENT_USER,szKey,
		0,NULL,REG_OPTION_BACKUP_RESTORE,KEY_ALL_ACCESS,NULL,&hKey,NULL);
	if (lRet!=ERROR_SUCCESS)
	{
		//ShowError(hWnd,IDS_ERRORCANNOTCREATEKEY,lRet);
		return FALSE;
	}
	lRet=RegRestoreKey(hKey,szFile,0);
	RegCloseKey(hKey);		
	
	return lRet==ERROR_SUCCESS;
}