如何使用ctypes中的IFileOperation

5

我希望使用IFileOperation在Python代码中复制文件 -

  • 它比Python快
  • 你会得到一个漂亮的对话框
  • 不会阻塞Python

在Windows 10,Python 3.8上可用 -

import ctypes

ctypes.windll.shell32.IFileOperation

似乎不存在。

我该如何使用ctypes访问IFileOperation(而不是已弃用的SHFileOperation API)?


1
我猜pywin32有这个功能 https://github.com/mhammond/pywin32 - ewerybody
win32com有什么问题? - Simon Mourier
https://github.com/frmdstryr/pywinutils/blob/master/winutils.py - Joe
2个回答

3
当然,它位于 pythoncomshell 中,用于常量,例如:
from win32com.shell import shell
import pythoncom

# create an instance of IFileOperation
fo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation)

# here you can use SetOperationFlags, progress Sinks, etc.

# create an instance of IShellItem for the source item
item1 = shell.SHCreateItemFromParsingName("c:\\temp\\source.txt", None, shell.IID_IShellItem)

# create an instance of IShellItem for the target folder
folder = shell.SHCreateItemFromParsingName("c:\\another", None, shell.IID_IShellItem)

# queue the copy operation
fo.CopyItem(item1, folder, "new name.txt", None)

# commit
fo.PerformOperations()

1

这个问题让我有了方向,因为它展示了 ctypes 可以加载 Windows 功能性的 COM,尽管需要更多的工作。

该问题唯一(非标准)依赖项是 comtypes.GUID

看着 comtypes 本身,它是纯 Python 的,并且使用 ctypes(用于 CoCreateInstance 和所有其他内容),并且可以找到需要加载和处理 COM 对象的 Windows 函数路径,特别是 -

import ctypes
ctypes.oledll.ole32.CoCreateInstance()

需要明确放置CLSIDs,就像所提到的问题一样 -
IID_IFileOperation  = '{947AAB5F-0A5C-4C13-B4D6-4BF7836FC9F8}'
CLSID_FileOperation = '{3AD05575-8857-4850-9277-11B85BDB8E09}'

总的来说,对于这个任务而言,comtypes是一个足够小巧纯粹的Python库,如果不想使用ctypes、粘贴GUID或者不介意依赖性的话,它似乎已经足够了。
然而,这完全可以在ctypes中实现,正如comtypes本身所证明的一样,只是可能需要手动添加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.CoTaskMemFree

class GUID(Structure):
    _fields_ = [("Data1", DWORD),
                ("Data2", WORD),
                ("Data3", WORD),
                ("Data4", BYTE * 8)]

    def __init__(self, name=None):
        if name is not None:
            _CLSIDFromString(unicode(name), byref(self))

    def __repr__(self):
        return u'GUID("%s")' % unicode(self)

    def __unicode__(self):
        p = c_wchar_p()
        _StringFromCLSID(byref(self), byref(p))
        result = p.value
        _CoTaskMemFree(p)
        return result
    __str__ = __unicode__

    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(unicode(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(unicode(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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接