【问题标题】:ValueError: NULL COM pointer accessValueError: NULL COM 指针访问
【发布时间】:2013-12-16 02:54:21
【问题描述】:

我正在尝试扩展 azure WinHttpRequest python 绑定,以便能够修改请求选项。理想情况下,我只想通过 winhttp.dll WinHttpSetOptions 函数设置全局选项,但我不太清楚如何做到这一点。无论如何,我决定冒险尝试这种方法,但我得到一个“NULL COM 指针访问”错误。这与我分配给 _put_Option 声明的无效序号有关吗?即(30, 'put_Option') 还是只是找不到符号?我正在使用 IWinHttpRequest 文档作为指导:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa383998(v=vs.85).aspx

class _WinHttpRequestOption(object):
    MaxResponseHeaderSize = 15


class _WinHttpRequestExtension(_WinHttpRequest):
    _put_Option = WINFUNCTYPE(HRESULT, c_int, VARIANT)(30, 'put_Option')

    def _SetOption(self, name, value):
        logging.getLogger(self.__class__.__name__).debug(
            "SetOption %s = %s" % (name, value)
        )

        enum_name = getattr(_WinHttpRequestOption, name)

        var_value = VARIANT()
        var_value.vt = VT_I4
        var_value.vdata.lval = long(value)

        _WinHttpRequestExtension._put_Option(self, enum_name, var_value)

哦,是的,设置属性的代码:

http_request = _WinHttpRequestExtension()
http_request._SetOption("MaxResponseHeaderSize", 128 * 1024)

更新:

找到这个链接:

https://chromium.googlesource.com/chromium/deps/perl/+/master%5E/c/i686-w64-mingw32/include/httprequestid.h

它为函数定义了一个调度 ID。由于我不是 Windows 开发人员,所以我不知道调度 ID 是什么。虽然用这个替换我的序数仍然不起作用。

#define DISPID_HTTPREQUEST_BASE 0x00000001
#define DISPID_HTTPREQUEST_OPTION (DISPID_HTTPREQUEST_BASE + 5)

_put_Option = WINFUNCTYPE(HRESULT, c_int, VARIANT)(6, 'Option')

还有

我发现了这个,这表明self 不是对正确事物的引用。查看此代码中的 COM 错误。 if (!this->b_ptr || *(void **)this->b_ptr == NULL) {

#ifdef MS_WIN32
if (self->index) {
    /* It's a COM method */
    CDataObject *this;
    this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */
    if (!this) {
        PyErr_SetString(PyExc_ValueError,
                        "native com method call without 'this' parameter");
        return NULL;
    }
    if (!CDataObject_Check(this)) {
        PyErr_SetString(PyExc_TypeError,
                        "Expected a COM this pointer as first argument");
        return NULL;
    }
    /* there should be more checks? No, in Python */
    /* First arg is an pointer to an interface instance */
    if (!this->b_ptr || *(void **)this->b_ptr == NULL) {
        PyErr_SetString(PyExc_ValueError,
                        "NULL COM pointer access");
        return NULL;
    }

如果我这样做,我会得到"Expected a COM this pointer as first argument" 错误:

_WinHttpRequestExtension._put_Option(super(_WinHttpRequestExtension, self), enum_name, var_value)

【问题讨论】:

    标签: python ctypes winhttp


    【解决方案1】:

    终于发现我正在访问对象上的方法之前它是CoInitialised()。将调用移到对 CoInitialise 的调用之外似乎让我走得更远,但现在我得到了错误:

    ValueError: 过程可能调用了太多参数(超过 16 个字节)

    【讨论】:

      猜你喜欢
      • 2014-03-16
      • 2012-03-11
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2010-10-28
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多