diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:08:06 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:08:06 +0200 |
commit | fbe76102e941b9f1edaf236788e42678f05fdf9a (patch) | |
tree | f5f538316019fa89be8dc478103490c3a826f3ac /shared/IFileOperation | |
parent | 3.8 (diff) | |
download | FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.gz FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.tar.bz2 FreeFileSync-fbe76102e941b9f1edaf236788e42678f05fdf9a.zip |
3.9
Diffstat (limited to 'shared/IFileOperation')
-rw-r--r-- | shared/IFileOperation/FileOperation_Vista.vcxproj | 6 | ||||
-rw-r--r-- | shared/IFileOperation/dll_main.cpp (renamed from shared/IFileOperation/dllmain.cpp) | 14 | ||||
-rw-r--r-- | shared/IFileOperation/file_op.cpp (renamed from shared/IFileOperation/fileOp.cpp) | 107 | ||||
-rw-r--r-- | shared/IFileOperation/file_op.h (renamed from shared/IFileOperation/fileOp.h) | 0 |
4 files changed, 44 insertions, 83 deletions
diff --git a/shared/IFileOperation/FileOperation_Vista.vcxproj b/shared/IFileOperation/FileOperation_Vista.vcxproj index 1cbb4a28..9f9a9c28 100644 --- a/shared/IFileOperation/FileOperation_Vista.vcxproj +++ b/shared/IFileOperation/FileOperation_Vista.vcxproj @@ -209,7 +209,7 @@ </Link> </ItemDefinitionGroup> <ItemGroup> - <ClCompile Include="dllmain.cpp"> + <ClCompile Include="dll_main.cpp"> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> </PrecompiledHeader> <CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged> @@ -223,10 +223,10 @@ </PrecompiledHeader> <CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</CompileAsManaged> </ClCompile> - <ClCompile Include="fileOp.cpp" /> + <ClCompile Include="file_op.cpp" /> </ItemGroup> <ItemGroup> - <ClInclude Include="fileOp.h" /> + <ClInclude Include="file_op.h" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> diff --git a/shared/IFileOperation/dllmain.cpp b/shared/IFileOperation/dll_main.cpp index 8dae897a..142e26a2 100644 --- a/shared/IFileOperation/dllmain.cpp +++ b/shared/IFileOperation/dll_main.cpp @@ -8,20 +8,18 @@ #define WIN32_LEAN_AND_MEAN #include <windows.h> - -BOOL APIENTRY DllMain( HMODULE hModule, - DWORD ul_reason_for_call, - LPVOID lpReserved - ) +//optional: add init/teardown logic here +BOOL APIENTRY DllMain(HINSTANCE hinstDLL, + DWORD fdwReason, + LPVOID lpvReserved) { - switch (ul_reason_for_call) + switch (fdwReason) { case DLL_PROCESS_ATTACH: + case DLL_PROCESS_DETACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: break; } return TRUE; } - diff --git a/shared/IFileOperation/fileOp.cpp b/shared/IFileOperation/file_op.cpp index 761ec06d..f37a2e66 100644 --- a/shared/IFileOperation/fileOp.cpp +++ b/shared/IFileOperation/file_op.cpp @@ -4,7 +4,9 @@ // * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de) * // ************************************************************************** // -#include "fileOp.h" +#include "file_op.h" +#include "../com_ptr.h" +#include "../com_error.h" #define WIN32_LEAN_AND_MEAN #include "windows.h" @@ -14,7 +16,7 @@ #include <algorithm> #include <string> #include <cstdio> -#include <comdef.h> +#include <comdef.h> void writeString(const std::wstring& input, wchar_t* output, size_t outputBufferLen) @@ -25,44 +27,6 @@ void writeString(const std::wstring& input, wchar_t* output, size_t outputBuffer } -std::wstring numberToHexString(long number) -{ - wchar_t result[100]; - swprintf(result, 100, L"0x%08x", number); - return std::wstring(result); -} - - -std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr) -{ - std::wstring output(input); - output += L" ("; - output += numberToHexString(hr); - output += L": "; - output += _com_error(hr).ErrorMessage(); - output += L")"; - return output; -} - - -//IShellItem resource management -template <class T> -class ReleaseAtExit -{ -public: - ReleaseAtExit(T*& item) : item_(item) {} - ~ReleaseAtExit() - { - if (item_ != NULL) - item_->Release(); - } -private: - ReleaseAtExit(const ReleaseAtExit&); - ReleaseAtExit& operator=(const ReleaseAtExit&); - T*& item_; -}; - - namespace FileOp { std::wstring lastErrorMessage; @@ -72,15 +36,16 @@ std::wstring lastErrorMessage; bool FileOp::moveToRecycleBin(const wchar_t* fileNames[], size_t fileNo) //size of fileNames array { + using Util::ComPtr; + using Util::generateErrorMsg; HRESULT hr; // Create the IFileOperation interface - IFileOperation* pfo = NULL; - ReleaseAtExit<IFileOperation> dummy(pfo); + ComPtr<IFileOperation> fileOp; hr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, - IID_PPV_ARGS(&pfo)); + IID_PPV_ARGS(fileOp.init())); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"CoCreateInstance\".", hr); @@ -91,10 +56,10 @@ bool FileOp::moveToRecycleBin(const wchar_t* fileNames[], // from being shown to the user during the // operation. This includes error, confirmation // and progress dialogs. - hr = pfo->SetOperationFlags(FOF_ALLOWUNDO | - FOF_NOCONFIRMATION | - FOF_SILENT | - FOF_NOERRORUI); + hr = fileOp->SetOperationFlags(FOF_ALLOWUNDO | + FOF_NOCONFIRMATION | + FOF_SILENT | + FOF_NOERRORUI); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"SetOperationFlags\".", hr); @@ -104,11 +69,10 @@ bool FileOp::moveToRecycleBin(const wchar_t* fileNames[], for (size_t i = 0; i < fileNo; ++i) { //create file/folder item object - IShellItem* psiFile = NULL; - ReleaseAtExit<IShellItem> dummy2(psiFile); + ComPtr<IShellItem> psiFile; hr = SHCreateItemFromParsingName(fileNames[i], NULL, - IID_PPV_ARGS(&psiFile)); + IID_PPV_ARGS(psiFile.init())); if (FAILED(hr)) { std::wstring message(L"Error calling \"SHCreateItemFromParsingName\" for file:\n"); @@ -118,7 +82,7 @@ bool FileOp::moveToRecycleBin(const wchar_t* fileNames[], return false; } - hr = pfo->DeleteItem(psiFile, NULL); + hr = fileOp->DeleteItem(psiFile.get(), NULL); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"DeleteItem\".", hr); @@ -127,7 +91,7 @@ bool FileOp::moveToRecycleBin(const wchar_t* fileNames[], } //perform actual operations - hr = pfo->PerformOperations(); + hr = fileOp->PerformOperations(); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"PerformOperations\".", hr); @@ -136,7 +100,7 @@ bool FileOp::moveToRecycleBin(const wchar_t* fileNames[], //check if errors occured: if FOFX_EARLYFAILURE is not used, PerformOperations() can return with success despite errors! BOOL pfAnyOperationsAborted = FALSE; - hr = pfo->GetAnyOperationsAborted(&pfAnyOperationsAborted); + hr = fileOp->GetAnyOperationsAborted(&pfAnyOperationsAborted); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"GetAnyOperationsAborted\".", hr); @@ -156,15 +120,17 @@ bool FileOp::moveToRecycleBin(const wchar_t* fileNames[], bool FileOp::copyFile(const wchar_t* sourceFile, const wchar_t* targetFile) { + using Util::ComPtr; + using Util::generateErrorMsg; + HRESULT hr; // Create the IFileOperation interface - IFileOperation* pfo = NULL; - ReleaseAtExit<IFileOperation> dummy(pfo); + ComPtr<IFileOperation> fileOp; hr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, - IID_PPV_ARGS(&pfo)); + IID_PPV_ARGS(fileOp.init())); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"CoCreateInstance\".", hr); @@ -175,10 +141,10 @@ bool FileOp::copyFile(const wchar_t* sourceFile, // from being shown to the user during the // operation. This includes error, confirmation // and progress dialogs. - hr = pfo->SetOperationFlags(FOF_NOCONFIRMATION | - FOF_SILENT | - FOFX_EARLYFAILURE | - FOF_NOERRORUI); + hr = fileOp->SetOperationFlags(FOF_NOCONFIRMATION | + FOF_SILENT | + FOFX_EARLYFAILURE | + FOF_NOERRORUI); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"SetOperationFlags\".", hr); @@ -186,11 +152,10 @@ bool FileOp::copyFile(const wchar_t* sourceFile, } //create source object - IShellItem* psiSourceFile = NULL; - ReleaseAtExit<IShellItem> dummy2(psiSourceFile); + ComPtr<IShellItem> psiSourceFile; hr = SHCreateItemFromParsingName(sourceFile, NULL, - IID_PPV_ARGS(&psiSourceFile)); + IID_PPV_ARGS(psiSourceFile.init())); if (FAILED(hr)) { std::wstring message(L"Error calling \"SHCreateItemFromParsingName\" for file:\n"); @@ -199,7 +164,7 @@ bool FileOp::copyFile(const wchar_t* sourceFile, return false; } - const size_t pos = std::wstring(targetFile).find_last_of(L'\\'); + const size_t pos = std::wstring(targetFile).find_last_of(L'\\'); if (pos == std::wstring::npos) { lastErrorMessage = L"Target filename does not contain a path separator."; @@ -210,11 +175,10 @@ bool FileOp::copyFile(const wchar_t* sourceFile, const std::wstring targetFileNameShort = targetFile + pos + 1; //create target folder object - IShellItem* psiTargetFolder = NULL; - ReleaseAtExit<IShellItem> dummy3(psiTargetFolder); - hr = SHCreateItemFromParsingName(targetFolder.c_str(), + ComPtr<IShellItem> psiTargetFolder; + hr = SHCreateItemFromParsingName(targetFolder.c_str(), NULL, - IID_PPV_ARGS(&psiTargetFolder)); + IID_PPV_ARGS(psiTargetFolder.init())); if (FAILED(hr)) { std::wstring message(L"Error calling \"SHCreateItemFromParsingName\" for folder:\n"); @@ -224,7 +188,7 @@ bool FileOp::copyFile(const wchar_t* sourceFile, } //schedule file copy operation - hr = pfo->CopyItem(psiSourceFile, psiTargetFolder, targetFileNameShort.c_str(), NULL); + hr = fileOp->CopyItem(psiSourceFile.get(), psiTargetFolder.get(), targetFileNameShort.c_str(), NULL); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"CopyItem\".", hr); @@ -232,7 +196,7 @@ bool FileOp::copyFile(const wchar_t* sourceFile, } //perform actual operations - hr = pfo->PerformOperations(); + hr = fileOp->PerformOperations(); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"PerformOperations\".", hr); @@ -241,14 +205,13 @@ bool FileOp::copyFile(const wchar_t* sourceFile, //check if errors occured: if FOFX_EARLYFAILURE is not used, PerformOperations() can return with success despite errors! BOOL pfAnyOperationsAborted = FALSE; - hr = pfo->GetAnyOperationsAborted(&pfAnyOperationsAborted); + hr = fileOp->GetAnyOperationsAborted(&pfAnyOperationsAborted); if (FAILED(hr)) { lastErrorMessage = generateErrorMsg(L"Error calling \"GetAnyOperationsAborted\".", hr); return false; } - if (pfAnyOperationsAborted == TRUE) { lastErrorMessage = L"Operation did not complete successfully."; diff --git a/shared/IFileOperation/fileOp.h b/shared/IFileOperation/file_op.h index 8fa6a75b..8fa6a75b 100644 --- a/shared/IFileOperation/fileOp.h +++ b/shared/IFileOperation/file_op.h |