📄 dwuninst.cpp
字号:
/* Copyright (C) 1999-2000, Ghostgum Software Pty Ltd. All rights reserved.
This file is part of GSview.
This program is distributed with NO WARRANTY OF ANY KIND. No author
or distributor accepts any responsibility for the consequences of using it,
or for whether it serves any particular purpose or works at all, unless he
or she says so in writing. Refer to the GSview Free Public Licence
(the "Licence") for full details.
Every copy of GSview must include a copy of the Licence, normally in a
plain ASCII text file named LICENCE. The Licence grants you the right
to copy, modify and redistribute GSview, but only under certain conditions
described in the Licence. Among other things, the Licence requires that
the copyright notice and this notice be preserved on all copies.
*/
// $Id: dwuninst.cpp $
#define STRICT
#include <windows.h>
#include <objbase.h>
#include <shlobj.h>
#include <shellapi.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <direct.h>
#include "dwuninst.h"
#ifdef _MSC_VER
#define _export
#define chdir(x) _chdir(x)
#define mkdir(x) _mkdir(x)
#endif
#define DELAY_STEP 500
#define DELAY_FILE 5
#define MAXSTR 256
#define UNINSTALLKEY TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
HWND hDlgModeless;
HWND hText1;
HWND hText2;
char path[MAXSTR];
int language = 0;
BOOL is_win4 = FALSE;
HINSTANCE phInstance;
char szSection[] = "////////////////////////////////";
BOOL bQuit = FALSE;
BOOL gError = FALSE; // set TRUE if an uninstall was not successful
BOOL bSilent = FALSE;
char szTitle[MAXSTR];
char szLogFile[MAXSTR];
char szLine[MAXSTR];
FILE *fLog;
void do_message(void);
BOOL dofiles(void);
BOOL registry_delete(void);
BOOL registry_import(void);
BOOL shell_new(void);
BOOL shell_old(void);
BOOL doEOF(void);
// #define gs_addmess(str) fputs(str, stdout) // for debug
#define gs_addmess(str)
int message_box(LPCTSTR message, LPCTSTR title, int nType)
{
if (bSilent)
return 0;
return MessageBox(HWND_DESKTOP, message, title, nType);
}
// linked list for deleting registry entries in reverse order
typedef struct tagKEY {
long index;
struct tagKEY *previous;
} KEY;
KEY *last_key = NULL;
// read a line from the log, removing trailing new line character
BOOL GetLine(void)
{
BOOL err = TRUE;
int i;
szLine[0] = '\0';
if (fLog)
err = (fgets(szLine, sizeof(szLine)-1, fLog) == NULL);
i = strlen(szLine) - 1;
if ( (szLine[0] != '\0') && (szLine[i] == '\n'))
szLine[i] = '\0';
return !err;
}
BOOL IsSection(void)
{
return (strncmp(szLine, szSection, strlen(szSection)) == 0);
}
BOOL
NextSection(void)
{
while (GetLine()) {
do_message();
if (bQuit)
return FALSE;
if (IsSection())
return TRUE;
}
return TRUE;
}
BOOL ReadSection(void)
{
do_message();
if (bQuit)
return FALSE;
GetLine();
if (strlen(szLine) == 0) {
doEOF();
return TRUE;
}
else if (strcmp(szLine, "FileNew")==0) {
SetWindowText(hText1, "Removing Files");
if (!bSilent)
Sleep(DELAY_STEP);
if (!dofiles())
return FALSE;
SetWindowText(hText1, "");
return TRUE;
}
else if (strcmp(szLine, "RegistryNew")==0) {
SetWindowText(hText1, "Removing Registry entries");
if (!bSilent)
Sleep(DELAY_STEP);
if (!registry_delete())
return FALSE;
SetWindowText(hText1, "");
return TRUE;
}
else if (strcmp(szLine, "RegistryOld")==0) {
SetWindowText(hText1, "Restoring Registry entries");
if (!bSilent)
Sleep(DELAY_STEP);
if (!registry_import())
return FALSE;
SetWindowText(hText1, "");
return TRUE;
}
else if (strcmp(szLine, "ShellNew")==0) {
SetWindowText(hText1, "Removing Start Menu items");
if (!bSilent)
Sleep(DELAY_STEP);
if (!shell_new())
return FALSE;
SetWindowText(hText1, "");
return TRUE;
}
else if (strcmp(szLine, "ShellOld")==0) {
SetWindowText(hText1, "Restoring Start Menu items");
if (!bSilent)
Sleep(DELAY_STEP);
if (!shell_old())
return FALSE;
SetWindowText(hText1, "");
return TRUE;
}
return FALSE;
}
BOOL
dofiles(void)
{
while (GetLine()) {
do_message();
if (bQuit)
return FALSE;
if (IsSection()) {
SetWindowText(hText2, "");
return TRUE;
}
if (szLine[0] != '\0') {
SetWindowText(hText2, szLine);
if (!bSilent)
Sleep(DELAY_FILE);
gs_addmess("Deleting File: ");
gs_addmess(szLine);
gs_addmess("\n");
DeleteFile(szLine);
}
}
return FALSE;
}
BOOL
doEOF(void)
{
fclose(fLog);
fLog = NULL;
unlink(szLogFile);
PostMessage(hDlgModeless, WM_COMMAND, IDC_DONE, 0L);
bQuit = TRUE;
return TRUE;
}
BOOL
registry_delete_key(void)
{
char keyname[MAXSTR];
HKEY hkey = HKEY_CLASSES_ROOT;
HKEY hrkey = HKEY_CLASSES_ROOT;
char *rkey, *skey;
char *name;
DWORD dwResult;
keyname[0] = '\0';
while (GetLine()) {
if ((szLine[0] == '\0') || (szLine[0] == '\r') || (szLine[0] == '\n'))
break;
if (szLine[0] == '[') {
// key name
rkey = strtok(szLine+1, "\\]\n\r");
if (rkey == (char *)NULL)
return FALSE;
skey = strtok(NULL, "]\n\r");
if (strcmp(rkey, "HKEY_CLASSES_ROOT")==0)
hrkey = HKEY_CLASSES_ROOT;
else if (strcmp(rkey, "HKEY_CURRENT_USER")==0)
hrkey = HKEY_CURRENT_USER;
else if (strcmp(rkey, "HKEY_LOCAL_MACHINE")==0)
hrkey = HKEY_LOCAL_MACHINE;
else if (strcmp(rkey, "HKEY_USERS")==0)
hrkey = HKEY_USERS;
else
return FALSE;
if (skey == (char *)NULL)
return FALSE;
gs_addmess("Opening registry key\n ");
gs_addmess(rkey);
gs_addmess("\\");
gs_addmess(skey);
gs_addmess("\n");
if (RegCreateKeyEx(hrkey, skey, 0, "", 0, KEY_ALL_ACCESS,
NULL, &hkey, &dwResult)
!= ERROR_SUCCESS)
return FALSE;
strcpy(keyname, skey);
}
else if (szLine[0] == '@') {
// default value
RegDeleteValue(hkey, NULL);
gs_addmess("Deleting registry default value\n");
}
else if (szLine[0] == '\042') {
// named value
name = strtok(szLine+1, "\042\r\n");
RegDeleteValue(hkey, name);
gs_addmess("Deleting registry named value\n ");
gs_addmess(name);
gs_addmess("\n");
}
}
// Find out if key has subkeys or values
TCHAR szClass[MAXSTR];
DWORD cchClass;
DWORD cSubKeys;
DWORD cchMaxSubKey;
DWORD cchMaxClass;
DWORD cValues;
DWORD cchMaxValueName;
DWORD cbMaxValueData;
DWORD cbSecurityDescriptor;
FILETIME ftLastWriteTime;
cchClass = sizeof(szClass) / sizeof(TCHAR);
cSubKeys = 0;
cValues = 0;
RegQueryInfoKey(hkey, szClass, &cchClass, NULL,
&cSubKeys, &cchMaxSubKey, &cchMaxClass,
&cValues, &cchMaxValueName, &cbMaxValueData,
&cbSecurityDescriptor, &ftLastWriteTime);
// close key
if (hkey != HKEY_CLASSES_ROOT)
RegCloseKey(hkey);
// delete the key
if ((cSubKeys != 0) || (cValues != 0)) {
gs_addmess("Not deleting non empty registry key\n ");
gs_addmess(keyname);
gs_addmess("\n");
}
else if (strlen(keyname)) {
gs_addmess("Deleting registry key\n ");
gs_addmess(keyname);
gs_addmess("\n");
RegOpenKeyEx(hrkey, NULL, 0, 0, &hkey);
RegDeleteKey(hkey, keyname);
RegCloseKey(hkey);
}
return TRUE;
}
BOOL
registry_delete()
{
long logindex;
KEY *key;
// scan log file
// so we can remove keys in reverse order
logindex = 0;
while (GetLine() && !IsSection()) {
KEY *key;
if (szLine[0] == '[') {
if ((key = (KEY *)malloc(sizeof(KEY)))
!= (KEY *)NULL) {
key->previous = last_key;
key->index = logindex;
last_key = key;
}
}
logindex = ftell(fLog);
}
// Remove keys
for (key = last_key; key != NULL; key = key->previous) {
if (key != last_key)
free(last_key);
fseek(fLog, key->index, SEEK_SET);
registry_delete_key();
last_key = key;
}
free(last_key);
fseek(fLog, logindex, SEEK_SET);
GetLine();
return TRUE;
}
void
registry_unquote(char *line)
{
char *s, *d;
int value;
s = d = line;
while (*s) {
if (*s != '\\') {
*d++ = *s;
}
else {
s++;
if (*s == '\\')
*d++ = *s;
else {
value = 0;
if (*s) {
value = *s++ - '0';
}
if (*s) {
value <<= 3;
value += *s++ - '0';
}
if (*s) {
value <<= 3;
value += *s - '0';
}
*d++ = (char)value;
}
}
s++;
}
*d = '\0';
}
BOOL
registry_import()
{
HKEY hkey = HKEY_CLASSES_ROOT;
HKEY hrkey;
char *rkey, *skey;
char *value;
char *name;
DWORD dwResult;
GetLine();
if (strncmp(szLine, "REGEDIT4", 8) != 0)
return FALSE;
while (GetLine()) {
if (IsSection())
break;
if ((szLine[0] == '\0') || (szLine[0] == '\r') || (szLine[0] == '\n'))
continue;
if (szLine[0] == '[') {
// key name
if (hkey != HKEY_CLASSES_ROOT) {
RegCloseKey(hkey);
hkey = HKEY_CLASSES_ROOT;
}
rkey = strtok(szLine+1, "\\]\n\r");
if (rkey == (char *)NULL)
return FALSE;
skey = strtok(NULL, "]\n\r");
if (strcmp(rkey, "HKEY_CLASSES_ROOT")==0)
hrkey = HKEY_CLASSES_ROOT;
else if (strcmp(rkey, "HKEY_CURRENT_USER")==0)
hrkey = HKEY_CURRENT_USER;
else if (strcmp(rkey, "HKEY_LOCAL_MACHINE")==0)
hrkey = HKEY_LOCAL_MACHINE;
else if (strcmp(rkey, "HKEY_USERS")==0)
hrkey = HKEY_USERS;
else
return FALSE;
if (skey == (char *)NULL)
return FALSE;
gs_addmess("Creating registry key\n ");
gs_addmess(rkey);
gs_addmess("\\");
gs_addmess("skey");
gs_addmess("\n");
if (RegCreateKeyEx(hrkey, skey, 0, "", 0, KEY_ALL_ACCESS,
NULL, &hkey, &dwResult)
!= ERROR_SUCCESS)
return FALSE;
}
else if (szLine[0] == '@') {
// default value
if (strlen(szLine) < 4)
return FALSE;
value = strtok(szLine+3, "\042\r\n");
if (value) {
registry_unquote(value);
gs_addmess("Setting registry key value\n ");
gs_addmess(value);
gs_addmess("\n");
if (RegSetValueEx(hkey, NULL, 0, REG_SZ,
(CONST BYTE *)value, strlen(value)+1)
!= ERROR_SUCCESS)
return FALSE;
}
}
else if (szLine[0] == '\042') {
// named value
name = strtok(szLine+1, "\042\r\n");
strtok(NULL, "\042\r\n");
value = strtok(NULL, "\042\r\n");
registry_unquote(value);
gs_addmess("Setting registry key value\n ");
gs_addmess(name);
gs_addmess("=");
gs_addmess(value);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -