global.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "stdafx.h"
  2. #include "global.h"
  3. #include "AppXmlInfo.h"
  4. #include "InstallXmlInfo.h"
  5. #include <tchar.h>
  6. #include "atlbase.h"
  7. #include "shobjidl.h"
  8. #include <locale>
  9. void DeleteDirectory(CString dir,bool delParenDir)
  10. {
  11. if(dir.IsEmpty())
  12. return ;
  13. CFileFind find;
  14. BOOL bFound = find.FindFile(dir + _T("\\*"),0);
  15. while(bFound)
  16. {
  17. bFound = find.FindNextFileW();
  18. if(find.IsDots())
  19. {
  20. continue;
  21. }
  22. SetFileAttributes(find.GetFilePath(),FILE_ATTRIBUTE_NORMAL);
  23. if(find.IsDirectory())
  24. {
  25. DeleteDirectory(find.GetFilePath(),true);
  26. }
  27. else
  28. {
  29. ;DeleteFile(find.GetFilePath());
  30. }
  31. }
  32. find.Close();
  33. if(delParenDir)
  34. RemoveDirectory(dir);
  35. return ;
  36. }
  37. void RemoveDirectoryRecursive(CString dir)
  38. {
  39. if(dir.IsEmpty())
  40. return ;
  41. CFileFind find;
  42. BOOL bFound = find.FindFile(dir + _T("\\*.*"),0);
  43. while(bFound)
  44. {
  45. bFound = find.FindNextFileW();
  46. if(find.IsDots())
  47. {
  48. continue;
  49. }
  50. if(find.IsDirectory())
  51. {
  52. SetFileAttributes(find.GetFilePath(),FILE_ATTRIBUTE_NORMAL);
  53. RemoveDirectoryRecursive(find.GetFilePath());
  54. }
  55. }
  56. find.Close();
  57. RemoveDirectory(dir);
  58. }
  59. CString GetSystemFolderByCSIDL(int csidl)
  60. {
  61. LPITEMIDLIST ppidl = NULL;
  62. TCHAR buffer[MAX_PATH];
  63. CString path;
  64. if (SHGetSpecialFolderLocation(NULL, csidl, &ppidl) == S_OK)
  65. {
  66. BOOL flag = SHGetPathFromIDList(ppidl, buffer);
  67. CoTaskMemFree(ppidl);
  68. path = buffer;
  69. }
  70. return path;
  71. }
  72. bool FileExist(CString strFileName)
  73. {
  74. CFileFind fFind;
  75. return (fFind.FindFile(strFileName)!=0)?true:false;
  76. }
  77. CString GetSysPath(CString sEnvironmentName)
  78. {
  79. TCHAR buffer[1024];
  80. DWORD dwRet = GetEnvironmentVariable(sEnvironmentName, buffer, sizeof(buffer));
  81. CString value;
  82. if (dwRet > 0)
  83. {
  84. value = buffer;
  85. }
  86. return value;
  87. }
  88. bool FolderExist(CString strPath)
  89. {
  90. WIN32_FIND_DATA wfd;
  91. bool rValue = false;
  92. HANDLE hFind = FindFirstFile(strPath, &wfd);
  93. if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  94. {
  95. rValue = true;
  96. }
  97. FindClose(hFind);
  98. return rValue;
  99. }
  100. UINT UninstallThreadFun(LPVOID lpParam)
  101. {
  102. ThreadParamInfo *info = (ThreadParamInfo*)lpParam;
  103. Sleep(1000);
  104. TCHAR exeFullPath[MAX_PATH]; // MAX_PATH
  105. DWORD ret = GetModuleFileName(NULL,exeFullPath,MAX_PATH);
  106. if(ret ==0 )
  107. {
  108. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_DIR_ERROR,0);
  109. return 0;
  110. }
  111. //卸载程序所在目录
  112. CString appFullPath = exeFullPath;
  113. //卸载程序主文件名
  114. CString appMainName = AfxGetApp()->m_pszAppName;
  115. if(appFullPath.IsEmpty())
  116. {
  117. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_DIR_ERROR,0);
  118. return 0;
  119. }
  120. CString appDir = appFullPath.Mid(0,appFullPath.ReverseFind(_T('\\')));
  121. CInstallXmlInfo installInfo(appDir + _T("\\install.xml"));
  122. if(installInfo.appPath.CompareNoCase(appDir)!=0||installInfo.programGroup.IsEmpty())
  123. {
  124. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_NOCONFIG_ERROR,0);
  125. return 0;
  126. }
  127. //删除快捷方式
  128. CString desktopPath = GetSystemFolderByCSIDL(CSIDL_DESKTOP);
  129. CString programPath = GetSystemFolderByCSIDL(CSIDL_PROGRAMS);
  130. if(desktopPath.GetLength()==0||programPath.GetLength()==0)
  131. {
  132. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_SHORTCUT_ERROR,0);
  133. return 0;
  134. }
  135. programPath = programPath + _T("\\") + installInfo.programGroup;
  136. for(int i=0;i<installInfo.shortcuts.GetSize();i++)
  137. {
  138. CShortCutInfo shortcutinfo = installInfo.shortcuts.GetAt(i);
  139. CString shortcutPath;
  140. if(shortcutinfo.pos.Compare(_T("desktop"))==0)
  141. {
  142. shortcutPath = desktopPath;
  143. }
  144. else if(shortcutinfo.pos.Compare(_T("program"))==0)
  145. {
  146. shortcutPath = programPath;
  147. }
  148. else
  149. {
  150. shortcutPath = installInfo.appPath;
  151. }
  152. CFileFind fFind;
  153. if(fFind.FindFile(shortcutPath + _T("\\") + shortcutinfo.display)!=0)
  154. {
  155. SetFileAttributes(shortcutPath + _T("\\") + shortcutinfo.display,FILE_ATTRIBUTE_NORMAL);
  156. if(DeleteFile(shortcutPath + _T("\\") + shortcutinfo.display)==0)
  157. {
  158. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_SHORTCUT_ERROR,0);
  159. return 0;
  160. }
  161. }
  162. }
  163. if(RemoveDirectory(programPath)==0)
  164. {
  165. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_SHORTCUT_ERROR,0);
  166. return 0;
  167. }
  168. //删除注册表
  169. CRegKey key;
  170. CString item = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\") ;
  171. if(key.Open(HKEY_LOCAL_MACHINE,item) == ERROR_SUCCESS)
  172. {
  173. LONG l = key.DeleteSubKey(installInfo.regInfo.regItem);
  174. CString str;
  175. str.Format(_T("%d"),l);
  176. if( l!= ERROR_SUCCESS)
  177. {
  178. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_REG_ERROR,0);
  179. return 0;
  180. }
  181. key.Close();
  182. }
  183. //删除安装文件
  184. for(int i=0;i<installInfo.locations.GetSize();i++)
  185. {
  186. CString fileName = installInfo.locations.GetAt(i).fileName;
  187. CString fileType = installInfo.locations.GetAt(i).fileType;
  188. CString subDir = installInfo.locations.GetAt(i).subDir;
  189. CString app = _T("app");
  190. CString data = _T("data");
  191. CString realDir;
  192. if(fileType.CompareNoCase(app)==0)
  193. {
  194. realDir = installInfo.appPath ;
  195. }
  196. else if(fileType.CompareNoCase(data)==0)
  197. {
  198. realDir = installInfo.dataPath ;
  199. if(!info->deluserdata)
  200. continue;
  201. }
  202. else
  203. {
  204. realDir = installInfo.appPath;
  205. }
  206. subDir.Replace(_T('/'),_T('\\'));
  207. realDir += subDir;
  208. if(FileExist(realDir + _T("\\") + fileName))
  209. {
  210. SetFileAttributes(realDir + _T("\\") + fileName,FILE_ATTRIBUTE_NORMAL);
  211. DeleteFile(realDir + _T("\\") + fileName);
  212. }
  213. }
  214. //删除cache及log目录
  215. DeleteDirectory(installInfo.cachePath,false);
  216. DeleteDirectory(installInfo.logPath,false);
  217. //删除配置文件
  218. SetFileAttributes(appDir + _T("\\install.xml"),FILE_ATTRIBUTE_NORMAL);
  219. SetFileAttributes(appDir + _T("\\app.xml"),FILE_ATTRIBUTE_NORMAL);
  220. DeleteFile(appDir + _T("\\install.xml"));
  221. DeleteFile(appDir + _T("\\app.xml"));
  222. //删除目录
  223. //AfxMessageBox(installInfo.corpPath);
  224. RemoveDirectoryRecursive(installInfo.corpPath);
  225. //删除自身
  226. CString tmpDir = GetSysPath(_T("temp"));
  227. CString batName = tmpDir+ _T("\\") + appMainName + _T("_delself.bat");
  228. CStdioFile file;
  229. if(file.Open(batName, CFile::modeCreate | CFile::modeWrite))
  230. {
  231. char * old_locale = _strdup(setlocale(LC_CTYPE,NULL));
  232. setlocale(LC_CTYPE,"chs");
  233. CString script;
  234. script = _T(":repeat \r\n");
  235. file.WriteString(script);
  236. script = _T( "del \"")+ appFullPath + _T("\"\r\n ") ;
  237. file.WriteString(script);
  238. script = _T("if exist \"") + appFullPath + _T("\" goto Repeat\r\n");
  239. file.WriteString(script);
  240. script = _T("rd \"") +installInfo.appPath + _T("\"\r\n");
  241. file.WriteString(script);
  242. script = _T("rd \"") +installInfo.productPath + _T("\"\r\n");
  243. file.WriteString(script);
  244. script = _T("rd \"") +installInfo.corpPath + _T("\"\r\n");
  245. file.WriteString(script);
  246. script = _T("del \"") + batName + _T("\"\r\n");
  247. file.WriteString(script);
  248. //script = _T("PAUSE\r\n");;
  249. //file.WriteString(script);
  250. setlocale(LC_CTYPE,old_locale);
  251. free(old_locale);
  252. file.Close();
  253. }
  254. ShellExecute(NULL,_T("open"),batName,NULL,tmpDir,SW_HIDE);
  255. ::PostMessage(info->hwnd,WM_USERMSG_UINSTALL_STATE,USER_PARAM_UNINSTALL_EXIT,0);
  256. return 1;
  257. }