| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #include "stdafx.h"
- #include "global.h"
- #include "AppXmlInfo.h"
- #include "InstallXmlInfo.h"
- #include <tchar.h>
- #include "atlbase.h"
- #include "shobjidl.h"
- #include <locale>
- void DeleteDirectory(CString dir,bool delParenDir)
- {
- if(dir.IsEmpty())
- return ;
- CFileFind find;
- BOOL bFound = find.FindFile(dir + _T("\\*"),0);
- while(bFound)
- {
- bFound = find.FindNextFileW();
- if(find.IsDots())
- {
- continue;
- }
- SetFileAttributes(find.GetFilePath(),FILE_ATTRIBUTE_NORMAL);
- if(find.IsDirectory())
- {
- DeleteDirectory(find.GetFilePath(),true);
- }
- else
- {
- ;DeleteFile(find.GetFilePath());
- }
- }
- find.Close();
- if(delParenDir)
- RemoveDirectory(dir);
- return ;
- }
- void RemoveDirectoryRecursive(CString dir)
- {
- if(dir.IsEmpty())
- return ;
- CFileFind find;
- BOOL bFound = find.FindFile(dir + _T("\\*.*"),0);
- while(bFound)
- {
- bFound = find.FindNextFileW();
- if(find.IsDots())
- {
- continue;
- }
- if(find.IsDirectory())
- {
- SetFileAttributes(find.GetFilePath(),FILE_ATTRIBUTE_NORMAL);
- RemoveDirectoryRecursive(find.GetFilePath());
- }
-
- }
- find.Close();
- RemoveDirectory(dir);
- }
- CString GetSystemFolderByCSIDL(int csidl)
- {
- LPITEMIDLIST ppidl = NULL;
- TCHAR buffer[MAX_PATH];
- CString path;
- if (SHGetSpecialFolderLocation(NULL, csidl, &ppidl) == S_OK)
- {
- BOOL flag = SHGetPathFromIDList(ppidl, buffer);
- CoTaskMemFree(ppidl);
- path = buffer;
- }
- return path;
- }
- bool FileExist(CString strFileName)
- {
- CFileFind fFind;
- return (fFind.FindFile(strFileName)!=0)?true:false;
- }
- CString GetSysPath(CString sEnvironmentName)
- {
-
- TCHAR buffer[1024];
- DWORD dwRet = GetEnvironmentVariable(sEnvironmentName, buffer, sizeof(buffer));
- CString value;
- if (dwRet > 0)
- {
- value = buffer;
- }
- return value;
- }
- bool FolderExist(CString strPath)
- {
- WIN32_FIND_DATA wfd;
- bool rValue = false;
- HANDLE hFind = FindFirstFile(strPath, &wfd);
-
- if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
- {
- rValue = true;
- }
- FindClose(hFind);
- return rValue;
- }
- UINT UninstallThreadFun(LPVOID lpParam)
- {
- ThreadParamInfo *info = (ThreadParamInfo*)lpParam;
- Sleep(1000);
- TCHAR exeFullPath[MAX_PATH]; // MAX_PATH
- DWORD ret = GetModuleFileName(NULL,exeFullPath,MAX_PATH);
- if(ret ==0 )
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_DIR_ERROR,0);
- return 0;
- }
- //卸载程序所在目录
- CString appFullPath = exeFullPath;
- //卸载程序主文件名
- CString appMainName = AfxGetApp()->m_pszAppName;
- if(appFullPath.IsEmpty())
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_DIR_ERROR,0);
- return 0;
- }
- CString appDir = appFullPath.Mid(0,appFullPath.ReverseFind(_T('\\')));
- CInstallXmlInfo installInfo(appDir + _T("\\install.xml"));
- if(installInfo.appPath.CompareNoCase(appDir)!=0||installInfo.programGroup.IsEmpty())
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_NOCONFIG_ERROR,0);
- return 0;
- }
- //删除快捷方式
- CString desktopPath = GetSystemFolderByCSIDL(CSIDL_DESKTOP);
- CString programPath = GetSystemFolderByCSIDL(CSIDL_PROGRAMS);
- if(desktopPath.GetLength()==0||programPath.GetLength()==0)
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_SHORTCUT_ERROR,0);
- return 0;
- }
-
- programPath = programPath + _T("\\") + installInfo.programGroup;
- for(int i=0;i<installInfo.shortcuts.GetSize();i++)
- {
- CShortCutInfo shortcutinfo = installInfo.shortcuts.GetAt(i);
- CString shortcutPath;
- if(shortcutinfo.pos.Compare(_T("desktop"))==0)
- {
- shortcutPath = desktopPath;
- }
- else if(shortcutinfo.pos.Compare(_T("program"))==0)
- {
- shortcutPath = programPath;
- }
- else
- {
- shortcutPath = installInfo.appPath;
- }
- CFileFind fFind;
- if(fFind.FindFile(shortcutPath + _T("\\") + shortcutinfo.display)!=0)
- {
- SetFileAttributes(shortcutPath + _T("\\") + shortcutinfo.display,FILE_ATTRIBUTE_NORMAL);
- if(DeleteFile(shortcutPath + _T("\\") + shortcutinfo.display)==0)
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_SHORTCUT_ERROR,0);
- return 0;
- }
- }
- }
- if(RemoveDirectory(programPath)==0)
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_SHORTCUT_ERROR,0);
- return 0;
- }
- //删除注册表
- CRegKey key;
- CString item = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\") ;
- if(key.Open(HKEY_LOCAL_MACHINE,item) == ERROR_SUCCESS)
- {
- LONG l = key.DeleteSubKey(installInfo.regInfo.regItem);
- CString str;
- str.Format(_T("%d"),l);
- if( l!= ERROR_SUCCESS)
- {
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_REG_ERROR,0);
- return 0;
- }
- key.Close();
- }
-
- //删除安装文件
- for(int i=0;i<installInfo.locations.GetSize();i++)
- {
- CString fileName = installInfo.locations.GetAt(i).fileName;
- CString fileType = installInfo.locations.GetAt(i).fileType;
- CString subDir = installInfo.locations.GetAt(i).subDir;
- CString app = _T("app");
- CString data = _T("data");
- CString realDir;
- if(fileType.CompareNoCase(app)==0)
- {
- realDir = installInfo.appPath ;
- }
- else if(fileType.CompareNoCase(data)==0)
- {
- realDir = installInfo.dataPath ;
- if(!info->deluserdata)
- continue;
- }
- else
- {
- realDir = installInfo.appPath;
- }
- subDir.Replace(_T('/'),_T('\\'));
- realDir += subDir;
- if(FileExist(realDir + _T("\\") + fileName))
- {
- SetFileAttributes(realDir + _T("\\") + fileName,FILE_ATTRIBUTE_NORMAL);
- DeleteFile(realDir + _T("\\") + fileName);
- }
- }
- //删除cache及log目录
- DeleteDirectory(installInfo.cachePath,false);
- DeleteDirectory(installInfo.logPath,false);
- //删除配置文件
- SetFileAttributes(appDir + _T("\\install.xml"),FILE_ATTRIBUTE_NORMAL);
- SetFileAttributes(appDir + _T("\\app.xml"),FILE_ATTRIBUTE_NORMAL);
- DeleteFile(appDir + _T("\\install.xml"));
- DeleteFile(appDir + _T("\\app.xml"));
- //删除目录
- //AfxMessageBox(installInfo.corpPath);
- RemoveDirectoryRecursive(installInfo.corpPath);
- //删除自身
- CString tmpDir = GetSysPath(_T("temp"));
- CString batName = tmpDir+ _T("\\") + appMainName + _T("_delself.bat");
- CStdioFile file;
- if(file.Open(batName, CFile::modeCreate | CFile::modeWrite))
- {
- char * old_locale = _strdup(setlocale(LC_CTYPE,NULL));
- setlocale(LC_CTYPE,"chs");
- CString script;
- script = _T(":repeat \r\n");
- file.WriteString(script);
- script = _T( "del \"")+ appFullPath + _T("\"\r\n ") ;
- file.WriteString(script);
- script = _T("if exist \"") + appFullPath + _T("\" goto Repeat\r\n");
- file.WriteString(script);
- script = _T("rd \"") +installInfo.appPath + _T("\"\r\n");
- file.WriteString(script);
- script = _T("rd \"") +installInfo.productPath + _T("\"\r\n");
- file.WriteString(script);
- script = _T("rd \"") +installInfo.corpPath + _T("\"\r\n");
- file.WriteString(script);
- script = _T("del \"") + batName + _T("\"\r\n");
- file.WriteString(script);
- //script = _T("PAUSE\r\n");;
- //file.WriteString(script);
- setlocale(LC_CTYPE,old_locale);
- free(old_locale);
- file.Close();
- }
-
- ShellExecute(NULL,_T("open"),batName,NULL,tmpDir,SW_HIDE);
- ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_EXIT,0);
- return 1;
- }
|