Use CComPtr for client information

This commit is contained in:
Kovid Goyal 2021-04-20 14:49:23 +05:30
parent 96af51a318
commit 06d82ad1c3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 8 additions and 20 deletions

View File

@ -7,10 +7,6 @@
#include "global.h"
extern IPortableDevice* wpd::open_device(const wchar_t *pnp_id, IPortableDeviceValues *client_information);
extern IPortableDeviceValues* wpd::get_client_information();
extern PyObject* wpd::get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **pb);
using namespace wpd;
// Device.__init__() {{{
static void
@ -28,8 +24,6 @@ dealloc(Device* self)
Py_END_ALLOW_THREADS;
}
if (self->client_information != NULL) { self->client_information->Release(); self->client_information = NULL; }
Py_XDECREF(self->device_information); self->device_information = NULL;
Py_TYPE(self)->tp_free((PyObject*)self);
@ -48,9 +42,9 @@ init(Device *self, PyObject *args, PyObject *kwds)
self->bulk_properties = NULL;
self->client_information = get_client_information();
if (self->client_information != NULL) {
self->device = open_device(self->pnp_id, self->client_information);
CComPtr<IPortableDeviceValues> 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) {

View File

@ -46,7 +46,7 @@ IPortableDeviceValues *get_client_information() { // {{{
return client_information;
} // }}}
IPortableDevice *open_device(const wchar_t *pnp_id, IPortableDeviceValues *client_information) { // {{{
IPortableDevice *open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information) { // {{{
IPortableDevice *device = NULL;
HRESULT hr;

View File

@ -8,6 +8,7 @@
#pragma once
#define UNICODE
#include <Windows.h>
#include <atlbase.h>
#include <Python.h>
#include <Objbase.h>
@ -40,7 +41,6 @@ typedef struct {
PyObject_HEAD
// Type-specific fields go here.
wchar_t *pnp_id;
IPortableDeviceValues *client_information;
IPortableDevice *device;
PyObject *device_information;
IPortableDevicePropertiesBulk *bulk_properties;
@ -55,7 +55,7 @@ PyObject *wchar_to_unicode(const wchar_t *o);
int pump_waiting_messages();
extern IPortableDeviceValues* get_client_information();
extern IPortableDevice* open_device(const wchar_t *pnp_id, IPortableDeviceValues *client_information);
extern IPortableDevice* open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information);
extern PyObject* get_device_information(IPortableDevice *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);

View File

@ -20,10 +20,6 @@ static int _com_initialized = 0;
// Application Info
wpd::ClientInfo wpd::client_info = {0};
extern IPortableDeviceValues* wpd::get_client_information();
extern IPortableDevice* wpd::open_device(const wchar_t *pnp_id, IPortableDeviceValues *client_information);
extern PyObject* wpd::get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **bulk_properties);
// Module startup/shutdown {{{
static PyObject *
wpd_init(PyObject *self, PyObject *args) {
@ -135,7 +131,6 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) {
static PyObject *
wpd_device_info(PyObject *self, PyObject *args) {
PyObject *ans = NULL;
IPortableDeviceValues *client_information = NULL;
IPortableDevice *device = NULL;
ENSURE_WPD(NULL);
@ -144,15 +139,14 @@ wpd_device_info(PyObject *self, PyObject *args) {
if (!PyArg_ParseTuple(args, "O&", py_to_wchar_no_none, &pnp_id)) return NULL;
if (wcslen(pnp_id.ptr()) < 1) { PyErr_SetString(WPDError, "The PNP id must not be empty."); return NULL; }
client_information = get_client_information();
if (client_information != NULL) {
CComPtr<IPortableDeviceValues> 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 (client_information != NULL) client_information->Release();
if (device != NULL) {device->Close(); device->Release();}
return ans;
} // }}}