Leaked source code of windows server 2003
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.

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