Clean up wpd utils

This commit is contained in:
Kovid Goyal 2021-04-20 14:06:14 +05:30
parent 7f4685544c
commit 1516d709fb
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 4 additions and 20 deletions

View File

@ -162,7 +162,7 @@
"name": "wpd",
"only": "windows",
"sources": "calibre/devices/mtp/windows/utils.cpp calibre/devices/mtp/windows/device_enumeration.cpp calibre/devices/mtp/windows/content_enumeration.cpp calibre/devices/mtp/windows/device.cpp calibre/devices/mtp/windows/wpd.cpp",
"headers": "calibre/devices/mtp/windows/global.h",
"headers": "calibre/devices/mtp/windows/global.h calibre/utils/windows/common.h",
"libraries": "ole32 oleaut32 portabledeviceguids user32",
"cflags": "/X"
},

View File

@ -6,28 +6,12 @@
*/
#include "global.h"
#include "../../../utils/windows/common.h"
using namespace wpd;
PyObject *wpd::hresult_set_exc(const char *msg, HRESULT hr) {
PyObject *o = NULL, *mess;
LPWSTR desc = NULL;
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&desc, 0, NULL);
if (desc == NULL) {
o = PyUnicode_FromString("No description available.");
} else {
o = PyUnicode_FromWideChar(desc, wcslen(desc));
LocalFree(desc);
}
if (o == NULL) return PyErr_NoMemory();
mess = PyUnicode_FromFormat("%s: hr=%lu facility=%u error_code=%u description: %U", msg, hr, HRESULT_FACILITY(hr), HRESULT_CODE(hr), o);
Py_XDECREF(o);
if (mess == NULL) return PyErr_NoMemory();
PyErr_SetObject(WPDError, mess);
Py_DECREF(mess);
return NULL;
return error_from_hresult(hr, msg);
}
wchar_t *wpd::unicode_to_wchar(PyObject *o) {
@ -46,7 +30,7 @@ wchar_t *wpd::unicode_to_wchar(PyObject *o) {
PyObject *wpd::wchar_to_unicode(const wchar_t *o) {
PyObject *ans;
if (o == NULL) return NULL;
ans = PyUnicode_FromWideChar(o, wcslen(o));
ans = PyUnicode_FromWideChar(o, -1);
if (ans == NULL) PyErr_NoMemory();
return ans;
}