Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenassoc.c
Go to the documentation of this file.
00001 /* 00002 * Assoc.C - assoc internal command. 00003 * 00004 * 00005 * History: 00006 * 00007 * 14-Mar-2009 Lee C. Baker 00008 * - initial implementation 00009 * 00010 * 15-Mar-2009 Lee C. Baker 00011 * - Don't write to (or use) HKEY_CLASSES_ROOT directly 00012 * - Externalize strings 00013 * 00014 * TODO: 00015 * - PrintAllAssociations might could be optimized to not fetch all registry subkeys under 'Classes', just the ones that start with '.' 00016 * - Make sure that non-administrator users can list associations, and get appropriate error messages when they don't have sufficient 00017 * priveleges to perform an operation 00018 */ 00019 00020 #include <precomp.h> 00021 00022 #ifdef INCLUDE_CMD_ASSOC 00023 00024 static INT 00025 PrintAssociation(LPTSTR extension) 00026 { 00027 DWORD return_val; 00028 HKEY hKey = NULL, hInsideKey = NULL; 00029 00030 DWORD fileTypeLength = 0; 00031 LPTSTR fileType = NULL; 00032 00033 return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey); 00034 00035 if(return_val != ERROR_SUCCESS) 00036 { 00037 RegCloseKey(hKey); 00038 return -1; 00039 } 00040 00041 return_val = RegOpenKeyEx(hKey, extension, 0, KEY_READ, &hInsideKey); 00042 00043 if(return_val != ERROR_SUCCESS) 00044 { 00045 RegCloseKey(hKey); 00046 RegCloseKey(hInsideKey); 00047 return 0; 00048 } 00049 00050 /* obtain string length */ 00051 return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, NULL, &fileTypeLength); 00052 00053 if(return_val == ERROR_FILE_NOT_FOUND) /* no default value, don't display */ 00054 { 00055 RegCloseKey(hInsideKey); 00056 RegCloseKey(hKey); 00057 return 0; 00058 } 00059 00060 if(return_val != ERROR_SUCCESS) 00061 { 00062 RegCloseKey(hInsideKey); 00063 RegCloseKey(hKey); 00064 return -2; 00065 } 00066 00067 fileType = cmd_alloc(fileTypeLength * sizeof(TCHAR)); 00068 00069 /* obtain actual file type */ 00070 return_val = RegQueryValueEx(hInsideKey, NULL, NULL, NULL, (LPBYTE) fileType, &fileTypeLength); 00071 00072 RegCloseKey(hInsideKey); 00073 RegCloseKey(hKey); 00074 00075 if(return_val != ERROR_SUCCESS) 00076 { 00077 cmd_free(fileType); 00078 return -2; 00079 } 00080 00081 if(fileTypeLength != 0) /* if there is a default key, display relevant information */ 00082 { 00083 ConOutPrintf(_T("%s=%s\r\n"), extension, fileType); 00084 } 00085 00086 if(fileTypeLength) 00087 cmd_free(fileType); 00088 00089 return 1; 00090 } 00091 00092 static INT 00093 PrintAllAssociations() 00094 { 00095 DWORD return_val = 0; 00096 HKEY hKey = NULL; 00097 DWORD numKeys = 0; 00098 00099 DWORD extLength = 0; 00100 LPTSTR extName = NULL; 00101 DWORD keyCtr = 0; 00102 00103 return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_READ, &hKey); 00104 00105 if(return_val != ERROR_SUCCESS) 00106 { 00107 RegCloseKey(hKey); 00108 return -1; 00109 } 00110 00111 return_val = RegQueryInfoKey(hKey, NULL, NULL, NULL, &numKeys, &extLength, NULL, NULL, NULL, NULL, NULL, NULL); 00112 00113 if(return_val != ERROR_SUCCESS) 00114 { 00115 RegCloseKey(hKey); 00116 return -2; 00117 } 00118 00119 extName = cmd_alloc(extLength * sizeof(TCHAR)); 00120 00121 for(keyCtr = 0; keyCtr < numKeys; keyCtr++) 00122 { 00123 DWORD buffer_size = extLength; 00124 return_val = RegEnumKeyEx(hKey, keyCtr, extName, &buffer_size, NULL, NULL, NULL, NULL); 00125 00126 if(return_val == ERROR_SUCCESS || return_val == ERROR_MORE_DATA) 00127 { 00128 if(*extName == _T('.')) 00129 PrintAssociation(extName); 00130 } 00131 else 00132 { 00133 cmd_free(extName); 00134 RegCloseKey(hKey); 00135 return -1; 00136 } 00137 } 00138 00139 RegCloseKey(hKey); 00140 00141 if(extName) 00142 cmd_free(extName); 00143 00144 return numKeys; 00145 } 00146 00147 static INT 00148 AddAssociation(LPTSTR extension, LPTSTR type) 00149 { 00150 DWORD return_val; 00151 HKEY hKey = NULL, insideKey = NULL; 00152 00153 return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_ALL_ACCESS, &hKey); 00154 00155 if(return_val != ERROR_SUCCESS) 00156 return -1; 00157 00158 return_val = RegCreateKeyEx(hKey, extension, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &insideKey, NULL); 00159 00160 if(return_val != ERROR_SUCCESS) 00161 { 00162 RegCloseKey(hKey); 00163 return -1; 00164 } 00165 00166 return_val = RegSetValueEx(insideKey, NULL, 0, REG_SZ, (LPBYTE)type, (_tcslen(type) + 1) * sizeof(TCHAR)); 00167 00168 if(return_val != ERROR_SUCCESS) 00169 { 00170 RegCloseKey(insideKey); 00171 RegCloseKey(hKey); 00172 return -2; 00173 } 00174 00175 RegCloseKey(insideKey); 00176 RegCloseKey(hKey); 00177 return 0; 00178 } 00179 00180 00181 static int 00182 RemoveAssociation(LPTSTR extension) 00183 { 00184 DWORD return_val; 00185 HKEY hKey; 00186 00187 return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Classes"), 0, KEY_ALL_ACCESS, &hKey); 00188 00189 if(return_val != ERROR_SUCCESS) 00190 return -1; 00191 00192 return_val = RegDeleteKey(hKey, extension); 00193 00194 if(return_val != ERROR_SUCCESS) 00195 { 00196 RegCloseKey(hKey); 00197 return -2; 00198 } 00199 00200 RegCloseKey(hKey); 00201 return 0; 00202 } 00203 00204 00205 00206 INT CommandAssoc (LPTSTR param) 00207 { 00208 00209 /* print help */ 00210 if (!_tcsncmp (param, _T("/?"), 2)) 00211 { 00212 ConOutResPaging(TRUE,STRING_ASSOC_HELP); 00213 return 0; 00214 } 00215 00216 nErrorLevel = 0; 00217 00218 if(_tcslen(param) == 0) 00219 PrintAllAssociations(); 00220 else 00221 { 00222 LPTSTR lpEqualSign = _tcschr(param, _T('=')); 00223 if(lpEqualSign != NULL) 00224 { 00225 LPTSTR fileType = lpEqualSign + 1; 00226 LPTSTR extension = cmd_alloc((lpEqualSign - param + 1) * sizeof(TCHAR)); 00227 00228 _tcsncpy(extension, param, lpEqualSign - param); 00229 extension[lpEqualSign - param] = (TCHAR)0; 00230 00231 if(_tcslen(fileType) == 0) 00232 /* if the equal sign is the last character 00233 in the string, then delete the key */ 00234 { 00235 RemoveAssociation(extension); 00236 } 00237 else 00238 /* otherwise, add the key and print out the association*/ 00239 { 00240 AddAssociation( extension, fileType); 00241 PrintAssociation(extension); 00242 } 00243 00244 cmd_free(extension); 00245 } 00246 else 00247 { 00248 /* no equal sign, print all associations */ 00249 INT retval = PrintAssociation(param); 00250 00251 if(retval == 0) /* if nothing printed out */ 00252 ConOutResPrintf(STRING_ASSOC_ERROR, param); 00253 } 00254 } 00255 00256 return 0; 00257 } 00258 00259 #endif /* INCLUDE_CMD_ASSOC */ Generated on Sun May 27 2012 04:18:09 for ReactOS by
1.7.6.1
|