Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
11 KiB

  1. // app.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <stdio.h>
  6. #include <shlobj.h>
  7. #include <shfolder.h>
  8. #include <windowsx.h>
  9. #include <resource.h>
  10. #define chHANDLE_DLGMSG(hwnd, message, fn) \
  11. case (message): return (SetDlgMsgResult(hwnd, uMsg, \
  12. HANDLE_##message((hwnd), (wParam), (lParam), (fn))))
  13. #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
  14. #define SHAnsiToUnicode(psz, pwsz, cchwsz) MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, cchwsz);
  15. #define SHUnicodeToAnsi(pwsz, psz, cchsz) WideCharToMultiByte(CP_ACP, 0, pwsz, -1, psz, cchsz, NULL, NULL);
  16. #ifdef UNICODE
  17. #else
  18. #define SHTCharToUnicode(t, w, cc) SHAnsiToUnicode(t, w, cc)
  19. #endif
  20. #define Q(x) #x,x
  21. #define HKCU HKEY_CURRENT_USER
  22. #define HKLM HKEY_LOCAL_MACHINE
  23. struct {
  24. CHAR *pszName;
  25. UINT csidl;
  26. HKEY hkey;
  27. char *pszRegValue;
  28. } Folders[] = {
  29. Q(CSIDL_DESKTOP), 0,0,
  30. Q(CSIDL_INTERNET),0,0,
  31. Q(CSIDL_PROGRAMS), HKCU,"Programs",
  32. Q(CSIDL_CONTROLS), 0,0,
  33. Q(CSIDL_PRINTERS), 0,0,
  34. Q(CSIDL_PERSONAL), HKCU, "Personal",
  35. Q(CSIDL_FAVORITES), HKCU, "Favorites",
  36. Q(CSIDL_STARTUP), HKCU, "Startup",
  37. Q(CSIDL_RECENT), HKCU, "Recent",
  38. Q(CSIDL_SENDTO), HKCU, "SendTo",
  39. Q(CSIDL_BITBUCKET),0,0,
  40. Q(CSIDL_STARTMENU), HKCU, "Start Menu",
  41. Q(CSIDL_DESKTOPDIRECTORY), HKCU, "Desktop",
  42. Q(CSIDL_DRIVES), 0,0,
  43. Q(CSIDL_NETWORK), 0,0,
  44. Q(CSIDL_NETHOOD), HKCU, "NetHood",
  45. Q(CSIDL_FONTS), HKCU, "Fonts",
  46. Q(CSIDL_TEMPLATES), HKCU, "Templates",
  47. Q(CSIDL_COMMON_STARTMENU),HKLM, "Common Start Menu",
  48. Q(CSIDL_COMMON_PROGRAMS), HKLM, "Common Programs",
  49. Q(CSIDL_COMMON_STARTUP), HKLM, "Common Startup",
  50. Q(CSIDL_COMMON_DESKTOPDIRECTORY), HKLM, "Common Desktop",
  51. Q(CSIDL_APPDATA), HKCU, "AppData",
  52. Q(CSIDL_PRINTHOOD), HKCU, "PrintHood",
  53. Q(CSIDL_LOCAL_APPDATA), HKCU, "Local AppData",
  54. Q(CSIDL_ALTSTARTUP), HKCU, "AltStartup",
  55. Q(CSIDL_COMMON_ALTSTARTUP), HKLM, "Common AltStartup",
  56. Q(CSIDL_COMMON_FAVORITES), HKLM, "Common Favorites",
  57. Q(CSIDL_INTERNET_CACHE), HKCU, "Cache",
  58. Q(CSIDL_COOKIES), HKCU, "Cookies",
  59. Q(CSIDL_HISTORY), HKCU, "History",
  60. Q(CSIDL_COMMON_APPDATA), HKLM, "Common AppData",
  61. Q(CSIDL_WINDOWS),0,0,
  62. Q(CSIDL_SYSTEM),0,0,
  63. Q(CSIDL_PROGRAM_FILES),0,0,
  64. Q(CSIDL_MYPICTURES),HKCU, "My Pictures",
  65. Q(CSIDL_PROFILE),0,0,
  66. Q(CSIDL_SYSTEMX86),0,0,
  67. Q(CSIDL_PROGRAM_FILESX86),0,0,
  68. Q(CSIDL_PROGRAM_FILES_COMMON),0,0,
  69. Q(CSIDL_PROGRAM_FILES_COMMONX86),0,0,
  70. Q(CSIDL_COMMON_TEMPLATES),HKLM, "Common Templates",
  71. Q(CSIDL_COMMON_DOCUMENTS), HKLM, "Common Documents",
  72. Q(CSIDL_COMMON_ADMINTOOLS), HKLM, "Common Administrative Tools",
  73. Q(CSIDL_ADMINTOOLS), HKCU, "Administrative Tools",
  74. /*
  75. */
  76. "Invalid",0xff,0,0,
  77. };
  78. HANDLE GetCurrentUserToken()
  79. {
  80. HANDLE hToken;
  81. if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken) ||
  82. OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
  83. return hToken;
  84. return NULL;
  85. }
  86. const CHAR c_szUSF[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
  87. const CHAR c_szSF[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
  88. void AddToList(HWND hwndList, UINT iItem, HRESULT hr, UINT iFolder, BOOL fUnicode, LPBYTE pzPath, BOOL fCreate)
  89. {
  90. LVITEMA lva;
  91. LVITEMW lvw;
  92. CHAR sz[MAX_PATH];
  93. WCHAR wsz[MAX_PATH];
  94. ZeroMemory(&lva, sizeof(lva));
  95. ZeroMemory(&lvw, sizeof(lvw));
  96. lva.mask = lvw.mask = LVIF_TEXT;
  97. lva.iItem = lvw.iItem = iItem;
  98. wsprintf(sz, "%x", hr);
  99. lva.pszText = sz;
  100. lva.iSubItem = 0;
  101. if (iItem != SendMessageA(hwndList, LVM_INSERTITEMA, 0, (LPARAM)&lva))
  102. MessageBox(0, "debug",0, MB_OK);
  103. if (fUnicode)
  104. {
  105. lvw.pszText = (WCHAR*)pzPath;
  106. lvw.iSubItem = 1;
  107. SendMessageW(hwndList,LVM_SETITEMW,0, (LPARAM)&lvw);
  108. lva.pszText = "1";
  109. lva.iSubItem = 4;
  110. SendMessageA(hwndList, LVM_SETITEMA, 0, (LPARAM)&lva);
  111. }
  112. else
  113. {
  114. lva.pszText = (CHAR*)pzPath;
  115. lva.iSubItem = 1;
  116. SendMessageA(hwndList,LVM_SETITEMA,0, (LPARAM)&lva);
  117. lva.pszText = "0";
  118. lva.iSubItem = 4;
  119. SendMessageA(hwndList,LVM_SETITEMA, 0, (LPARAM)&lva);
  120. }
  121. if (Folders[iFolder].hkey)
  122. {
  123. HKEY hkey;
  124. DWORD dwType;
  125. DWORD dwSize;
  126. wsz[0] = 0;
  127. sz[0] = 0;
  128. if (ERROR_SUCCESS == RegOpenKey(Folders[iFolder].hkey, c_szUSF, &hkey))
  129. {
  130. if (fUnicode)
  131. {
  132. WCHAR wszRegValue[MAX_PATH];
  133. dwSize = sizeof(sz);
  134. SHAnsiToUnicode( Folders[iFolder].pszRegValue, wszRegValue, MAX_PATH);
  135. RegQueryValueExW(hkey, wszRegValue, 0, &dwType, (LPBYTE) wsz, &dwSize);
  136. lvw.iSubItem = 2;
  137. lvw.pszText = wsz;
  138. SendMessageW(hwndList, LVM_SETITEMW, 0, (LPARAM)&lvw);
  139. }
  140. else
  141. {
  142. DWORD dwSize = sizeof(sz);
  143. RegQueryValueExA(hkey, Folders[iFolder].pszRegValue, 0, &dwType, (LPBYTE)sz,&dwSize );
  144. lva.iSubItem = 2;
  145. lva.pszText = sz;
  146. SendMessageA(hwndList, LVM_SETITEMA, 0, (LPARAM)&lva);
  147. }
  148. RegCloseKey(hkey);
  149. }
  150. if (ERROR_SUCCESS == RegOpenKey(Folders[iFolder].hkey, c_szSF, &hkey))
  151. {
  152. if (fUnicode)
  153. {
  154. WCHAR wszRegValue[MAX_PATH];
  155. dwSize = sizeof(wsz);
  156. SHAnsiToUnicode( Folders[iFolder].pszRegValue, wszRegValue, MAX_PATH);
  157. RegQueryValueExW(hkey, wszRegValue, 0, &dwType, (LPBYTE) wsz, &dwSize);
  158. lvw.iSubItem = 3;
  159. lvw.pszText = wsz;
  160. SendMessageW(hwndList, LVM_SETITEMW, 0, (LPARAM)&lvw);
  161. }
  162. else
  163. {
  164. DWORD dwSize = sizeof(sz);
  165. RegQueryValueExA(hkey, Folders[iFolder].pszRegValue, 0, &dwType, (LPBYTE)sz, &dwSize);
  166. lva.iSubItem = 3;
  167. lva.pszText = sz;
  168. SendMessageA(hwndList, LVM_SETITEMA, 0, (LPARAM)&lva);
  169. }
  170. RegCloseKey(hkey);
  171. }
  172. }
  173. else
  174. {
  175. lva.pszText = "N/A";
  176. lva.iSubItem = 2;
  177. SendMessageA(hwndList, LVM_SETITEMA, 0, (LPARAM)&lva);
  178. lva.iSubItem = 3;
  179. SendMessageA(hwndList, LVM_SETITEMA, 0, (LPARAM)&lva);
  180. }
  181. lva.iSubItem = 5;
  182. if (fCreate)
  183. lva.pszText = "1";
  184. else
  185. lva.pszText = "0";
  186. SendMessageA(hwndList, LVM_SETITEMA, 0, (LPARAM)&lva);
  187. }
  188. void CheckFolders( HWND hList)
  189. {
  190. int i;
  191. int j;
  192. static HINSTANCE hmod;
  193. PFNSHGETFOLDERPATHA GetFolderPathA;
  194. PFNSHGETFOLDERPATHW GetFolderPathW;
  195. CHAR szDll[] = "shfolder.dll";
  196. hmod = LoadLibrary(szDll);
  197. if( !hmod) {
  198. MessageBoxA(0,"couldn't find your dll %s\n", 0, MB_OK);
  199. ExitProcess(1);
  200. }
  201. GetFolderPathA = (PFNSHGETFOLDERPATHA) GetProcAddress( hmod, "SHGetFolderPathA");
  202. GetFolderPathW = (PFNSHGETFOLDERPATHW) GetProcAddress( hmod, "SHGetFolderPathW");
  203. // SendMessageA(hList, LB_RESETCONTENT,0,0);
  204. SendMessage(hList, LVM_DELETEALLITEMS,0,0);
  205. for ( i=0,j=0; i< ARRAYSIZE(Folders); i++)
  206. {
  207. CHAR szPath[MAX_PATH];
  208. WCHAR wszPath[MAX_PATH];
  209. HRESULT hr;
  210. CHAR szOut[MAX_PATH];
  211. WCHAR wszOut[MAX_PATH];
  212. WCHAR wszConv[MAX_PATH];
  213. LPBYTE pzPath;
  214. int k;
  215. for (k=0; k<6; k++)
  216. {
  217. BOOL fCreate = (k == 3) || (k == 2);
  218. if (k%2)
  219. {
  220. hr = GetFolderPathW(NULL, Folders[i].csidl |fCreate, NULL,0, wszPath);
  221. pzPath = (LPBYTE) wszPath;
  222. }
  223. else
  224. {
  225. hr = GetFolderPathA(NULL, Folders[i].csidl | fCreate, NULL, 0, szPath);
  226. pzPath = (LPBYTE) szPath;
  227. }
  228. AddToList(hList, j++ ,hr, i, k%2, pzPath, fCreate);
  229. }
  230. }
  231. }
  232. BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus,
  233. LPARAM lParam)
  234. {
  235. RECT rc;
  236. LVCOLUMN lvc;
  237. HWND hwndList = GetDlgItem(hwnd, IDC_LIST1);
  238. ZeroMemory(&lvc, sizeof(lvc));
  239. lvc.mask = LVCF_FMT | LVCF_ORDER |LVCF_SUBITEM | LVCF_TEXT;
  240. lvc.fmt = LVCFMT_LEFT;
  241. lvc.pszText = "HRESULT";
  242. lvc.iOrder = 0;
  243. lvc.iSubItem = 0;
  244. ListView_InsertColumn(hwndList, 0, &lvc);
  245. lvc.pszText = "Value";
  246. lvc.iOrder = 1;
  247. lvc.iSubItem = 1;
  248. ListView_InsertColumn(hwndList, 1, &lvc);
  249. lvc.pszText ="USF value";
  250. lvc.iOrder =2 ;
  251. lvc.iSubItem = 2;
  252. ListView_InsertColumn(hwndList, 2, &lvc);
  253. lvc.pszText = "SF value";
  254. lvc.iOrder = 3;
  255. lvc.iSubItem = 3;
  256. ListView_InsertColumn(hwndList, 3, &lvc);
  257. lvc.pszText = "Unicode";
  258. lvc.iOrder = 4;
  259. lvc.iSubItem = 4;
  260. ListView_InsertColumn(hwndList, 4, &lvc);
  261. lvc.pszText = "Create";
  262. lvc.iOrder = 5;
  263. lvc.iSubItem = 5;
  264. ListView_InsertColumn(hwndList, 5, &lvc);
  265. // Associate an icon with the dialog box.
  266. CheckFolders(hwndList);
  267. GetClientRect(hwnd, &rc);
  268. SetWindowPos(GetDlgItem(hwnd, IDC_LIST1), NULL,
  269. 0, 0, rc.right, rc.bottom, SWP_NOZORDER);
  270. return(TRUE);
  271. }
  272. void Dlg_OnSize (HWND hwnd, UINT state, int cx, int cy) {
  273. // SetWindowPos(GetDlgItem(hwnd, IDC_LIST1), NULL, 0, 0,
  274. // cx, cy, SWP_NOZORDER);
  275. }
  276. void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl,
  277. UINT codeNotify)
  278. {
  279. switch (id)
  280. {
  281. case IDCANCEL:
  282. EndDialog(hwnd, id);
  283. break;
  284. case IDOK:
  285. // Call the recursive routine to walk the tree.
  286. CheckFolders(GetDlgItem(hwnd, IDC_LIST1));
  287. break;
  288. }
  289. }
  290. BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg,
  291. WPARAM wParam, LPARAM lParam)
  292. {
  293. switch (uMsg) {
  294. chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
  295. chHANDLE_DLGMSG(hwnd, WM_SIZE, Dlg_OnSize);
  296. chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
  297. }
  298. return(FALSE);
  299. }
  300. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  301. LPSTR pszArgs, INT command)
  302. {
  303. #if 0
  304. if (argc > 1)
  305. SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH, argv[1], NULL);
  306. Sleep(6000);
  307. #endif
  308. CoInitialize(0);
  309. DialogBox( hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, Dlg_Proc);
  310. GetLastError();
  311. #if 0
  312. SHFILEINFO fi;
  313. SHGetFileInfo(TEXT("\\\\chrisg06\\public"), FILE_ATTRIBUTE_DIRECTORY, &fi, sizeof(fi), SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME | SHGFI_ICON);
  314. {
  315. TCHAR szPath[MAX_PATH];
  316. SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, GetCurrentUserToken(), 0, szPath);
  317. SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, 0, szPath);
  318. SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES_COMMON, NULL, 0, szPath);
  319. SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS | CSIDL_FLAG_CREATE, NULL, 0, szPath);
  320. SHGetFolderPath(NULL, CSIDL_COMMON_TEMPLATES | CSIDL_FLAG_CREATE, NULL, 0, szPath);
  321. SHGetFolderPath(NULL, CSIDL_RECENT, NULL, 0, szPath);
  322. SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, 0, szPath);
  323. SHGetFolderPath(NULL, CSIDL_MYPICTURES | CSIDL_FLAG_CREATE, NULL, 0, szPath);
  324. SHGetFolderPath(NULL, CSIDL_MYPICTURES, NULL, 0, szPath);
  325. // SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, szPath);
  326. // SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, szPath);
  327. }
  328. #endif
  329. CoUninitialize();
  330. return 0;
  331. }