http://blog.csdn.net/lyris/archive/2006/06/23/825568.aspx#457378
提到shlwapi.dll里面的函数StrFormatByteSizeA、StrFormatKBSizeA以及PathCompactPathA。闲着无聊,用ctypes实现了它们
from ctypes import *
def StrFormatByteSize(qdw):
'''
Converts a numeric value into a string that represents the number expressed as a size value in bytes, kilobytes, megabytes, or gigabytes, depending on the size.
Syntax
LPTSTR StrFormatByteSize64(
LONGLONG qdw,
LPTSTR pszBuf,
UINT uiBufSize
);
Parameters
qdw
[in] Numeric value to be converted.
pszBuf
[out] Pointer to a buffer to hold the converted number.
uiBufSize
[in] Size of pszBuf, in characters.
Return Value
Returns the address of the converted string, or NULL if the conversion fails.
'''
lng=32
res=' '*32
cchBuf=c_uint(lng)
pszBuf=c_char_p(res)
windll.shlwapi.StrFormatByteSize64A(c_longlong(qdw),res,cchBuf)
res=res.strip()[:-1]
if len(res)>0:
return res
else:
return None
def StrFormatKBSize(qdw):
'''
Converts a numeric value into a string that represents the number expressed as a size value in kilobytes.
Syntax
LPTSTR StrFormatKBSize(
LONGLONG qdw,
LPTSTR pszBuf,
UINT uiBufSize
);
Parameters
qdw
[in] Numeric value to be converted.
pszBuf
[out] Pointer to a buffer to hold the converted number.
uiBufSize
[in] Size of pszBuf, in characters.
Return Value
Returns a pointer to the converted string, or NULL if the conversion fails.
'''
lng=32
res=' '*32
cchBuf=c_uint(lng)
pszBuf=c_char_p(res)
windll.SHLWAPI.StrFormatKBSizeA(c_longlong(qdw),res,cchBuf)
res=res.strip()[:-1]
if len(res)>0:
return res
else:
return None
def PathCompactPath(Path, dx):
'''
Truncates a file path to fit within a given pixel width by replacing path components with ellipses.
Syntax
BOOL PathCompactPath(
HDC hDC,
LPTSTR lpszPath,
UINT dx
);
Parameters
hDC
[in] Handle to the device context used for font metrics.
lpszPath
[in, out] Pointer to a null-terminated string of length MAX_PATH that contains the path to be modified. On return, this buffer will contain the modified string.
dx
[in] Width, in pixels, within which the string will be forced to fit.
Return Value
Returns TRUE if the path was successfully compacted to the specified width. Returns FALSE on failure, or if the base portion of the path would not fit the specified width.
'''
hDC=windll.user32.GetDC('\0')
if not Path.endswith('\0'):
Path=Path+chr(0)
_Path='%s'%Path
lpszPath=c_char_p(Path)
dx=c_uint(dx)
if windll.shlwapi.PathCompactPathA(hDC, lpszPath, dx):
Path=Path[:Path.find('\0')]
return [Path, True]
else:
#print '_path=', _Path
Path=Path[:Path.find('\0')]
_Path=_Path[:_Path.find('\0')]
#return [_Path, False]
return [Path, False]
if __name__=='__main__':
print windll.shlwapi
shlwapi=windll.shlwapi
s=shlwapi
print dir(s)
#s.StrFormatByteSize
#windll.kernel32.GetModuleHandleA
print 'windll.kernel32.__dict__=',(windll.kernel32.__dict__)
print
#s=cdll.shlwapi
print s.__dict__
print
#print s.DllGetVerison()
#getattr(cdll.msvcrt,"??2@YAPAXI@Z")
print 'StrFormatByteSizeA'
a=' '*32
#<=3.99 GB =0xFFFF,FFFF,FFFF,FF
windll.SHLWAPI.StrFormatByteSizeA(c_ulong(0xFFFFFFFFFFFFFF),c_char_p(a),c_uint(32))
a=a.strip()[:-1]
if len(a)>0:
print a
print
print 'StrFormatByteSizeW'
a=' '*64
windll.SHLWAPI.StrFormatByteSizeW(c_longlong(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF),c_wchar_p(a),c_uint(64))
a=a.strip()[:-1]
if len(a)>0:
print a
print
print 'using python StrFormatKBSize function'
for i in [1, 20, 0xFF, 0xFFF, 0xFFFFF, 0xFFFFFFF, 0xEFFFFFFF]:
print '%12i -> %s' % (i, StrFormatKBSize(i))
print
print 'using python StrFormatByteSize function'
test=[532, 1340, 23506, 2400016, 2400000000, 0xFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF]
for i in test:
print '%12i -> %s' % (i, StrFormatByteSize(i))
print
############################################
'''
PathCompactPathEx Function
Truncates a path to fit within a certain number of characters by replacing path components with ellipses.
Syntax
BOOL PathCompactPathEx(
LPTSTR pszOut,
LPCTSTR pszSrc,
UINT cchMax,
DWORD dwFlags
);
Parameters
pszOut
[out] Address of the string that has been altered.
pszSrc
[in] Pointer to a null-terminated string of maximum length MAX_PATH that contains the path to be altered.
cchMax
[in] Maximum number of characters to be contained in the new string, including the terminating NULL character. For example, if cchMax = 8, the resulting string can contain a maximum of 7 characters plus the terminating NULL character.
dwFlags
Reserved.
Return Value
Returns TRUE if successful, or FALSE otherwise.
'''
print 'try PathCompactPathEx'
Out=''*1000
Src=r'c:\WINNT\system32\asferror.dll' +chr(0)
pszOut=c_char_p(Out)
pszSrc=c_char_p(Src)
cchMax=c_uint(10)
dwFlags=c_ulong(0)
if windll.shlwapi.PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags)==True:
print pszOut
print
############################################
print 'using python PathCompactPath function'
Path=r'c:\WINNT\system32\adsldp.dll'
for dx in range(0,500,30):
print '%s -> %s' % (Path, PathCompactPath(Path, dx))
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=6292958