Start using RAII in WPD driver

This commit is contained in:
Kovid Goyal 2021-04-20 14:29:51 +05:30
parent 1516d709fb
commit 96af51a318
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 13 additions and 19 deletions

View File

@ -22,7 +22,7 @@ IPortableDeviceValues *get_client_information() { // {{{
if (FAILED(hr)) { hresult_set_exc("Failed to create IPortableDeviceValues", hr); return NULL; } if (FAILED(hr)) { hresult_set_exc("Failed to create IPortableDeviceValues", hr); return NULL; }
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
hr = client_information->SetStringValue(WPD_CLIENT_NAME, client_info.name); hr = client_information->SetStringValue(WPD_CLIENT_NAME, client_info.name.ptr());
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
if (FAILED(hr)) { hresult_set_exc("Failed to set client name", hr); return NULL; } if (FAILED(hr)) { hresult_set_exc("Failed to set client name", hr); return NULL; }
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;

View File

@ -13,6 +13,7 @@
#include <Objbase.h> #include <Objbase.h>
#include <PortableDeviceApi.h> #include <PortableDeviceApi.h>
#include <PortableDevice.h> #include <PortableDevice.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 == NULL) { PyErr_SetString(NoWPD, "No WPD service available."); return retval; }
@ -27,7 +28,7 @@ extern IPortableDeviceManager *portable_device_manager;
// Application info // Application info
typedef struct { typedef struct {
wchar_t *name; wchar_raii name;
unsigned int major_version; unsigned int major_version;
unsigned int minor_version; unsigned int minor_version;
unsigned int revision; unsigned int revision;
@ -63,4 +64,3 @@ extern PyObject* delete_object(IPortableDevice *device, const wchar_t *object_id
extern PyObject* put_file(IPortableDevice *device, const wchar_t *parent_id, const wchar_t *name, PyObject *src, unsigned PY_LONG_LONG size, PyObject *callback); extern PyObject* put_file(IPortableDevice *device, const wchar_t *parent_id, const wchar_t *name, PyObject *src, unsigned PY_LONG_LONG size, PyObject *callback);
} }

View File

@ -6,7 +6,6 @@
*/ */
#include "global.h" #include "global.h"
#include "../../../utils/windows/common.h"
using namespace wpd; using namespace wpd;

View File

@ -18,7 +18,7 @@ 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;
// Application Info // Application Info
wpd::ClientInfo wpd::client_info = {NULL, 0, 0, 0}; wpd::ClientInfo wpd::client_info = {0};
extern IPortableDeviceValues* wpd::get_client_information(); extern IPortableDeviceValues* wpd::get_client_information();
extern IPortableDevice* wpd::open_device(const wchar_t *pnp_id, IPortableDeviceValues *client_information); extern IPortableDevice* wpd::open_device(const wchar_t *pnp_id, IPortableDeviceValues *client_information);
@ -28,10 +28,7 @@ extern PyObject* wpd::get_device_information(IPortableDevice *device, IPortableD
static PyObject * static PyObject *
wpd_init(PyObject *self, PyObject *args) { wpd_init(PyObject *self, PyObject *args) {
HRESULT hr; HRESULT hr;
PyObject *o; if (!PyArg_ParseTuple(args, "O&III", py_to_wchar_no_none, &client_info.name, &client_info.major_version, &client_info.minor_version, &client_info.revision)) return NULL;
if (!PyArg_ParseTuple(args, "OIII", &o, &client_info.major_version, &client_info.minor_version, &client_info.revision)) return NULL;
client_info.name = unicode_to_wchar(o);
if (client_info.name == NULL) return NULL;
if (!_com_initialized) { if (!_com_initialized) {
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
@ -75,7 +72,7 @@ wpd_uninit(PyObject *self, PyObject *args) {
_com_initialized = 0; _com_initialized = 0;
} }
if (client_info.name != NULL) { free(client_info.name); } client_info.name.release();
// hresult_set_exc("test", HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)); return NULL; // hresult_set_exc("test", HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)); return NULL;
Py_RETURN_NONE; Py_RETURN_NONE;
@ -137,27 +134,24 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) {
// device_info() {{{ // device_info() {{{
static PyObject * static PyObject *
wpd_device_info(PyObject *self, PyObject *args) { wpd_device_info(PyObject *self, PyObject *args) {
PyObject *py_pnp_id, *ans = NULL; PyObject *ans = NULL;
wchar_t *pnp_id;
IPortableDeviceValues *client_information = NULL; IPortableDeviceValues *client_information = NULL;
IPortableDevice *device = NULL; IPortableDevice *device = NULL;
ENSURE_WPD(NULL); ENSURE_WPD(NULL);
if (!PyArg_ParseTuple(args, "O", &py_pnp_id)) return NULL; wchar_raii pnp_id;
pnp_id = unicode_to_wchar(py_pnp_id); if (!PyArg_ParseTuple(args, "O&", py_to_wchar_no_none, &pnp_id)) return NULL;
if (pnp_id == NULL) return NULL; if (wcslen(pnp_id.ptr()) < 1) { PyErr_SetString(WPDError, "The PNP id must not be empty."); return NULL; }
if (wcslen(pnp_id) < 1) { PyErr_SetString(WPDError, "The PNP id must not be empty."); return NULL; }
client_information = get_client_information(); client_information = get_client_information();
if (client_information != NULL) { if (client_information != NULL) {
device = open_device(pnp_id, client_information); device = open_device(pnp_id.ptr(), client_information);
if (device != NULL) { if (device != NULL) {
ans = get_device_information(device, NULL); ans = get_device_information(device, NULL);
} }
} }
if (pnp_id != NULL) free(pnp_id);
if (client_information != NULL) client_information->Release(); if (client_information != NULL) client_information->Release();
if (device != NULL) {device->Close(); device->Release();} if (device != NULL) {device->Close(); device->Release();}
return ans; return ans;

View File

@ -36,7 +36,8 @@ class wchar_raii {
wchar_raii() : handle(NULL) {} wchar_raii() : handle(NULL) {}
wchar_raii(wchar_t *h) : handle(h) {} wchar_raii(wchar_t *h) : handle(h) {}
~wchar_raii() { ~wchar_raii() { release(); }
void release() {
if (handle) { if (handle) {
PyMem_Free(handle); PyMem_Free(handle);
handle = NULL; handle = NULL;