如何从ctypes使用IFileOperation

如何从ctypes使用IFileOperation,第1张

如何从ctypes使用IFileOperation

该问题使用comtypes.GUID作为唯一(非标准)依赖项。

看一下comtypes本身,它是纯python并使用ctypes(用于CoCreateInstance和所有其他类型),并且可以找到加载和处理COM对象所需的windows函数的路径,特别是-

import ctypesctypes.oledll.ole32.CoCreateInstance()

需要明确地放置CLSID,如所提到的问题-

IID_IFileOperation  = '{947AAB5F-0A5C-4C13-B4D6-4BF7836FC9F8}'CLSID_FileOperation = '{3AD05575-8857-4850-9277-11B85BDB8E09}'

总而言之,comtypes是一个小的纯python库,似乎足以完成此任务,如果您不想修改ctypes,粘贴GUID或不介意依赖项。

但是,正如comtypes本身所证明的那样,这在ctypes中是完全可实现的,但有可能需要手动添加GUID的警告-

from ctypes import *BYTE, WORD, DWORD = c_byte, c_ushort, c_ulong_StringFromCLSID = oledll.ole32.StringFromCLSID_ProgIDFromCLSID = oledll.ole32.ProgIDFromCLSID_CLSIDFromString = oledll.ole32.CLSIDFromString_CLSIDFromProgID = oledll.ole32.CLSIDFromProgID_CoCreateGuid    = oledll.ole32.CoCreateGuid_CoTaskMemFree   = windll.ole32.CoTaskMemFreeclass GUID(Structure):    _fields_ = [("Data1", DWORD),     ("Data2", WORD),     ("Data3", WORD),     ("Data4", BYTE * 8)]    def __init__(self, name=None):        if name is not None: _CLSIDFromString(unipre(name), byref(self))    def __repr__(self):        return u'GUID("%s")' % unipre(self)    def __unipre__(self):        p = c_wchar_p()        _StringFromCLSID(byref(self), byref(p))        result = p.value        _CoTaskMemFree(p)        return result    __str__ = __unipre__    def __cmp__(self, other):        if isinstance(other, GUID): return cmp(bytes(self), bytes(other))        return -1    def __nonzero__(self):        return self != GUID_null    def __eq__(self, other):        return isinstance(other, GUID) and     bytes(self) == bytes(other)    def __hash__(self):        # We make GUID instances hashable, although they are mutable.        return hash(bytes(self))    def copy(self):        return GUID(unipre(self))    def from_progid(cls, progid):        """Get guid from progid, ...        """        if hasattr(progid, "_reg_clsid_"): progid = progid._reg_clsid_        if isinstance(progid, cls): return progid        elif isinstance(progid, basestring): if progid.startswith("{"):     return cls(progid) inst = cls() _CLSIDFromProgID(unipre(progid), byref(inst)) return inst        else: raise TypeError("Cannot construct guid from %r" % progid)    from_progid = classmethod(from_progid)    def as_progid(self):        "Convert a GUID into a progid"        progid = c_wchar_p()        _ProgIDFromCLSID(byref(self), byref(progid))        result = progid.value        _CoTaskMemFree(progid)        return result    def create_new(cls):        "Create a brand new guid"        guid = cls()        _CoCreateGuid(byref(guid))        return guid    create_new = classmethod(create_new)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5644639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存