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.

441 lines
12 KiB

  1. /*************************************************************************
  2. FileName : DlgWindow.cpp
  3. Purpose : To accomodate the functions called for display of dialogs
  4. for the user to choose the name of the file which is to be
  5. saved to or to choose a file for filexfer by RA.
  6. Functions
  7. defined : InitializeOpenFileName,
  8. SaveTheFile,
  9. OpenTheFile,
  10. ResolveIt
  11. Author : Sudha Srinivasan (a-sudsi)
  12. *************************************************************************/
  13. #include "stdafx.h"
  14. #include "DlgWindow.h"
  15. #include "Resource.h"
  16. extern "C" {
  17. #include <shlobj.h>
  18. #include <objbase.h>
  19. }
  20. HINSTANCE g_hInst = NULL;
  21. extern CComBSTR g_bstrFileName;
  22. extern BOOL g_bFileNameSet;
  23. extern CComBSTR g_bstrFileType;
  24. extern CComBSTR g_bstrOpenFileName;
  25. extern CComBSTR g_bstrOpenFileSize;
  26. extern BOOL g_bOpenFileNameSet;
  27. OPENFILENAME g_OpenFileName;
  28. //
  29. // FUNCTION: InitializeOpenFileName()
  30. //
  31. // PURPOSE: Invokes common dialog function to save a file.
  32. //
  33. // COMMENTS:
  34. //
  35. // This function initializes the OPENFILENAME structure and calls
  36. // the GetSaveFileName() common dialog function.
  37. //
  38. // RETURN VALUES:
  39. // TRUE - The file name is chosen successfully and read into the buffer.
  40. // FALSE - No filename is chosen.
  41. //
  42. //
  43. void InitializeOpenFileName()
  44. {
  45. g_OpenFileName.lStructSize = sizeof(OPENFILENAME);
  46. g_OpenFileName.hwndOwner = GetFocus();
  47. g_OpenFileName.hInstance = g_hInst;
  48. g_OpenFileName.lpstrCustomFilter = NULL;
  49. g_OpenFileName.nMaxCustFilter = 0;
  50. g_OpenFileName.nFilterIndex = 0;
  51. g_OpenFileName.lpstrFileTitle = NULL;
  52. g_OpenFileName.nMaxFileTitle = 0;
  53. g_OpenFileName.lpstrInitialDir = NULL;
  54. g_OpenFileName.nFileOffset = 0;
  55. g_OpenFileName.nFileExtension = 0;
  56. g_OpenFileName.lpstrDefExt = NULL;
  57. g_OpenFileName.lCustData = NULL;
  58. g_OpenFileName.lpfnHook = NULL;
  59. g_OpenFileName.lpTemplateName = NULL;
  60. return;
  61. }
  62. //
  63. // FUNCTION: SaveTheFile()
  64. //
  65. // PURPOSE: Invokes common dialog function to save a file.
  66. //
  67. // COMMENTS:
  68. //
  69. // This function initializes the OPENFILENAME structure and calls
  70. // the GetSaveFileName() common dialog function.
  71. //
  72. // RETURN VALUES:
  73. // TRUE - The file name is chosen successfully and read into the buffer.
  74. // FALSE - No filename is chosen.
  75. //
  76. //
  77. DWORD SaveTheFile()
  78. {
  79. USES_CONVERSION;
  80. TCHAR szFile[MAX_PATH] = "\0";
  81. TCHAR szFilter[MAX_PATH+1] = "\0";
  82. TCHAR *tszFile =NULL;
  83. DWORD dwSuc = TRUE;
  84. HRESULT hr=S_OK;
  85. //Incase the user has given a filename to be displayed in the dialog.
  86. if (g_bFileNameSet)
  87. {
  88. tszFile = OLE2T(g_bstrFileName);
  89. if(NULL != tszFile)
  90. {
  91. //SWI
  92. //strcpy( szFile, tszFile);
  93. hr=StringCchCopy(szFile,ARRAYSIZE(szFile),tszFile);
  94. if(FAILED(hr))
  95. {
  96. dwSuc = FALSE;
  97. g_bstrFileName = "";
  98. goto DoneSaveTheFile;
  99. }
  100. }
  101. else
  102. {
  103. //
  104. // Error condition
  105. //
  106. dwSuc = FALSE;
  107. g_bstrFileName = "";
  108. goto DoneSaveTheFile;
  109. }
  110. }
  111. else
  112. {
  113. //SWI
  114. //strcpy( szFile, "");
  115. //the length of szFile is obviously greater than 1.
  116. //So return value not checked
  117. StringCchCopy(szFile,ARRAYSIZE(szFile),tszFile);
  118. }
  119. //To display file types.
  120. if (g_bstrFileType.Length() > 0)
  121. {
  122. //SWI
  123. //strcpy (szFilter, OLE2T(g_bstrFileType));
  124. hr=StringCchCopy(szFilter,ARRAYSIZE(szFilter), OLE2T(g_bstrFileType));
  125. if(FAILED(hr))
  126. {
  127. dwSuc = FALSE;
  128. g_bstrFileName = "";
  129. goto DoneSaveTheFile;
  130. }
  131. //SWI
  132. //lstrcat(szFilter, "\0\0");
  133. hr=StringCchCat(szFilter,ARRAYSIZE(szFilter),"\0\0");
  134. if(FAILED(hr))
  135. {
  136. dwSuc = FALSE;
  137. g_bstrFileName = "";
  138. goto DoneSaveTheFile;
  139. }
  140. }
  141. else
  142. {
  143. TCHAR szAllFilesFilter[MAX_PATH+1];
  144. LoadString(g_hInst, IDS_ALLFILESFILTER, szAllFilesFilter,MAX_PATH);
  145. //SWI
  146. //strcpy( szFilter, szAllFilesFilter);
  147. hr=StringCchCopy(szFilter,ARRAYSIZE(szFilter),szAllFilesFilter);
  148. if(FAILED(hr))
  149. {
  150. dwSuc = FALSE;
  151. g_bstrFileName = "";
  152. goto DoneSaveTheFile;
  153. }
  154. }
  155. // Fill in the OPENFILENAME structure to support a template and hook.
  156. InitializeOpenFileName();
  157. g_OpenFileName.lpstrFilter = szFilter;
  158. g_OpenFileName.lpstrFile = szFile;
  159. g_OpenFileName.nMaxFile = sizeof(szFile);
  160. TCHAR szSaveFile[MAX_PATH+1];
  161. LoadString(g_hInst, IDS_SAVEFILE, szSaveFile,MAX_PATH);
  162. g_OpenFileName.lpstrTitle = szSaveFile;
  163. g_OpenFileName.nFilterIndex = 1;
  164. g_OpenFileName.Flags = OFN_HIDEREADONLY | OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
  165. // Call the common dialog function.
  166. dwSuc = GetSaveFileName(&g_OpenFileName);
  167. if (dwSuc)
  168. {
  169. g_bstrFileName = g_OpenFileName.lpstrFile;
  170. }
  171. else
  172. {
  173. g_bstrFileName = "";
  174. }
  175. DoneSaveTheFile:
  176. return dwSuc;
  177. }
  178. //
  179. // FUNCTION: OpenTheFile()
  180. //
  181. // PURPOSE: Invokes common dialog function to open a file.
  182. //
  183. // COMMENTS:
  184. //
  185. // This function initializes the OPENFILENAME structure and calls
  186. // the GetOpenFileName() common dialog function.
  187. //
  188. // INPUTS:
  189. // The initial folder which has to be displayed in the open file dialog.
  190. // If this is NULL, it means that the default initial folder is to be
  191. // displayed. Hence this value is not checked explicitly when the
  192. // control enters the function.
  193. //
  194. // RETURN VALUES:
  195. // TRUE - The file name is chosen successfully and read into the buffer.
  196. // FALSE - No filename is chosen.
  197. //
  198. //
  199. DWORD OpenTheFile(TCHAR *pszInitialDir)
  200. {
  201. USES_CONVERSION;
  202. TCHAR szFile[MAX_PATH] = "\0";
  203. TCHAR szFilter[MAX_PATH+1] = "\0";
  204. ZeroMemory(szFilter, sizeof(TCHAR));
  205. HRESULT hr=S_OK;
  206. DWORD dwSuc = 0;
  207. //SWI
  208. //strcpy( szFile, "");
  209. StringCchCopy(szFile,ARRAYSIZE(szFile),"");
  210. TCHAR szAllFilesFilter[MAX_PATH+1];
  211. LoadString(g_hInst, IDS_ALLFILESFILTER, szAllFilesFilter,MAX_PATH);
  212. //SWI
  213. //strcpy( szFilter, szAllFilesFilter);
  214. hr=StringCchCopy(szFilter,ARRAYSIZE(szFilter),szAllFilesFilter);
  215. if(FAILED(hr))
  216. {
  217. return FALSE;
  218. }
  219. // Fill in the OPENFILENAME structure.
  220. InitializeOpenFileName();
  221. g_OpenFileName.lpstrFilter = szFilter;
  222. g_OpenFileName.lpstrFile = szFile;
  223. g_OpenFileName.nMaxFile = sizeof(szFile);
  224. g_OpenFileName.lpstrInitialDir = pszInitialDir;
  225. TCHAR szChooseFile[MAX_PATH+1];
  226. LoadString(g_hInst, IDS_CHOOSEFILE, szChooseFile,MAX_PATH);
  227. g_OpenFileName.lpstrTitle = szChooseFile;
  228. g_OpenFileName.Flags = OFN_PATHMUSTEXIST | OFN_EXPLORER ;
  229. // Call the common dialog function.
  230. dwSuc = GetOpenFileName(&g_OpenFileName);
  231. if (dwSuc)
  232. {
  233. g_bstrOpenFileName = g_OpenFileName.lpstrFile;
  234. // Grab the filesize and put it in g_bstrOpenFileSize
  235. HANDLE hFile = NULL;
  236. TCHAR fileSize[100];
  237. memset(fileSize, 0, sizeof(TCHAR) * 100);
  238. hFile = CreateFile(g_OpenFileName.lpstrFile,
  239. FILE_READ_ATTRIBUTES,
  240. 0,
  241. NULL,
  242. OPEN_EXISTING,
  243. FILE_ATTRIBUTE_NORMAL,
  244. NULL);
  245. if (hFile != INVALID_HANDLE_VALUE)
  246. {
  247. DWORD low = 0x0, high = 0x0;
  248. low = GetFileSize(hFile, &high);
  249. if (low != INVALID_FILE_SIZE)
  250. {
  251. wsprintf(fileSize, TEXT("%d"), low);
  252. g_bstrOpenFileSize = fileSize;
  253. }
  254. CloseHandle(hFile);
  255. }
  256. TCHAR *pstrTemp = NULL;
  257. pstrTemp = g_OpenFileName.lpstrFile;
  258. if (NULL == pstrTemp)
  259. {
  260. g_bstrOpenFileName = "";
  261. }
  262. // Find out whether it is a LNK file.
  263. else if (_tcsstr(pstrTemp, ".lnk") != NULL)
  264. {
  265. // Make the call to ResolveIt here.
  266. dwSuc = (DWORD)ResolveIt(pstrTemp);
  267. g_bstrOpenFileName = pstrTemp;
  268. }
  269. }
  270. else
  271. {
  272. g_bstrOpenFileName = "";
  273. }
  274. return dwSuc;
  275. }
  276. //
  277. // FUNCTION: ResolveIt
  278. //
  279. // PURPOSE: Find the destination of a shortcut.
  280. //
  281. // COMMENTS:
  282. //
  283. // This function resolves the short-cut and populates the global variables
  284. // and calls back the OpenTheFile to display the appropriate folder.
  285. //
  286. // RETURN VALUES:
  287. // standard hres codes
  288. //
  289. //
  290. HRESULT ResolveIt(TCHAR *pszShortcutFile)
  291. {
  292. HRESULT hres = S_OK;
  293. IShellLink *psl;
  294. USES_CONVERSION;
  295. TCHAR szGotPath[MAX_PATH];
  296. TCHAR szDescription[MAX_PATH];
  297. WIN32_FIND_DATA wfd;
  298. if ( NULL == pszShortcutFile )
  299. {
  300. return hres;
  301. }
  302. // Get a pointer to the IShellLink interface.
  303. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  304. IID_IShellLink, (void **)&psl);
  305. if (SUCCEEDED(hres))
  306. {
  307. // If hres is success, it means that psl is not NULL. Hence no explicit
  308. // check is done for NULL values.
  309. IPersistFile *ppf;
  310. // Get a pointer to the IPersistFile interface.
  311. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
  312. if (SUCCEEDED(hres))
  313. {
  314. // If hres is success, it means that ppf is not NULL. Hence no explicit
  315. // check is done for NULL values.
  316. WORD wsz[MAX_PATH]; // buffer for Unicode string
  317. // Ensure that the string consists of Unicode characters.
  318. MultiByteToWideChar(CP_ACP, 0, pszShortcutFile, -1, wsz,
  319. MAX_PATH);
  320. // Load the shortcut.
  321. hres = ppf->Load(wsz, STGM_READ);
  322. if (SUCCEEDED(hres))
  323. {
  324. // Resolve the shortcut.
  325. hres = psl->Resolve(GetFocus(), SLR_ANY_MATCH);
  326. if (SUCCEEDED(hres))
  327. {
  328. //SWI
  329. //_tcscpy(szGotPath, pszShortcutFile);
  330. hres=StringCchCopy(szGotPath,ARRAYSIZE(szGotPath),pszShortcutFile);
  331. if(FAILED(hres))
  332. {
  333. ppf->Release();
  334. psl->Release();
  335. return hres;
  336. }
  337. // Get the parth to the shortcut target.
  338. hres = psl->GetPath(szGotPath, MAX_PATH,
  339. (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH );
  340. if (!SUCCEEDED(hres))
  341. {
  342. TCHAR szErrMsg[MAX_PATH+1];
  343. LoadString(g_hInst, IDS_URECLNKFILE, szErrMsg,MAX_PATH);
  344. TCHAR szErrCaption[MAX_PATH+1];
  345. LoadString(g_hInst, IDS_GETPATHFAILED, szErrCaption,MAX_PATH);
  346. MessageBox(GetFocus(), szErrMsg, szErrCaption, MB_OK);
  347. }
  348. // Get the description of the target.
  349. hres = psl->GetDescription(szDescription, MAX_PATH);
  350. if (!SUCCEEDED(hres))
  351. {
  352. TCHAR szErrMsg[MAX_PATH+1];
  353. LoadString(g_hInst, IDS_URECLNKFILE, szErrMsg,MAX_PATH);
  354. TCHAR szErrCaption[MAX_PATH+1];
  355. LoadString(g_hInst, IDS_GETDESCFAILED, szErrCaption,MAX_PATH);
  356. MessageBox(GetFocus(), szErrMsg, szErrCaption, MB_OK);
  357. }
  358. //hres = OpenTheFile(szGotPath);
  359. lstrcpy(pszShortcutFile,szGotPath);
  360. hres = 1;
  361. }
  362. }
  363. // Release the pointer to IPersistFile.
  364. // ppf is not checked for NULL value because the control wouldn't come here
  365. // otherwise (in the case where QueryInterface would have failed).
  366. ppf->Release();
  367. }
  368. // Release the pointer to IShellLink.
  369. // psl is not checked for NULL value because the control comes here only if it is
  370. // not NULL when the CoCreateInstance succeeds.
  371. psl->Release();
  372. }
  373. return hres;
  374. }