diff options
Diffstat (limited to 'shared/Taskbar_Seven')
-rw-r--r-- | shared/Taskbar_Seven/taskbar.cpp | 112 | ||||
-rw-r--r-- | shared/Taskbar_Seven/taskbar.h | 27 |
2 files changed, 49 insertions, 90 deletions
diff --git a/shared/Taskbar_Seven/taskbar.cpp b/shared/Taskbar_Seven/taskbar.cpp index c9a2e7df..d1b1638c 100644 --- a/shared/Taskbar_Seven/taskbar.cpp +++ b/shared/Taskbar_Seven/taskbar.cpp @@ -11,69 +11,41 @@ #include <ShObjIdl.h> #include <map> -#include <string> +#include <string> #include <comdef.h> #include "../com_error.h" +#include "../com_ptr.h" +#include "../c_dll.h" -namespace -{ -void writeString(const std::wstring& input, wchar_t* output, size_t outputBufferLen) -{ - const size_t newSize = min(input.length() + 1, outputBufferLen); //including null-termination - memcpy(output, input.c_str(), newSize * sizeof(wchar_t)); - output[newSize-1] = 0; //if output buffer is too small... -} +using namespace util; +using namespace c_dll; -using Util::generateErrorMsg; -using TaskbarSeven::TBHandle; -typedef std::map<TBHandle, ITaskbarList3*> TaskBarHandleMap; - -TaskbarSeven::TBHandle generateHandle() +namespace { - static TBHandle handle = 0; - return ++handle; //don't return 0! 0 is reserved for indicating failure -} - -TaskBarHandleMap taskBarHandles; - std::wstring lastErrorMessage; -} -//################################################################################################## -TaskbarSeven::TBHandle TaskbarSeven::init() //call on app initializaiton; returns handle +ComPtr<ITaskbarList3> getInstance() { - ITaskbarList3* pto = NULL; - HRESULT hr = CoCreateInstance(CLSID_TaskbarList, - NULL, - CLSCTX_ALL, - IID_PPV_ARGS(&pto)); - if (FAILED(hr)) + static ComPtr<ITaskbarList3> taskbarlist; + if (!taskbarlist) { - lastErrorMessage = generateErrorMsg(L"Error calling \"CoCreateInstance\".", hr); - return 0; + HRESULT hr = CoCreateInstance(CLSID_TaskbarList, + NULL, + CLSCTX_ALL, + IID_PPV_ARGS(taskbarlist.init())); + if (FAILED(hr)) + lastErrorMessage = generateErrorMsg(L"Error calling \"CoCreateInstance\".", hr); } - TBHandle newHandle = ::generateHandle(); - taskBarHandles[newHandle] = pto; - return newHandle; + return taskbarlist; } - -void TaskbarSeven::release(TBHandle handle) //release handle on app exit -{ - TaskBarHandleMap::const_iterator iter = taskBarHandles.find(handle); - if (iter != taskBarHandles.end()) - { - if (iter->second != NULL) - iter->second->Release(); - taskBarHandles.erase(iter); - } } +//################################################################################################## -bool TaskbarSeven::setStatus(TBHandle handle, - void* hwnd, //HWND: window assciated to the taskbar icon +bool tbseven::setStatus(void* hwnd, //HWND: window assciated to the taskbar icon TaskBarStatus status) { TBPFLAG flag = TBPF_NORMAL; @@ -96,45 +68,45 @@ bool TaskbarSeven::setStatus(TBHandle handle, break; } - ITaskbarList3* pto = taskBarHandles[handle]; - if (pto) + ComPtr<ITaskbarList3> taskbarlist = getInstance(); + if (!taskbarlist) //error msg already set + return false; + + HRESULT hr = taskbarlist->SetProgressState(static_cast<HWND>(hwnd), //[in] HWND hwnd, + flag); //[in] TBPFLAG tbpFlags + if (FAILED(hr)) { - HRESULT hr = pto->SetProgressState(static_cast<HWND>(hwnd), //[in] HWND hwnd, - flag); //[in] TBPFLAG tbpFlags - if (FAILED(hr)) - { - lastErrorMessage = generateErrorMsg(L"Error calling \"SetProgressState\".", hr); - return false; - } + lastErrorMessage = generateErrorMsg(L"Error calling \"SetProgressState\".", hr); + return false; } + return true; } -bool TaskbarSeven::setProgress(TBHandle handle, - void* hwnd, //HWND: window assciated to the taskbar icon +bool tbseven::setProgress(void* hwnd, //HWND: window assciated to the taskbar icon size_t current, size_t total) { - ITaskbarList3* pto = taskBarHandles[handle]; - - if (pto) + ComPtr<ITaskbarList3> taskbarlist = getInstance(); + if (!taskbarlist) //error msg already set + return false; + + HRESULT hr = taskbarlist->SetProgressValue( + static_cast<HWND>(hwnd), //[in] HWND hwnd, + current, //[in] ULONGLONG ullCompleted, + total); //[in] ULONGLONG ullTotal + if (FAILED(hr)) { - HRESULT hr = pto->SetProgressValue( - static_cast<HWND>(hwnd), //[in] HWND hwnd, - current, //[in] ULONGLONG ullCompleted, - total); //[in] ULONGLONG ullTotal - if (FAILED(hr)) - { - lastErrorMessage = generateErrorMsg(L"Error calling \"SetProgressValue\".", hr); - return false; - } + lastErrorMessage = generateErrorMsg(L"Error calling \"SetProgressValue\".", hr); + return false; } + return true; } -void TaskbarSeven::getLastError(wchar_t* errorMessage, size_t errorBufferLen) +void tbseven::getLastError(wchar_t* errorMessage, size_t errorBufferLen) { writeString(lastErrorMessage, errorMessage, errorBufferLen); } diff --git a/shared/Taskbar_Seven/taskbar.h b/shared/Taskbar_Seven/taskbar.h index 3b7abc51..34e122c0 100644 --- a/shared/Taskbar_Seven/taskbar.h +++ b/shared/Taskbar_Seven/taskbar.h @@ -14,7 +14,7 @@ #endif -namespace TaskbarSeven +namespace tbseven { enum TaskBarStatus { @@ -25,25 +25,16 @@ enum TaskBarStatus STATUS_PAUSED }; -typedef size_t TBHandle; - //COM needs to be initialized before calling any of these functions! CoInitializeEx/CoUninitialize -DLL_FUNCTION_DECLARATION -TBHandle init(); //returns handle; 0 on failure - -DLL_FUNCTION_DECLARATION -void release(TBHandle handle); //release taskbar handle DLL_FUNCTION_DECLARATION -bool setStatus(TBHandle handle, - void* hwnd, //HWND: window assciated to the taskbar icon +bool setStatus(void* hwnd, //HWND: window assciated to the taskbar icon TaskBarStatus status); DLL_FUNCTION_DECLARATION -bool setProgress(TBHandle handle, - void* hwnd, //HWND: window assciated to the taskbar icon +bool setProgress(void* hwnd, //HWND: window assciated to the taskbar icon size_t current, size_t total); @@ -53,18 +44,14 @@ void getLastError(wchar_t* errorMessage, size_t errorBufferLen); //function typedefs -typedef TBHandle (*initFct)(); -typedef void (*releaseFct)(TBHandle handle); -typedef bool (*setStatusFct)(TBHandle handle, void* hwnd, TaskBarStatus status); -typedef bool (*setProgressFct)(TBHandle handle, void* hwnd, size_t current, size_t total); -typedef void (*getLastErrorFct)(wchar_t* errorMessage, size_t errorBufferLen); +typedef bool (*SetStatusFct)(void* hwnd, TaskBarStatus status); +typedef bool (*SetProgressFct)(void* hwnd, size_t current, size_t total); +typedef void (*GetLastErrorFct)(wchar_t* errorMessage, size_t errorBufferLen); //function names (use const pointers to ensure internal linkage) -const char* const initFctName = "init"; -const char* const releaseFctName = "release"; const char* const setStatusFctName = "setStatus"; const char* const setProgressFctName = "setProgress"; const char* const getLastErrorFctName = "getLastError"; } -#endif //TASKBAR_SEVEN_DLL_H
\ No newline at end of file +#endif //TASKBAR_SEVEN_DLL_H |