2

I'm using StgCreateStorageEx from python win32com based adapting the code in testStorage.py to write my own file_id attribute onto any file.

According to alternate-streams (though not necessarily from this API call) it should be possible to save to a directory/folder, but changing the flags yield different errors, eg:

from win32com import storagecon
import pythoncom, os, win32api

fname = r"c:\temp\test\test.txt" #works
fname = r"c:\temp\test\test2"

def testit():
    m=storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE
    pss=pythoncom.StgOpenStorageEx(fname, m, storagecon.STGFMT_FILE, 0, pythoncom.IID_IPropertySetStorage)

results in:

pywintypes.com_error: (-2147024895, 'Incorrect function.', None, None)

EDIT: Any suggestions on how to get this to work from both WinXP, Win7 and Windows Server 2003/R2?

Note that the end result does not necessarily need to use this API, I just need to be able to do it efficiently from Python. By efficiently I mean not through many different technology layers.

RuiDC
  • 8,403
  • 7
  • 26
  • 21
  • I don't have access to a windows machine, or I would give this more of a look. The win32com library in python is just a wrapper for the win32 c/c++ api. You may want to look there to see if the MSDN is any help. [StgCreateStorageEx function]{http://msdn.microsoft.com/en-us/library/windows/desktop/aa380328%28v=vs.85%29.aspx) – BigHandsome Aug 17 '12 at 22:57
  • For an ADS, don't you need a colon somewhere? Like `test2:myads`? – user541686 Aug 18 '12 at 00:16
  • Mehrdad - not for access through this API, if you find an alternate, let me know. BigHandsome - Thanks, i'm aware of that but haven't been able to find anything to do the job. – RuiDC Aug 18 '12 at 07:03

2 Answers2

1

Looking at the results, StgOpenStorageEx adds a lot more to the file than simply writing to open(fname + ":stream_name"), so i've opted for that. Is there any downside to this compared to StgOpenStorageEx apart from not being able to write to the standard summary fields?

RuiDC
  • 8,403
  • 7
  • 26
  • 21
0

It succeeds if storagecon.STGM_DIRECT is added to the mode.

Roger Upole
  • 434
  • 3
  • 5
  • Whoops, that doesn't actually help. It worked on Windows 7 with or without that flag, but still fails on XP. – Roger Upole Aug 18 '12 at 01:42
  • pSecurityDescriptor on Windows Server 2003, Windows 2000 Server, Windows XP, and Windows 2000 Professional Value must be NULL. could that be the Windows XP issue? – BigHandsome Aug 18 '12 at 04:12
  • Doesn't seem to be. Once a directory is opened from Windows 7, it can then be opened from XP, even if you don't actually add any property sets. From looking at the folder before and after, the only difference is that the control stream is created by Windows 7. – Roger Upole Aug 18 '12 at 06:52
  • Thanks, WinXP is my starting point but need several target OS's - I've edited the question to reflect this. – RuiDC Aug 18 '12 at 07:11
  • 1
    If you manually create the control stream named ':{4c8cc155-6c1e-11d1-8e41-00c04fb9386d}', StgOpenStorageEx succeeds on WinXP. – Roger Upole Aug 18 '12 at 07:24