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.

147 lines
5.4 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. #include "svselfil.h"
  4. #include <windows.h>
  5. #include <shlobj.h>
  6. #include <shlobjp.h>
  7. HRESULT OpenShellFolder::OpenShellFolderAndSelectFile( HWND hWnd, const CSimpleDynamicArray<CSimpleString> &Filenames )
  8. {
  9. //
  10. // Assume failure
  11. //
  12. HRESULT hr = E_FAIL;
  13. //
  14. // Make sure we have some files
  15. //
  16. if (Filenames.Size())
  17. {
  18. //
  19. // Save the path name from the first file
  20. //
  21. TCHAR szPath[MAX_PATH];
  22. StrCpyN( szPath, Filenames[0], ARRAYSIZE(szPath));
  23. //
  24. // Remove the filename and extension
  25. //
  26. if (PathRemoveFileSpec( szPath ))
  27. {
  28. //
  29. // Create the path's IDLIST
  30. //
  31. LPITEMIDLIST pidlFolder = NULL;
  32. if (SUCCEEDED(SHParseDisplayName( szPath, NULL, &pidlFolder, NULL, NULL )) && pidlFolder)
  33. if (pidlFolder)
  34. {
  35. //
  36. // Create an array to contain the list of filenames
  37. //
  38. LPCITEMIDLIST *ppidlFullyQualified = new LPCITEMIDLIST[Filenames.Size()];
  39. if (ppidlFullyQualified)
  40. {
  41. //
  42. // Make sure the array doesn't contain any wild pointers
  43. //
  44. ZeroMemory(ppidlFullyQualified,sizeof(LPCITEMIDLIST) * Filenames.Size() );
  45. //
  46. // Create the list of relative pidls
  47. //
  48. LPCITEMIDLIST *ppidlRelative = new LPCITEMIDLIST[Filenames.Size()];
  49. if (ppidlRelative)
  50. {
  51. //
  52. // Make sure the array doesn't contain any wild pointers
  53. //
  54. ZeroMemory(ppidlRelative,sizeof(LPCITEMIDLIST) * Filenames.Size() );
  55. //
  56. // Create the list of fully qualified pidls
  57. //
  58. int nFileCount = 0;
  59. for (int i=0;i<Filenames.Size();i++)
  60. {
  61. //
  62. // Get the fully qualified PIDL for this file
  63. //
  64. LPITEMIDLIST pidlFullyQualified = NULL;
  65. if (SUCCEEDED(SHParseDisplayName( Filenames[i], NULL, &pidlFullyQualified, NULL, NULL )) && pidlFullyQualified)
  66. {
  67. //
  68. // Get the last part of the PIDL
  69. //
  70. LPITEMIDLIST pidlRelative = ILFindLastID(pidlFullyQualified);
  71. if (pidlRelative)
  72. {
  73. //
  74. // Save the pidl in our list of fully qualified PIDLs and relative PIDLs
  75. //
  76. ppidlFullyQualified[nFileCount] = pidlFullyQualified;
  77. ppidlRelative[nFileCount] = pidlRelative;
  78. //
  79. // Increment the file count
  80. //
  81. nFileCount++;
  82. //
  83. // Set the fully qualified PIDL to NULL so we don't free it
  84. //
  85. pidlFullyQualified = NULL;
  86. }
  87. //
  88. // If the PIDL is non-NULL here, free it. Otherwise, it will get freed below.
  89. //
  90. if (pidlFullyQualified)
  91. {
  92. ILFree(pidlFullyQualified);
  93. }
  94. }
  95. }
  96. //
  97. // If we have a file count, open the folder and select the items
  98. //
  99. if (nFileCount)
  100. {
  101. hr = SHOpenFolderAndSelectItems( pidlFolder, nFileCount, ppidlRelative, 0 );
  102. }
  103. //
  104. // Free all of the fully qualified PIDLs
  105. //
  106. for (int i=0;i<Filenames.Size();i++)
  107. {
  108. if (ppidlFullyQualified[i])
  109. {
  110. ILFree(const_cast<LPITEMIDLIST>(ppidlFullyQualified[i]));
  111. }
  112. }
  113. //
  114. // Free the PIDL array containing the relative pidls
  115. //
  116. delete[] ppidlRelative;
  117. }
  118. //
  119. // Free the PIDL array containing the fully qualified pidls
  120. //
  121. delete[] ppidlFullyQualified;
  122. }
  123. //
  124. // Free the folder PIDL
  125. //
  126. ILFree(pidlFolder);
  127. }
  128. }
  129. }
  130. return hr;
  131. }