diff --git a/src/calibre/devices/mtp/windows/device.cpp b/src/calibre/devices/mtp/windows/device.cpp index d7f63f7038..6fd65dc58d 100644 --- a/src/calibre/devices/mtp/windows/device.cpp +++ b/src/calibre/devices/mtp/windows/device.cpp @@ -12,15 +12,13 @@ using namespace wpd; static void dealloc(Device* self) { - if (self->pnp_id != NULL) free(self->pnp_id); - self->pnp_id = NULL; + self->pnp_id.release(); + if (self->bulk_properties) self->bulk_properties.Release(); - if (self->bulk_properties != NULL) { self->bulk_properties->Release(); self->bulk_properties = NULL; } - - if (self->device != NULL) { + if (self->device) { Py_BEGIN_ALLOW_THREADS; - self->device->Close(); self->device->Release(); - self->device = NULL; + self->device->Close(); + self->device.Release(); Py_END_ALLOW_THREADS; } @@ -32,28 +30,21 @@ dealloc(Device* self) static int init(Device *self, PyObject *args, PyObject *kwds) { - PyObject *pnp_id; int ret = -1; - - if (!PyArg_ParseTuple(args, "O", &pnp_id)) return -1; - - self->pnp_id = unicode_to_wchar(pnp_id); - if (self->pnp_id == NULL) return -1; - - self->bulk_properties = NULL; - + if (!PyArg_ParseTuple(args, "O&", py_to_wchar_no_none, &self->pnp_id)) return -1; + self->bulk_properties.Release(); CComPtr client_information = get_client_information(); if (client_information) { - self->device = open_device(self->pnp_id, client_information); - if (self->device != NULL) { - self->device_information = get_device_information(self->device, &(self->bulk_properties)); - if (self->device_information != NULL) { - ret = 0; - } - + self->device = open_device(self->pnp_id.ptr(), client_information); + if (self->device) { + IPortableDevicePropertiesBulk *bulk_properties = NULL; + self->device_information = get_device_information(self->device, &bulk_properties); + if (self->device_information) { + ret = 0; + self->bulk_properties = bulk_properties; + } } } - return ret; } diff --git a/src/calibre/devices/mtp/windows/device_enumeration.cpp b/src/calibre/devices/mtp/windows/device_enumeration.cpp index dd4435ed25..ecc085af89 100644 --- a/src/calibre/devices/mtp/windows/device_enumeration.cpp +++ b/src/calibre/devices/mtp/windows/device_enumeration.cpp @@ -10,14 +10,13 @@ namespace wpd { IPortableDeviceValues *get_client_information() { // {{{ - IPortableDeviceValues *client_information; HRESULT hr; ENSURE_WPD(NULL); + CComPtr client_information; Py_BEGIN_ALLOW_THREADS; - hr = CoCreateInstance(CLSID_PortableDeviceValues, NULL, - CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&client_information)); + hr = client_information.CoCreateInstance(CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER); Py_END_ALLOW_THREADS; if (FAILED(hr)) { hresult_set_exc("Failed to create IPortableDeviceValues", hr); return NULL; } @@ -43,7 +42,7 @@ IPortableDeviceValues *get_client_information() { // {{{ hr = client_information->SetUnsignedIntegerValue(WPD_CLIENT_SECURITY_QUALITY_OF_SERVICE, SECURITY_IMPERSONATION); Py_END_ALLOW_THREADS; if (FAILED(hr)) { hresult_set_exc("Failed to set quality of service", hr); return NULL; } - return client_information; + return client_information.Detach(); } // }}} IPortableDevice *open_device(const wchar_t *pnp_id, CComPtr &client_information) { // {{{ @@ -207,7 +206,8 @@ end: return ans; } // }}} -PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **pb) { // {{{ +PyObject* +get_device_information(CComPtr &device, IPortableDevicePropertiesBulk **pb) { // {{{ IPortableDeviceContent *content = NULL; IPortableDeviceProperties *properties = NULL; IPortableDevicePropertiesBulk *properties_bulk = NULL; diff --git a/src/calibre/devices/mtp/windows/global.h b/src/calibre/devices/mtp/windows/global.h index 8baee04bff..8d9de83d5f 100644 --- a/src/calibre/devices/mtp/windows/global.h +++ b/src/calibre/devices/mtp/windows/global.h @@ -40,11 +40,10 @@ extern ClientInfo client_info; typedef struct { PyObject_HEAD // Type-specific fields go here. - wchar_t *pnp_id; - IPortableDevice *device; + wchar_raii pnp_id; + CComPtr device; PyObject *device_information; - IPortableDevicePropertiesBulk *bulk_properties; - + CComPtr bulk_properties; } Device; extern PyTypeObject DeviceType; @@ -56,7 +55,7 @@ int pump_waiting_messages(); extern IPortableDeviceValues* get_client_information(); extern IPortableDevice* open_device(const wchar_t *pnp_id, CComPtr &client_information); -extern PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **bulk_properties); +extern PyObject* get_device_information(CComPtr &device, IPortableDevicePropertiesBulk **bulk_properties); extern PyObject* get_filesystem(IPortableDevice *device, const wchar_t *storage_id, IPortableDevicePropertiesBulk *bulk_properties, PyObject *callback); extern PyObject* get_file(IPortableDevice *device, const wchar_t *object_id, PyObject *dest, PyObject *callback); extern PyObject* create_folder(IPortableDevice *device, const wchar_t *parent_id, const wchar_t *name); diff --git a/src/calibre/devices/mtp/windows/wpd.cpp b/src/calibre/devices/mtp/windows/wpd.cpp index 9355e0bfea..b34082ce8a 100644 --- a/src/calibre/devices/mtp/windows/wpd.cpp +++ b/src/calibre/devices/mtp/windows/wpd.cpp @@ -131,7 +131,7 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) { static PyObject * wpd_device_info(PyObject *self, PyObject *args) { PyObject *ans = NULL; - IPortableDevice *device = NULL; + CComPtr device = NULL; ENSURE_WPD(NULL); @@ -142,12 +142,10 @@ wpd_device_info(PyObject *self, PyObject *args) { CComPtr client_information = get_client_information(); if (client_information) { device = open_device(pnp_id.ptr(), client_information); - if (device != NULL) { - ans = get_device_information(device, NULL); - } + if (device) ans = get_device_information(device, NULL); } - if (device != NULL) {device->Close(); device->Release();} + if (device) device->Close(); return ans; } // }}}