-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_list_formats.cpp
More file actions
71 lines (67 loc) · 3.52 KB
/
Copy pathtest_list_formats.cpp
File metadata and controls
71 lines (67 loc) · 3.52 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
#include <iostream>
#include <string>
#include <vector>
#include "7zip/Archive/IArchive.h"
#include "Windows/PropVariant.h"
extern "C" __declspec(dllimport) HRESULT __stdcall GetNumberOfFormats(UInt32 *numFormats);
extern "C" __declspec(dllimport) HRESULT __stdcall GetHandlerProperty2(UInt32 index, PROPID propID, PROPVARIANT *value);
extern "C" __declspec(dllimport) HRESULT __stdcall CreateArchiver(const GUID *clsid, const GUID *iid, void **outObject);
#include "7zip/Common/FileStreams.h"
int wmain(int argc, wchar_t** wargv) {
UInt32 n = 0;
HRESULT hr = GetNumberOfFormats(&n);
std::cout << "GetNumberOfFormats hr=" << std::hex << hr << std::dec << ", n=" << n << "\n";
for (UInt32 i = 0; i < n; ++i) {
NWindows::NCOM::CPropVariant prop;
if (GetHandlerProperty2(i, NArchive::NHandlerPropID::kName, &prop) == S_OK) {
if (prop.vt == VT_BSTR && prop.bstrVal) {
std::wcout << L"[" << i << L"] " << std::wstring(prop.bstrVal, prop.bstrVal + SysStringLen(prop.bstrVal)) << L"\n";
}
}
NWindows::NCOM::PropVariant_Clear(&prop);
}
if (argc >= 2) {
// try open as UDF (preferred) then ISO by name match
GUID isoClsid{}; bool foundIso=false;
GUID udfClsid{}; bool foundUdf=false;
for (UInt32 i = 0; i < n; ++i) {
NWindows::NCOM::CPropVariant prop;
if (GetHandlerProperty2(i, NArchive::NHandlerPropID::kName, &prop) == S_OK && prop.vt == VT_BSTR && prop.bstrVal) {
std::wstring name(prop.bstrVal, prop.bstrVal + SysStringLen(prop.bstrVal));
if (name == L"Udf") {
NWindows::NCOM::PropVariant_Clear(&prop);
if (GetHandlerProperty2(i, NArchive::NHandlerPropID::kClassID, &prop) == S_OK && prop.vt == VT_BSTR && prop.bstrVal && SysStringByteLen(prop.bstrVal)==sizeof(GUID)) {
memcpy(&udfClsid, prop.bstrVal, sizeof(GUID));
foundUdf = true;
}
}
if (name == L"Iso") {
NWindows::NCOM::PropVariant_Clear(&prop);
if (GetHandlerProperty2(i, NArchive::NHandlerPropID::kClassID, &prop) == S_OK && prop.vt == VT_BSTR && prop.bstrVal && SysStringByteLen(prop.bstrVal)==sizeof(GUID)) {
memcpy(&isoClsid, prop.bstrVal, sizeof(GUID));
foundIso = true;
}
}
}
NWindows::NCOM::PropVariant_Clear(&prop);
}
const GUID *toTry[2] = { foundUdf ? &udfClsid : nullptr, foundIso ? &isoClsid : nullptr };
for (int k=0;k<2;k++) if (toTry[k]) {
CMyComPtr<IInArchive> arc;
if (CreateArchiver(toTry[k], &IID_IInArchive, (void**)&arc) == S_OK && arc) {
CInFileStream *fs = new CInFileStream();
CMyComPtr<IInStream> in = fs;
if (fs->Open(wargv[1])) {
const UInt64 kMaxCheckStartPosition = 1 << 20;
HRESULT hr2 = arc->Open(in, &kMaxCheckStartPosition, nullptr);
std::wcout << L"Open() hr=" << std::hex << hr2 << std::dec << L"\n";
UInt32 numItems=0; HRESULT hr3 = arc->GetNumberOfItems(&numItems);
std::wcout << L"GetNumberOfItems hr=" << std::hex << hr3 << std::dec << L" count=" << numItems << L"\n";
} else {
std::wcout << L"Failed to open file: " << wargv[1] << L"\n";
}
}
}
}
return 0;
}