-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetColumnsCppAppATL.cpp
More file actions
117 lines (101 loc) · 3.74 KB
/
GetColumnsCppAppATL.cpp
File metadata and controls
117 lines (101 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <windows.h>
#include <shlobj.h>
#include <shobjidl.h>
#include <propkey.h>
#include <propsys.h>
#include <iostream>
#include <atlbase.h>
#include <atlcom.h>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "propsys.lib")
// TODO: break this method up into multiple methods
void ListFileExplorerDetailsViewVisibleColumns(LPCWSTR folderPath) {
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) {
std::wcout << L"Failed to initialize COM. Error: " << hr << std::endl;
return;
}
//LPITEMIDLIST pidl;
CComHeapPtr<ITEMIDLIST> pidl;
hr = SHParseDisplayName(folderPath, NULL, &pidl, 0, NULL);
if (FAILED(hr)) {
std::wcout << L"Failed to parse display name. Error: " << hr << std::endl;
CoUninitialize();
return;
}
CComPtr<IShellFolder> pShellFolder;
hr = SHBindToObject(NULL, pidl, NULL, IID_IShellFolder, (void**)&pShellFolder);
if (FAILED(hr)) {
std::wcout << L"Failed to bind to shell folder. Error: " << hr << std::endl;
//ILFree(pidl); // Clean up PIDL
CoUninitialize();
return;
}
CSFV csfv = { 0 };
csfv.cbSize = sizeof(CSFV);
csfv.pshf = pShellFolder;
csfv.psvOuter = NULL;
csfv.pidl = NULL;
csfv.lEvents = 0;
csfv.pfnCallback = NULL;
CComPtr<IShellView> pShellView;
hr = SHCreateShellFolderViewEx(&csfv, &pShellView);
if (FAILED(hr)) {
std::wcout << L"Failed to create shell folder view. Error: " << hr << std::endl;
//ILFree(pidl); // Clean up PIDL
CoUninitialize();
return;
}
CComPtr<IColumnManager> pColumnManager;
hr = pShellView->QueryInterface(IID_PPV_ARGS(&pColumnManager));
if (SUCCEEDED(hr)) {
UINT columnCount = 0;
HRESULT hr = pColumnManager->GetColumnCount(CM_ENUM_VISIBLE, &columnCount);
if (FAILED(hr)) {
std::wcout << L"Failed to get column count. Error: " << hr << std::endl;
return;
}
//PROPERTYKEY* pColumns = new PROPERTYKEY[columnCount];
CComHeapPtr<PROPERTYKEY> pColumns;
// Allocate the memory for the columns
if (!pColumns.Allocate(columnCount)) {
std::wcout << L"Failed to allocate memory for columns." << std::endl;
return;
}
std::wcout << L"Windows File Explorer Detail View Visible Columns for " << folderPath << std::endl;
hr = pColumnManager->GetColumns(CM_ENUM_VISIBLE, pColumns, columnCount);
if (SUCCEEDED(hr)) {
for (UINT i = 0; i < columnCount; ++i) {
//LPWSTR pszCanonicalName = nullptr;
CComHeapPtr<WCHAR> pszCanonicalName;
hr = PSGetNameFromPropertyKey(pColumns[i], &pszCanonicalName);
if (SUCCEEDED(hr)) {
//std::wcout << L"Column " << i + 1 << L": " << pszCanonicalName.m_pData << std::endl;
std::wcout << L"Column " << i + 1 << L": " << static_cast<WCHAR*>(pszCanonicalName) << std::endl;
//CoTaskMemFree(pszCanonicalName);
}
else {
std::wcout << L"Failed to get column name for index " << i << L". Error: " << hr << std::endl;
}
}
}
else {
std::wcout << L"Failed to get columns. Error: " << hr << std::endl;
}
//delete[] pColumns;
}
else {
std::wcout << L"Failed to query IColumnManager interface. Error: " << hr << std::endl;
}
//ILFree(pidl);
CoUninitialize();
}
int wmain(int argc, wchar_t* argv[]) {
if (argc != 2)
{
std::wcout << L"Usage: GetColumnsCppApATL.exe <directory_path>" << std::endl;
return 1;
}
ListFileExplorerDetailsViewVisibleColumns(argv[1]);
return 0;
}