设置扩展名系统关联
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
/* * Assoc.C - assoc internal command. * * * History: * * 14-Mar-2009 Lee C. Baker * - initial implementation * * 15-Mar-2009 Lee C. Baker * - Don't write to (or use) HKEY_CLASSES_ROOT directly * - Externalize strings * * TODO: * - PrintAllAssociations might could be optimized to not fetch all registry subkeys under 'Classes', just the ones that start with '.' * - Make sure that non-administrator users can list associations, and get appropriate error messages when they don't have sufficient * priveleges to perform an operation */ #ifdef INCLUDE_CMD_ASSOC static INT PrintAssociation(LPTSTR extension) { DWORD return_val; HKEY hKey = NULL, hInsideKey = NULL; DWORD fileTypeLength = 0; LPTSTR fileType = NULL; return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey); if(return_val != ERROR_SUCCESS) { RegCloseKey(hKey); return -1; } return_val = RegOpenKeyEx(hKey, extension, 0, KEY_READ, &hInsideKey); if(return_val != ERROR_SUCCESS) { RegCloseKey(hKey); RegCloseKey(hInsideKey); return 0; } /* obtain string length */ return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, NULL, &fileTypeLength); if(return_val == ERROR_FILE_NOT_FOUND) /* no default value, don't display */ { RegCloseKey(hInsideKey); RegCloseKey(hKey); return 0; } if(return_val != ERROR_SUCCESS) { RegCloseKey(hInsideKey); RegCloseKey(hKey); return -2; } fileType = cmd_alloc(fileTypeLength * sizeof(TCHAR)); /* obtain actual file type */ return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, (LPBYTE) fileType, &fileTypeLength); RegCloseKey(hInsideKey); RegCloseKey(hKey); if(return_val != ERROR_SUCCESS) { cmd_free(fileType); return -2; } if(fileTypeLength != 0) /* if there is a default key, display relevant information */ { ConOutPrintf(_T("%s=%s\r\n"), extension, fileType); } if(fileTypeLength) cmd_free(fileType); return 1; } static INT PrintAllAssociations() { DWORD return_val = 0; HKEY hKey = NULL; DWORD numKeys = 0; DWORD extLength = 0; LPTSTR extName = NULL; DWORD keyCtr = 0; return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey); if(return_val != ERROR_SUCCESS) { RegCloseKey(hKey); return -1; } return_val = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numKeys, &extLength, NULL, NULL, NULL, NULL, NULL, NULL); if(return_val != ERROR_SUCCESS) { RegCloseKey(hKey); return -2; } extName = cmd_alloc(extLength * sizeof(TCHAR)); for(keyCtr = 0; keyCtr < numKeys; keyCtr++) { DWORD buffer_size = extLength; return_val = RegEnumKeyEx(hKey, keyCtr, extName, &buffer_size, NULL, NULL, NULL, NULL); if(return_val == ERROR_SUCCESS || return_val == ERROR_MORE_DATA) { if(*extName == _T('.')) PrintAssociation(extName); } else { cmd_free(extName); RegCloseKey(hKey); return -1; } } RegCloseKey(hKey); if(extName) cmd_free(extName); return numKeys; } static INT AddAssociation(LPTSTR extension, LPTSTR type) { DWORD return_val; HKEY hKey = NULL, insideKey = NULL; return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_ALL_ACCESS, &hKey); if(return_val != ERROR_SUCCESS) return -1; return_val = RegCreateKeyEx(hKey, extension, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &insideKey, NULL); if(return_val != ERROR_SUCCESS) { RegCloseKey(hKey); return -1; } return_val = RegSetValueEx(insideKey, NULL, 0, REG_SZ, (LPBYTE)type, (_tcslen(type) + 1) * sizeof(TCHAR)); if(return_val != ERROR_SUCCESS) { RegCloseKey(insideKey); RegCloseKey(hKey); return -2; } RegCloseKey(insideKey); RegCloseKey(hKey); return 0; } static int RemoveAssociation(LPTSTR extension) { DWORD return_val; HKEY hKey; return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_ALL_ACCESS, &hKey); if(return_val != ERROR_SUCCESS) return -1; return_val = RegDeleteKey(hKey, extension); if(return_val != ERROR_SUCCESS) { RegCloseKey(hKey); return -2; } RegCloseKey(hKey); return 0; } INT CommandAssoc (LPTSTR param) { /* print help */ if (!_tcsncmp (param, _T("/?"), 2)) { ConOutResPaging(TRUE,STRING_ASSOC_HELP); return 0; } nErrorLevel = 0; if(_tcslen(param) == 0) PrintAllAssociations(); else { LPTSTR lpEqualSign = _tcschr(param, _T('=')); if(lpEqualSign != NULL) { LPTSTR fileType = lpEqualSign + 1; LPTSTR extension = cmd_alloc((lpEqualSign - param + 1) * sizeof(TCHAR)); _tcsncpy(extension, param, lpEqualSign - param); extension[lpEqualSign - param] = (TCHAR)0; if(_tcslen(fileType) == 0) /* if the equal sign is the last character in the string, then delete the key */ { RemoveAssociation(extension); } else /* otherwise, add the key and print out the association*/ { AddAssociation( extension, fileType); PrintAssociation(extension); } cmd_free(extension); } else { /* no equal sign, print all associations */ INT retval = PrintAssociation(param); if(retval == 0) /* if nothing printed out */ ConOutResPrintf(STRING_ASSOC_ERROR, param); } } return 0; } #endif /* INCLUDE_CMD_ASSOC */ |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=504