From 014f7fe82a7946d73254291acc45cb9f004aea9d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Apr 2021 20:30:07 +0530 Subject: [PATCH] Use a smart pointer for portable device manager --- src/calibre/devices/mtp/windows/global.h | 4 ++-- src/calibre/devices/mtp/windows/wpd.cpp | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/calibre/devices/mtp/windows/global.h b/src/calibre/devices/mtp/windows/global.h index 8d9de83d5f..88d51f5dcb 100644 --- a/src/calibre/devices/mtp/windows/global.h +++ b/src/calibre/devices/mtp/windows/global.h @@ -17,7 +17,7 @@ #include "../../../utils/windows/common.h" #define ENSURE_WPD(retval) \ - if (portable_device_manager == NULL) { PyErr_SetString(NoWPD, "No WPD service available."); return retval; } + if (!portable_device_manager) { PyErr_SetString(NoWPD, "No WPD service available."); return retval; } namespace wpd { @@ -25,7 +25,7 @@ namespace wpd { extern PyObject *WPDError, *NoWPD, *WPDFileBusy; // The global device manager -extern IPortableDeviceManager *portable_device_manager; +extern CComPtr portable_device_manager; // Application info typedef struct { diff --git a/src/calibre/devices/mtp/windows/wpd.cpp b/src/calibre/devices/mtp/windows/wpd.cpp index b34082ce8a..9edd9d4110 100644 --- a/src/calibre/devices/mtp/windows/wpd.cpp +++ b/src/calibre/devices/mtp/windows/wpd.cpp @@ -13,7 +13,7 @@ using namespace wpd; PyObject *wpd::WPDError = NULL, *wpd::NoWPD = NULL, *wpd::WPDFileBusy = NULL; // The global device manager -IPortableDeviceManager *wpd::portable_device_manager = NULL; +CComPtr wpd::portable_device_manager = NULL; // Flag indicating if COM has been initialized static int _com_initialized = 0; @@ -34,14 +34,13 @@ wpd_init(PyObject *self, PyObject *args) { else {PyErr_SetString(WPDError, "Failed to initialize COM"); return NULL;} } - if (portable_device_manager == NULL) { + if (!portable_device_manager) { Py_BEGIN_ALLOW_THREADS; - hr = CoCreateInstance(CLSID_PortableDeviceManager, NULL, - CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&portable_device_manager)); + hr = portable_device_manager.CoCreateInstance(CLSID_PortableDeviceManager, NULL, CLSCTX_INPROC_SERVER); Py_END_ALLOW_THREADS; if (FAILED(hr)) { - portable_device_manager = NULL; + portable_device_manager.Release(); PyErr_SetString((hr == REGDB_E_CLASSNOTREG) ? NoWPD : WPDError, (hr == REGDB_E_CLASSNOTREG) ? "This computer is not running the Windows Portable Device framework. You may need to install Windows Media Player 11 or newer." : "Failed to create the WPD device manager interface"); @@ -54,11 +53,10 @@ wpd_init(PyObject *self, PyObject *args) { static PyObject * wpd_uninit(PyObject *self, PyObject *args) { - if (portable_device_manager != NULL) { + if (portable_device_manager) { Py_BEGIN_ALLOW_THREADS; - portable_device_manager->Release(); + portable_device_manager.Release(); Py_END_ALLOW_THREADS; - portable_device_manager = NULL; } if (_com_initialized) {