#include "stdafx.h" #include "global.h" #include "AppXmlInfo.h" #include "InstallXmlInfo.h" #include #include "atlbase.h" #include "shobjidl.h" #include 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;ihwnd,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;ideluserdata) 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; }