Background: I'm coding in Python and trying to use the win32 IFileOperation
to perform shell file operations. In particular, moving/copying a file or directory to a non-existant location requires you to get a IShellItem
(or IShellItem2
) item referring to the target directory, but SHCreateItemFromParsingName
fails if the target doesn't exist yet. Following advice from these related questions:
- Creating directories during a copy using IFileOperation
- IFileSystemBindData implementation not fully working
I've handled creating the WIN32_FIND_DATAW
structure, created the IBindCtx
object via pywin32's pythoncom.CreateBindCtx()
. The step I'm stuck on is implementing a IFileSysBindData
class in Python.
Here's what I've got so far:
class FileSysBindData:
_public_methods_ = ['SetFindData', 'GetFindData']
_reg_clsid_ = '{01E18D10-4D8B-11d2-855D-006008059367}'
def SetFindData(self, win32_find_data):
self.win32_find_data = win32_find_data
return 0
def GetFindData(self, p_win32_find_data):
p_win32_find_data.contents = self.win32_find_data
return 0
bind_data = FileSysBindData()
ibindctx = pythoncom.CreateBindCtx()
# TODO: set the bind data
ibindctx.RegisterObjectParam('File System Bind Data', bind_data)
The last line gives:
ValueError: argument is not a COM object (got type=FileSysBindData)
So obviously there's more to do to get pywin32 to create this in a COM-compatible way.
But I'm stuck on how to use pywin32 (or another solution) to actually create it as a COM object, so I can pass it to the IBindCtx
instance.
I'm not super familiar with COM in general, so reading the docs in pywin32 haven't helped me much. I don't even know if I'm supposed to register my class as a server, or use it as a client somehow.
The difficulty is you have to use two python packages:
Here is the code, and you'll see the trick to pass a comtypes's custom COM object to pywin32: