2 * Assoc.C - assoc internal command.
7 * 14-Mar-2009 Lee C. Baker
8 * - initial implementation
10 * 15-Mar-2009 Lee C. Baker
11 * - Don't write to (or use) HKEY_CLASSES_ROOT directly
12 * - Externalize strings
15 * - PrintAllAssociations might could be optimized to not fetch all registry subkeys under 'Classes', just the ones that start with '.'
16 * - Make sure that non-administrator users can list associations, and get appropriate error messages when they don't have sufficient
17 * priveleges to perform an operation
23 #ifdef INCLUDE_CMD_ASSOC
26 PrintAssociation(LPTSTR extension
)
29 HKEY hKey
= NULL
, hInsideKey
= NULL
;
31 DWORD fileTypeLength
= 0;
32 LPTSTR fileType
= NULL
;
34 return_val
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("SOFTWARE\\Classes"), 0, KEY_READ
, &hKey
);
36 if(return_val
!= ERROR_SUCCESS
)
42 return_val
= RegOpenKeyEx(hKey
, extension
, 0, KEY_READ
, &hInsideKey
);
44 if(return_val
!= ERROR_SUCCESS
)
47 RegCloseKey(hInsideKey
);
51 /* obtain string length */
52 return_val
= RegQueryValueEx(hInsideKey
, NULL
, NULL
, NULL
, NULL
, &fileTypeLength
);
54 if(return_val
== ERROR_FILE_NOT_FOUND
) /* no default value, don't display */
56 RegCloseKey(hInsideKey
);
61 if(return_val
!= ERROR_SUCCESS
)
63 RegCloseKey(hInsideKey
);
68 fileType
= cmd_alloc(fileTypeLength
* sizeof(TCHAR
));
70 /* obtain actual file type */
71 return_val
= RegQueryValueEx(hInsideKey
, NULL
, NULL
, NULL
, (LPBYTE
) fileType
, &fileTypeLength
);
73 RegCloseKey(hInsideKey
);
76 if(return_val
!= ERROR_SUCCESS
)
82 if(fileTypeLength
!= 0) /* if there is a default key, display relevant information */
84 ConOutPrintf(_T("%s=%s\r\n"), extension
, fileType
);
94 PrintAllAssociations()
101 LPTSTR extName
= NULL
;
104 return_val
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("SOFTWARE\\Classes"), 0, KEY_READ
, &hKey
);
106 if(return_val
!= ERROR_SUCCESS
)
112 return_val
= RegQueryInfoKey(hKey
, NULL
, NULL
, NULL
, &numKeys
, &extLength
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
114 if(return_val
!= ERROR_SUCCESS
)
120 extName
= cmd_alloc(extLength
* sizeof(TCHAR
));
122 for(keyCtr
= 0; keyCtr
< numKeys
; keyCtr
++)
124 DWORD buffer_size
= extLength
;
125 return_val
= RegEnumKeyEx(hKey
, keyCtr
, extName
, &buffer_size
, NULL
, NULL
, NULL
, NULL
);
127 if(return_val
== ERROR_SUCCESS
|| return_val
== ERROR_MORE_DATA
)
129 if(*extName
== _T('.'))
130 PrintAssociation(extName
);
149 AddAssociation(LPTSTR extension
, LPTSTR type
)
152 HKEY hKey
= NULL
, insideKey
= NULL
;
154 return_val
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("SOFTWARE\\Classes"), 0, KEY_ALL_ACCESS
, &hKey
);
156 if(return_val
!= ERROR_SUCCESS
)
159 return_val
= RegCreateKeyEx(hKey
, extension
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &insideKey
, NULL
);
161 if(return_val
!= ERROR_SUCCESS
)
167 return_val
= RegSetValueEx(insideKey
, NULL
, 0, REG_SZ
, (LPBYTE
)type
, (_tcslen(type
) + 1) * sizeof(TCHAR
));
169 if(return_val
!= ERROR_SUCCESS
)
171 RegCloseKey(insideKey
);
176 RegCloseKey(insideKey
);
183 RemoveAssociation(LPTSTR extension
)
188 return_val
= RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("SOFTWARE\\Classes"), 0, KEY_ALL_ACCESS
, &hKey
);
190 if(return_val
!= ERROR_SUCCESS
)
193 return_val
= RegDeleteKey(hKey
, extension
);
195 if(return_val
!= ERROR_SUCCESS
)
207 INT
CommandAssoc (LPTSTR param
)
210 LPTSTR lpEqualSign
= NULL
;
213 if (!_tcsncmp (param
, _T("/?"), 2))
215 ConOutResPaging(TRUE
,STRING_ASSOC_HELP
);
221 if(_tcslen(param
) == 0)
222 PrintAllAssociations();
225 lpEqualSign
= _tcschr(param
, _T('='));
226 if(lpEqualSign
!= NULL
)
228 LPTSTR fileType
= lpEqualSign
+ 1;
229 LPTSTR extension
= cmd_alloc((lpEqualSign
- param
+ 1) * sizeof(TCHAR
));
231 _tcsncpy(extension
, param
, lpEqualSign
- param
);
232 extension
[lpEqualSign
- param
] = (TCHAR
)0;
234 if(_tcslen(fileType
) == 0)
235 /* if the equal sign is the last character
236 in the string, then delete the key */
238 RemoveAssociation(extension
);
241 /* otherwise, add the key and print out the association*/
243 AddAssociation( extension
, fileType
);
244 PrintAssociation(extension
);
251 /* no equal sign, print all associations */
252 INT retval
= PrintAssociation(param
);
254 if(retval
== 0) /* if nothing printed out */
255 ConOutResPrintf(STRING_ASSOC_ERROR
, param
);
262 #endif /* INCLUDE_CMD_ASSOC */