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.

331 lines
9.0 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: crfile.cpp
  7. //
  8. // Contents: Cert Server wrapper routines
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. HRESULT
  14. myFixupRCFilterString(WCHAR *szFilter)
  15. {
  16. if (NULL == szFilter)
  17. return S_OK;
  18. // translate to end of string
  19. for(LPWSTR szTmpPtr=szFilter; szTmpPtr=wcschr(szTmpPtr, L'|'); )
  20. {
  21. // replace every "|" with NULL termination
  22. szTmpPtr[0] = L'\0';
  23. szTmpPtr++;
  24. }
  25. return S_OK;
  26. }
  27. HRESULT
  28. myGetFileName(
  29. IN HWND hwndOwner,
  30. IN HINSTANCE hInstance,
  31. IN BOOL fOpen,
  32. OPTIONAL IN int iRCTitle,
  33. OPTIONAL IN WCHAR const *pwszTitleInsert,
  34. OPTIONAL IN int iRCFilter,
  35. OPTIONAL IN int iRCDefExt,
  36. OPTIONAL IN DWORD Flags,
  37. OPTIONAL IN WCHAR const *pwszDefaultFile,
  38. OUT WCHAR **ppwszFile)
  39. {
  40. HRESULT hr;
  41. WCHAR *pwszTitle = NULL;
  42. WCHAR *pwszExpandedTitle = NULL;
  43. WCHAR *pwszFilter = NULL;
  44. WCHAR *pwszDefExt = NULL;
  45. WCHAR wszFileName[MAX_PATH] = L"\0";
  46. WCHAR wszEmptyFilter[] = L"\0";
  47. WCHAR wszPath[MAX_PATH];
  48. WCHAR *pwszFilePortion;
  49. DWORD dwFileAttr;
  50. BOOL fGetFile;
  51. OPENFILENAME ofn;
  52. CSASSERT(NULL != ppwszFile);
  53. // init
  54. *ppwszFile = NULL;
  55. ZeroMemory(&ofn, sizeof(OPENFILENAME));
  56. if (0 != iRCTitle)
  57. {
  58. // load title
  59. hr = myLoadRCString(hInstance, iRCTitle, &pwszTitle);
  60. if (S_OK != hr)
  61. {
  62. CSASSERT(NULL == pwszTitle);
  63. _PrintError(hr, "myLoadECString(iRCTitle)");
  64. }
  65. else if (NULL != pwszTitleInsert)
  66. {
  67. // replace %1
  68. if (FormatMessage(
  69. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  70. FORMAT_MESSAGE_FROM_STRING |
  71. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  72. pwszTitle,
  73. 0,
  74. 0,
  75. reinterpret_cast<WCHAR *>(&pwszExpandedTitle),
  76. 0,
  77. reinterpret_cast<va_list *>
  78. (const_cast<WCHAR **>(&pwszTitleInsert))) )
  79. {
  80. CSASSERT(NULL != pwszExpandedTitle);
  81. // free title with %1
  82. LocalFree(pwszTitle);
  83. pwszTitle = pwszExpandedTitle;
  84. pwszExpandedTitle = NULL;
  85. }
  86. }
  87. }
  88. if (0 != iRCFilter)
  89. {
  90. // load filter
  91. hr = myLoadRCString(hInstance, iRCFilter, &pwszFilter);
  92. if (S_OK != hr)
  93. {
  94. CSASSERT(NULL == pwszFilter);
  95. _PrintError(hr, "myLoadECString(iRCFilter)");
  96. }
  97. if (NULL == pwszFilter)
  98. {
  99. //point to empty one
  100. pwszFilter = wszEmptyFilter;
  101. }
  102. else
  103. {
  104. hr = myFixupRCFilterString(pwszFilter);
  105. _JumpIfError(hr, error , "myFixupRCFilterString");
  106. }
  107. }
  108. if (0 != iRCDefExt)
  109. {
  110. // load default extension
  111. hr = myLoadRCString(hInstance, iRCDefExt, &pwszDefExt);
  112. if (S_OK != hr)
  113. {
  114. CSASSERT(NULL == pwszDefExt);
  115. _PrintError(hr, "myLoadECString(iRCDefExt)");
  116. }
  117. }
  118. ofn.lStructSize = CCSIZEOF_STRUCT(OPENFILENAME, lpTemplateName);
  119. ofn.hwndOwner = hwndOwner;
  120. ofn.hInstance = hInstance;
  121. ofn.lpstrTitle = pwszTitle;
  122. ofn.lpstrFilter = pwszFilter;
  123. ofn.lpstrDefExt = pwszDefExt;
  124. ofn.Flags = Flags;
  125. ofn.lpstrFile = wszFileName; // for out
  126. ofn.nMaxFile = ARRAYSIZE(wszFileName);
  127. if (NULL != pwszDefaultFile)
  128. {
  129. // analysis of default directory and file
  130. dwFileAttr = GetFileAttributes(pwszDefaultFile);
  131. if (0xFFFFFFFF == dwFileAttr &&
  132. HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) != (hr = myHLastError()) )
  133. {
  134. // error, ignore, pop up file dialog without defaults
  135. _PrintError(hr, "GetFileAttributes");
  136. }
  137. else
  138. {
  139. if (0xFFFFFFFF != dwFileAttr &&
  140. FILE_ATTRIBUTE_DIRECTORY & dwFileAttr)
  141. {
  142. // only pass a dircetory path
  143. ofn.lpstrInitialDir = pwszDefaultFile;
  144. }
  145. else
  146. {
  147. // full path
  148. pwszFilePortion = NULL; // init
  149. if (0 == GetFullPathName(
  150. pwszDefaultFile,
  151. ARRAYSIZE(wszPath),
  152. wszPath,
  153. &pwszFilePortion) )
  154. {
  155. // error, ignore
  156. hr = myHLastError();
  157. _PrintError(hr, "GetFullPathName");
  158. }
  159. else
  160. {
  161. if (NULL != pwszFilePortion)
  162. {
  163. wcscpy(wszFileName, pwszFilePortion);
  164. }
  165. *pwszFilePortion = L'\0'; // make init dir
  166. ofn.lpstrInitialDir = wszPath;
  167. }
  168. }
  169. }
  170. }
  171. if (fOpen)
  172. {
  173. fGetFile = GetOpenFileName(&ofn);
  174. }
  175. else
  176. {
  177. fGetFile = GetSaveFileName(&ofn);
  178. }
  179. if (!fGetFile)
  180. {
  181. hr = CommDlgExtendedError();
  182. if (S_OK == hr)
  183. {
  184. // cancel would make Get?FileName return FALSE but no error
  185. goto done;
  186. }
  187. _JumpError(hr, error, "GetOpenFileName");
  188. }
  189. // ok get file name
  190. hr = myDupString(wszFileName, ppwszFile);
  191. _JumpIfError(hr, error, "myDupString");
  192. done:
  193. hr = S_OK;
  194. error:
  195. if (NULL != pwszTitle)
  196. {
  197. LocalFree(pwszTitle);
  198. }
  199. if (NULL != pwszExpandedTitle)
  200. {
  201. LocalFree(pwszExpandedTitle);
  202. }
  203. if (NULL != pwszFilter && pwszFilter != wszEmptyFilter)
  204. {
  205. LocalFree(pwszFilter);
  206. }
  207. if (NULL != pwszDefExt)
  208. {
  209. LocalFree(pwszDefExt);
  210. }
  211. return hr;
  212. }
  213. HRESULT
  214. myGetOpenFileName(
  215. IN HWND hwndOwner,
  216. IN HINSTANCE hInstance,
  217. OPTIONAL IN int iRCTitle,
  218. OPTIONAL IN int iRCFilter,
  219. OPTIONAL IN int iRCDefExt,
  220. OPTIONAL IN DWORD Flags,
  221. OPTIONAL IN WCHAR const *pwszDefaultFile,
  222. OUT WCHAR **ppwszFile)
  223. {
  224. return myGetFileName(
  225. hwndOwner,
  226. hInstance,
  227. TRUE, // open file
  228. iRCTitle,
  229. NULL,
  230. iRCFilter,
  231. iRCDefExt,
  232. Flags,
  233. pwszDefaultFile,
  234. ppwszFile);
  235. }
  236. HRESULT
  237. myGetSaveFileName(
  238. IN HWND hwndOwner,
  239. IN HINSTANCE hInstance,
  240. OPTIONAL IN int iRCTitle,
  241. OPTIONAL IN int iRCFilter,
  242. OPTIONAL IN int iRCDefExt,
  243. OPTIONAL IN DWORD Flags,
  244. OPTIONAL IN WCHAR const *pwszDefaultFile,
  245. OUT WCHAR **ppwszFile)
  246. {
  247. return myGetFileName(
  248. hwndOwner,
  249. hInstance,
  250. FALSE, // save file
  251. iRCTitle,
  252. NULL,
  253. iRCFilter,
  254. iRCDefExt,
  255. Flags,
  256. pwszDefaultFile,
  257. ppwszFile);
  258. }
  259. HRESULT
  260. myGetOpenFileNameEx(
  261. IN HWND hwndOwner,
  262. IN HINSTANCE hInstance,
  263. OPTIONAL IN int iRCTitle,
  264. OPTIONAL IN WCHAR const *pwszTitleInsert,
  265. OPTIONAL IN int iRCFilter,
  266. OPTIONAL IN int iRCDefExt,
  267. OPTIONAL IN DWORD Flags,
  268. OPTIONAL IN WCHAR const *pwszDefaultFile,
  269. OUT WCHAR **ppwszFile)
  270. {
  271. return myGetFileName(
  272. hwndOwner,
  273. hInstance,
  274. TRUE, // open file
  275. iRCTitle,
  276. pwszTitleInsert,
  277. iRCFilter,
  278. iRCDefExt,
  279. Flags,
  280. pwszDefaultFile,
  281. ppwszFile);
  282. }
  283. HRESULT
  284. myGetSaveFileNameEx(
  285. IN HWND hwndOwner,
  286. IN HINSTANCE hInstance,
  287. OPTIONAL IN int iRCTitle,
  288. OPTIONAL IN WCHAR const *pwszTitleInsert,
  289. OPTIONAL IN int iRCFilter,
  290. OPTIONAL IN int iRCDefExt,
  291. OPTIONAL IN DWORD Flags,
  292. OPTIONAL IN WCHAR const *pwszDefaultFile,
  293. OUT WCHAR **ppwszFile)
  294. {
  295. return myGetFileName(
  296. hwndOwner,
  297. hInstance,
  298. FALSE, // save file
  299. iRCTitle,
  300. pwszTitleInsert,
  301. iRCFilter,
  302. iRCDefExt,
  303. Flags,
  304. pwszDefaultFile,
  305. ppwszFile);
  306. }