K
Kirillenseer
Guest
I'm trying to zip a couple files using Windows. And it doesn't work. First, the code
HRESULT ShellZIP(wstring In1, wstring In2, wstring Out)
{
if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(In1.c_str()))
return HRESULT_FROM_WIN32(GetLastError());
if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(In2.c_str()))
return HRESULT_FROM_WIN32(GetLastError());
BYTE Bootstrap[] = { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
auto Handle = CreateFileW(Out.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (INVALID_HANDLE_VALUE == Handle)
return HRESULT_FROM_WIN32(GetLastError());
DWORD BytesWritten;
auto Result = WriteFile(Handle, Bootstrap, sizeof(Bootstrap), &BytesWritten, nullptr);
CloseHandle(Handle);
if (!Result)
return HRESULT_FROM_WIN32(GetLastError());
HRESULT Result;
_variant_t Source1(In1.c_str());
_variant_t Source2(In2.c_str());
_variant_t Destination(Out.c_str());
_variant_t Options(FOF_NO_UI);
Folder* PDestination = nullptr;
IShellDispatch* API = nullptr;
DWORD Delay = 1000;
Result = CoInitialize(nullptr);
if (S_OK != Result)
return Result;
Result = CoCreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER, IID_IShellDispatch, reinterpret_cast<void**>(&API));
if (S_OK != Result)
goto Cleanup;
Result = API->NameSpace(Destination, &PDestination);
if (S_OK != Result)
goto Cleanup;
PDestination->CopyHere(Source1, Options);
Sleep(Delay);
PDestination->CopyHere(Source2, Options);
Sleep(Delay);
Cleanup:
if (nullptr != PDestination)
PDestination->Release();
if (nullptr != API)
API->Release();
CoUninitialize();
return Result;
}
The CopyHere-calls fail unless I'm running elevated. Then it works. Is there something I'm missing? Does CopyHere really need administrative privileges or did corporate IT screw up here? And in the latter case, any idea what policy may be responsible?
Continue reading...
HRESULT ShellZIP(wstring In1, wstring In2, wstring Out)
{
if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(In1.c_str()))
return HRESULT_FROM_WIN32(GetLastError());
if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(In2.c_str()))
return HRESULT_FROM_WIN32(GetLastError());
BYTE Bootstrap[] = { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
auto Handle = CreateFileW(Out.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (INVALID_HANDLE_VALUE == Handle)
return HRESULT_FROM_WIN32(GetLastError());
DWORD BytesWritten;
auto Result = WriteFile(Handle, Bootstrap, sizeof(Bootstrap), &BytesWritten, nullptr);
CloseHandle(Handle);
if (!Result)
return HRESULT_FROM_WIN32(GetLastError());
HRESULT Result;
_variant_t Source1(In1.c_str());
_variant_t Source2(In2.c_str());
_variant_t Destination(Out.c_str());
_variant_t Options(FOF_NO_UI);
Folder* PDestination = nullptr;
IShellDispatch* API = nullptr;
DWORD Delay = 1000;
Result = CoInitialize(nullptr);
if (S_OK != Result)
return Result;
Result = CoCreateInstance(CLSID_Shell, nullptr, CLSCTX_INPROC_SERVER, IID_IShellDispatch, reinterpret_cast<void**>(&API));
if (S_OK != Result)
goto Cleanup;
Result = API->NameSpace(Destination, &PDestination);
if (S_OK != Result)
goto Cleanup;
PDestination->CopyHere(Source1, Options);
Sleep(Delay);
PDestination->CopyHere(Source2, Options);
Sleep(Delay);
Cleanup:
if (nullptr != PDestination)
PDestination->Release();
if (nullptr != API)
API->Release();
CoUninitialize();
return Result;
}
The CopyHere-calls fail unless I'm running elevated. Then it works. Is there something I'm missing? Does CopyHere really need administrative privileges or did corporate IT screw up here? And in the latter case, any idea what policy may be responsible?
Continue reading...