From 544fb33475eb7ea1735f9e19f78dcf3c5e1ceb66 Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Tue, 29 Dec 2015 19:59:31 +0000 Subject: Toaster: memleaks fix, code clenup git-svn-id: http://svn.miranda-ng.org/main/trunk@15967 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Toaster/src/add_to_start_menu.cpp | 56 --------------------- plugins/Toaster/src/add_to_start_menu.h | 7 --- plugins/Toaster/src/images.h | 65 ------------------------ plugins/Toaster/src/services.cpp | 1 - plugins/Toaster/src/stdafx.h | 19 ++++--- plugins/Toaster/src/string_reference_wrapper.h | 30 +++-------- plugins/Toaster/src/toast_notification.cpp | 3 +- plugins/Toaster/src/toast_notification.h | 8 --- plugins/Toaster/src/utils.cpp | 61 ++++++++++++++++++++++ plugins/Toaster/src/utils.h | 70 ++++++++++++++++++++++++++ 10 files changed, 148 insertions(+), 172 deletions(-) delete mode 100644 plugins/Toaster/src/add_to_start_menu.cpp delete mode 100644 plugins/Toaster/src/add_to_start_menu.h delete mode 100644 plugins/Toaster/src/images.h create mode 100644 plugins/Toaster/src/utils.cpp create mode 100644 plugins/Toaster/src/utils.h diff --git a/plugins/Toaster/src/add_to_start_menu.cpp b/plugins/Toaster/src/add_to_start_menu.cpp deleted file mode 100644 index 612ac322d7..0000000000 --- a/plugins/Toaster/src/add_to_start_menu.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "stdafx.h" - -#define SHORTCUT_PATH "\\Microsoft\\Windows\\Start Menu\\Programs\\Miranda NG.lnk" - -using namespace Microsoft::WRL; - -wchar_t* GetShortcutPath() -{ - wchar_t path[MAX_PATH]; - GetEnvironmentVariable(_T("APPDATA"), path, MAX_PATH); - wcscat_s(path, _T(SHORTCUT_PATH)); - return mir_wstrdup(path); -} - -HRESULT InstallShortcut(_In_z_ wchar_t *shortcutPath) -{ - wchar_t exePath[MAX_PATH]; - GetModuleFileName(NULL, exePath, MAX_PATH); - - ComPtr shellLink; - CHECKHR(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink))); - - CHECKHR(shellLink->SetPath(exePath)); - - CHECKHR(shellLink->SetArguments(L"")); - - ComPtr propertyStore; - CHECKHR(shellLink.As(&propertyStore)); - - PROPVARIANT appIdPropVar; - HRESULT hr = InitPropVariantFromString(AppUserModelID, &appIdPropVar); - if (SUCCEEDED(hr)) - { - CHECKHR(propertyStore->SetValue(PKEY_AppUserModel_ID, appIdPropVar)); - - CHECKHR(propertyStore->Commit()); - - ComPtr persistFile; - CHECKHR(hr = shellLink.As(&persistFile)) - - hr = persistFile->Save(shortcutPath, TRUE); - } - PropVariantClear(&appIdPropVar); - - return hr; -} - -bool ShortcutExists() -{ - return (GetFileAttributes(ptrW(GetShortcutPath())) < 0xFFFFFFF); -} - -HRESULT TryCreateShortcut() -{ - return (ShortcutExists() ? S_OK : InstallShortcut(ptrW(GetShortcutPath()))); -} diff --git a/plugins/Toaster/src/add_to_start_menu.h b/plugins/Toaster/src/add_to_start_menu.h deleted file mode 100644 index 45d866c0fd..0000000000 --- a/plugins/Toaster/src/add_to_start_menu.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef _ADD_TOSTART_MENU_H_ -#define _ADD_TOSTART_MENU_H_ - -DEFINE_PROPERTYKEY(PKEY_AppUserModel_ID, 0x9F4C2855, 0x9F79, 0x4B39, 0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3, 5); -HRESULT TryCreateShortcut(); - -#endif //_ADD_TOSTART_MENU_H_ \ No newline at end of file diff --git a/plugins/Toaster/src/images.h b/plugins/Toaster/src/images.h deleted file mode 100644 index 01b05720d1..0000000000 --- a/plugins/Toaster/src/images.h +++ /dev/null @@ -1,65 +0,0 @@ -extern wchar_t wszTempDir[]; - -class ToasterImage -{ - HBITMAP _hBitmap; - ptrT tszId; - -public: - __inline explicit ToasterImage(HICON hIcon) : _hBitmap(NULL), tszId(CMString(FORMAT, _T("%p"), hIcon).Detach()) - { - ICONINFO icon = { 0 }; - if (GetIconInfo(hIcon, &icon)) - { - _hBitmap = icon.hbmColor; - DeleteObject(icon.hbmMask); - } - } - - __inline explicit ToasterImage(HBITMAP bmp) : _hBitmap(bmp), tszId(CMString(FORMAT, _T("%p"), bmp).Detach()) - { - } - - __inline explicit ToasterImage(const char *szProto) : _hBitmap(NULL), tszId(mir_a2t(szProto)) - { - ICONINFO icon = { 0 }; - if (GetIconInfo(Skin_LoadProtoIcon(szProto, ID_STATUS_ONLINE, 1), &icon)) - { - _hBitmap = icon.hbmColor; - DeleteObject(icon.hbmMask); - } - } - - __inline ~ToasterImage() - { - DeleteObject(_hBitmap); - } - - wchar_t* Save() - { - wchar_t wszSavePath[MAX_PATH]; - mir_snwprintf(wszSavePath, L"%s\\MirandaToaster.%s.png", wszTempDir, tszId); - - if (!(GetFileAttributes(wszSavePath) < 0xFFFFFFF)) - { - IMGSRVC_INFO isi = { sizeof(isi) }; - isi.wszName = mir_wstrdup(wszSavePath); - isi.hbm = _hBitmap; - isi.dwMask = IMGI_HBITMAP; - isi.fif = FREE_IMAGE_FORMAT::FIF_PNG; - __try - { - CallService(MS_IMG_SAVE, (WPARAM)&isi, IMGL_WCHAR); - } - __except (EXCEPTION_EXECUTE_HANDLER) - { - return nullptr; - } - } - return mir_wstrdup(wszSavePath); - } - - __inline operator wchar_t*() - { return Save(); - } -}; \ No newline at end of file diff --git a/plugins/Toaster/src/services.cpp b/plugins/Toaster/src/services.cpp index 9342af8672..f217367509 100644 --- a/plugins/Toaster/src/services.cpp +++ b/plugins/Toaster/src/services.cpp @@ -165,7 +165,6 @@ static INT_PTR CreateClassPopup(WPARAM, LPARAM lParam) static INT_PTR UnRegisterClass(WPARAM, LPARAM lParam) { - for (auto it = mp_Classes.begin(); it != mp_Classes.end(); it++) { if (it->second == (void*)lParam) diff --git a/plugins/Toaster/src/stdafx.h b/plugins/Toaster/src/stdafx.h index 3faa59fec7..5b4df4701c 100644 --- a/plugins/Toaster/src/stdafx.h +++ b/plugins/Toaster/src/stdafx.h @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -27,22 +26,22 @@ #include "version.h" #include "resource.h" -const wchar_t AppUserModelID[] = _T("MirandaNG"); +const wchar_t AppUserModelID[] = L"MirandaNG"; #define MODULE "Toaster" #define CHECKHR(x) if (FAILED(x)) return x; -#include "string_reference_wrapper.h" -#include "toast_notification.h" -#include "add_to_start_menu.h" -#include "options.h" -#include "structs.h" -#include "images.h" - +class ToastNotification; extern HINSTANCE g_hInstance; extern mir_cs csNotifications; -extern OBJLIST lstNotifications; extern wchar_t wszTempDir[MAX_PATH]; +extern OBJLIST lstNotifications; + +#include "string_reference_wrapper.h" +#include "utils.h" +#include "options.h" +#include "structs.h" +#include "toast_notification.h" void CleanupClasses(); void InitServices(); diff --git a/plugins/Toaster/src/string_reference_wrapper.h b/plugins/Toaster/src/string_reference_wrapper.h index 6daeb0fee1..addfb32394 100644 --- a/plugins/Toaster/src/string_reference_wrapper.h +++ b/plugins/Toaster/src/string_reference_wrapper.h @@ -1,47 +1,31 @@ -#ifndef _STRING_WRAPPER_H_ -#define _STRING_WRAPPER_H_ +#pragma once class StringReferenceWrapper { public: StringReferenceWrapper(_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw() - { - HRESULT hr = WindowsCreateStringReference(stringRef, length, &_header, &_hstring); - - if (FAILED(hr)) - RaiseException(static_cast(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr); + { WindowsCreateStringReference(stringRef, length, &_header, &_hstring); } template StringReferenceWrapper(_In_reads_(N) wchar_t const (&stringRef)[N]) throw() - { - UINT32 length = N - 1; - HRESULT hr = WindowsCreateStringReference(stringRef, length, &_header, &_hstring); - - if (FAILED(hr)) - RaiseException(static_cast(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr); + { WindowsCreateStringReference(stringRef, (UINT32)(N - 1), &_header, &_hstring); } template StringReferenceWrapper(_In_reads_(_) wchar_t(&stringRef)[_]) throw() - { - size_t length = mir_wstrlen(stringRef); - WindowsCreateStringReference(stringRef, length, &_header, &_hstring); + { WindowsCreateStringReference(stringRef, mir_wstrlen(stringRef), &_header, &_hstring); } ~StringReferenceWrapper() - { - WindowsDeleteString(_hstring); + { WindowsDeleteString(_hstring); } HSTRING Get() const throw() - { - return _hstring; + { return _hstring; } private: HSTRING _hstring; HSTRING_HEADER _header; -}; - -#endif //_STRING_WRAPPER_H_ \ No newline at end of file +}; \ No newline at end of file diff --git a/plugins/Toaster/src/toast_notification.cpp b/plugins/Toaster/src/toast_notification.cpp index 1d1c73aed4..70a2872396 100644 --- a/plugins/Toaster/src/toast_notification.cpp +++ b/plugins/Toaster/src/toast_notification.cpp @@ -23,7 +23,6 @@ ToastNotification::ToastNotification( notification->add_Dismissed(Callback(this, &ToastNotification::OnDismiss).Get(), &_ertDismissed); notification->add_Failed(Callback(this, &ToastNotification::OnFail).Get(), &_ertFailed); notifier->Show(notification.Get()); - } ToastNotification::~ToastNotification() @@ -62,7 +61,7 @@ HRESULT ToastNotification::CreateXml(_Outptr_ ABI::Windows::Data::Xml::Dom::IXml } int nLength; - TCHAR *xtmp = xmlToString(xmlToast, &nLength); + ptrT xtmp(xmlToString(xmlToast, &nLength)); xmlDestroyNode(xmlToast); CHECKHR(xmlDocument->LoadXml(StringReferenceWrapper(xtmp, nLength).Get())); diff --git a/plugins/Toaster/src/toast_notification.h b/plugins/Toaster/src/toast_notification.h index 757cf269d5..2320cbce99 100644 --- a/plugins/Toaster/src/toast_notification.h +++ b/plugins/Toaster/src/toast_notification.h @@ -1,17 +1,9 @@ #pragma once -typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastActivatedEventHandler; -typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastDismissedEventHandler; -typedef ABI::Windows::Foundation::ITypedEventHandler DesktopToastFailedEventHandler; typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_IInspectable ToastActivationHandler; typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastDismissedEventArgs ToastDismissHandler; typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastFailedEventArgs ToastFailHandler; -class ToastNotification; - -extern OBJLIST lstNotifications; -extern mir_cs csNotifications; - class ToastNotification { private: diff --git a/plugins/Toaster/src/utils.cpp b/plugins/Toaster/src/utils.cpp new file mode 100644 index 0000000000..86062d2f4a --- /dev/null +++ b/plugins/Toaster/src/utils.cpp @@ -0,0 +1,61 @@ +#include "stdafx.h" + +#define SHORTCUT_PATH "\\Microsoft\\Windows\\Start Menu\\Programs\\Miranda NG.lnk" + +using namespace Microsoft::WRL; + +wchar_t* GetShortcutPath() +{ + wchar_t path[MAX_PATH]; + GetEnvironmentVariable(_T("APPDATA"), path, MAX_PATH); + wcscat_s(path, _T(SHORTCUT_PATH)); + return mir_wstrdup(path); +} + +HRESULT InstallShortcut(_In_z_ wchar_t *shortcutPath) +{ + wchar_t exePath[MAX_PATH]; + GetModuleFileName(NULL, exePath, MAX_PATH); + + ComPtr shellLink; + CHECKHR(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink))); + + CHECKHR(shellLink->SetPath(exePath)); + + CHECKHR(shellLink->SetArguments(L"")); + + ComPtr propertyStore; + CHECKHR(shellLink.As(&propertyStore)); + + PROPVARIANT appIdPropVar; + HRESULT hr = InitPropVariantFromString(AppUserModelID, &appIdPropVar); + if (SUCCEEDED(hr)) + { + hr = propertyStore->SetValue(PKEY_AppUserModel_ID, appIdPropVar); + if (SUCCEEDED(hr)) + { + hr = propertyStore->Commit(); + if (SUCCEEDED(hr)) + { + ComPtr persistFile; + hr = shellLink.As(&persistFile); + if (SUCCEEDED(hr)) + { + hr = persistFile->Save(shortcutPath, TRUE); + } + } + } + PropVariantClear(&appIdPropVar); + } + return hr; +} + +bool ShortcutExists() +{ + return (GetFileAttributes(ptrW(GetShortcutPath())) < 0xFFFFFFF); +} + +HRESULT TryCreateShortcut() +{ + return (ShortcutExists() ? S_OK : InstallShortcut(ptrW(GetShortcutPath()))); +} diff --git a/plugins/Toaster/src/utils.h b/plugins/Toaster/src/utils.h new file mode 100644 index 0000000000..858d04f1be --- /dev/null +++ b/plugins/Toaster/src/utils.h @@ -0,0 +1,70 @@ +#pragma once + +DEFINE_PROPERTYKEY(PKEY_AppUserModel_ID, 0x9F4C2855, 0x9F79, 0x4B39, 0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3, 5); +HRESULT TryCreateShortcut(); + +extern wchar_t wszTempDir[]; + +class ToasterImage +{ + HBITMAP _hBitmap; + ptrT tszId; + +public: + __inline explicit ToasterImage(HICON hIcon) : _hBitmap(NULL), tszId(CMString(FORMAT, _T("%p"), hIcon).Detach()) + { + ICONINFO icon = { 0 }; + if (GetIconInfo(hIcon, &icon)) + { + _hBitmap = icon.hbmColor; + DeleteObject(icon.hbmMask); + } + } + + __inline explicit ToasterImage(HBITMAP bmp) : _hBitmap(bmp), tszId(CMString(FORMAT, _T("%p"), bmp).Detach()) + { + } + + __inline explicit ToasterImage(const char *szProto) : _hBitmap(NULL), tszId(mir_a2t(szProto)) + { + ICONINFO icon = { 0 }; + if (GetIconInfo(Skin_LoadProtoIcon(szProto, ID_STATUS_ONLINE, 1), &icon)) + { + _hBitmap = icon.hbmColor; + DeleteObject(icon.hbmMask); + } + } + + __inline ~ToasterImage() + { + DeleteObject(_hBitmap); + } + + wchar_t* Save() + { + wchar_t wszSavePath[MAX_PATH]; + mir_snwprintf(wszSavePath, L"%s\\MirandaToaster.%s.png", wszTempDir, tszId); + + if (!(GetFileAttributes(wszSavePath) < 0xFFFFFFF)) + { + IMGSRVC_INFO isi = { sizeof(isi) }; + isi.wszName = mir_wstrdup(wszSavePath); + isi.hbm = _hBitmap; + isi.dwMask = IMGI_HBITMAP; + isi.fif = FREE_IMAGE_FORMAT::FIF_PNG; + __try + { + CallService(MS_IMG_SAVE, (WPARAM)&isi, IMGL_WCHAR); + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + return nullptr; + } + } + return mir_wstrdup(wszSavePath); + } + + __inline operator wchar_t*() + { return Save(); + } +}; \ No newline at end of file -- cgit v1.2.3