Use a smart pointer for portable device manager

This commit is contained in:
Kovid Goyal 2021-04-20 20:30:07 +05:30
parent e1a133c990
commit 014f7fe82a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 8 additions and 10 deletions

View File

@ -17,7 +17,7 @@
#include "../../../utils/windows/common.h" #include "../../../utils/windows/common.h"
#define ENSURE_WPD(retval) \ #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 { namespace wpd {
@ -25,7 +25,7 @@ namespace wpd {
extern PyObject *WPDError, *NoWPD, *WPDFileBusy; extern PyObject *WPDError, *NoWPD, *WPDFileBusy;
// The global device manager // The global device manager
extern IPortableDeviceManager *portable_device_manager; extern CComPtr<IPortableDeviceManager> portable_device_manager;
// Application info // Application info
typedef struct { typedef struct {

View File

@ -13,7 +13,7 @@ using namespace wpd;
PyObject *wpd::WPDError = NULL, *wpd::NoWPD = NULL, *wpd::WPDFileBusy = NULL; PyObject *wpd::WPDError = NULL, *wpd::NoWPD = NULL, *wpd::WPDFileBusy = NULL;
// The global device manager // The global device manager
IPortableDeviceManager *wpd::portable_device_manager = NULL; CComPtr<IPortableDeviceManager> wpd::portable_device_manager = NULL;
// Flag indicating if COM has been initialized // Flag indicating if COM has been initialized
static int _com_initialized = 0; 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;} else {PyErr_SetString(WPDError, "Failed to initialize COM"); return NULL;}
} }
if (portable_device_manager == NULL) { if (!portable_device_manager) {
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
hr = CoCreateInstance(CLSID_PortableDeviceManager, NULL, hr = portable_device_manager.CoCreateInstance(CLSID_PortableDeviceManager, NULL, CLSCTX_INPROC_SERVER);
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&portable_device_manager));
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
if (FAILED(hr)) { if (FAILED(hr)) {
portable_device_manager = NULL; portable_device_manager.Release();
PyErr_SetString((hr == REGDB_E_CLASSNOTREG) ? NoWPD : WPDError, (hr == REGDB_E_CLASSNOTREG) ? 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." : "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"); "Failed to create the WPD device manager interface");
@ -54,11 +53,10 @@ wpd_init(PyObject *self, PyObject *args) {
static PyObject * static PyObject *
wpd_uninit(PyObject *self, PyObject *args) { wpd_uninit(PyObject *self, PyObject *args) {
if (portable_device_manager != NULL) { if (portable_device_manager) {
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
portable_device_manager->Release(); portable_device_manager.Release();
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
portable_device_manager = NULL;
} }
if (_com_initialized) { if (_com_initialized) {