1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """
32 Wrapper for shell32.dll in ctypes.
33 """
34
35
36
37
38
39 __revision__ = "$Id: shell32.py 1299 2013-12-20 09:30:55Z qvasimodo $"
40
41 from defines import *
42 from kernel32 import LocalFree
43
44
45
46 _all = None
47 _all = set(vars().keys())
48
49
50
51
52 SEE_MASK_DEFAULT = 0x00000000
53 SEE_MASK_CLASSNAME = 0x00000001
54 SEE_MASK_CLASSKEY = 0x00000003
55 SEE_MASK_IDLIST = 0x00000004
56 SEE_MASK_INVOKEIDLIST = 0x0000000C
57 SEE_MASK_ICON = 0x00000010
58 SEE_MASK_HOTKEY = 0x00000020
59 SEE_MASK_NOCLOSEPROCESS = 0x00000040
60 SEE_MASK_CONNECTNETDRV = 0x00000080
61 SEE_MASK_NOASYNC = 0x00000100
62 SEE_MASK_DOENVSUBST = 0x00000200
63 SEE_MASK_FLAG_NO_UI = 0x00000400
64 SEE_MASK_UNICODE = 0x00004000
65 SEE_MASK_NO_CONSOLE = 0x00008000
66 SEE_MASK_ASYNCOK = 0x00100000
67 SEE_MASK_HMONITOR = 0x00200000
68 SEE_MASK_NOZONECHECKS = 0x00800000
69 SEE_MASK_WAITFORINPUTIDLE = 0x02000000
70 SEE_MASK_FLAG_LOG_USAGE = 0x04000000
71
72 SE_ERR_FNF = 2
73 SE_ERR_PNF = 3
74 SE_ERR_ACCESSDENIED = 5
75 SE_ERR_OOM = 8
76 SE_ERR_DLLNOTFOUND = 32
77 SE_ERR_SHARE = 26
78 SE_ERR_ASSOCINCOMPLETE = 27
79 SE_ERR_DDETIMEOUT = 28
80 SE_ERR_DDEFAIL = 29
81 SE_ERR_DDEBUSY = 30
82 SE_ERR_NOASSOC = 31
83
84 SHGFP_TYPE_CURRENT = 0
85 SHGFP_TYPE_DEFAULT = 1
86
87 CSIDL_DESKTOP = 0x0000
88 CSIDL_INTERNET = 0x0001
89 CSIDL_PROGRAMS = 0x0002
90 CSIDL_CONTROLS = 0x0003
91 CSIDL_PRINTERS = 0x0004
92 CSIDL_PERSONAL = 0x0005
93 CSIDL_FAVORITES = 0x0006
94 CSIDL_STARTUP = 0x0007
95 CSIDL_RECENT = 0x0008
96 CSIDL_SENDTO = 0x0009
97 CSIDL_BITBUCKET = 0x000a
98 CSIDL_STARTMENU = 0x000b
99 CSIDL_MYDOCUMENTS = CSIDL_PERSONAL
100 CSIDL_MYMUSIC = 0x000d
101 CSIDL_MYVIDEO = 0x000e
102 CSIDL_DESKTOPDIRECTORY = 0x0010
103 CSIDL_DRIVES = 0x0011
104 CSIDL_NETWORK = 0x0012
105 CSIDL_NETHOOD = 0x0013
106 CSIDL_FONTS = 0x0014
107 CSIDL_TEMPLATES = 0x0015
108 CSIDL_COMMON_STARTMENU = 0x0016
109 CSIDL_COMMON_PROGRAMS = 0x0017
110 CSIDL_COMMON_STARTUP = 0x0018
111 CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019
112 CSIDL_APPDATA = 0x001a
113 CSIDL_PRINTHOOD = 0x001b
114 CSIDL_LOCAL_APPDATA = 0x001c
115 CSIDL_ALTSTARTUP = 0x001d
116 CSIDL_COMMON_ALTSTARTUP = 0x001e
117 CSIDL_COMMON_FAVORITES = 0x001f
118 CSIDL_INTERNET_CACHE = 0x0020
119 CSIDL_COOKIES = 0x0021
120 CSIDL_HISTORY = 0x0022
121 CSIDL_COMMON_APPDATA = 0x0023
122 CSIDL_WINDOWS = 0x0024
123 CSIDL_SYSTEM = 0x0025
124 CSIDL_PROGRAM_FILES = 0x0026
125 CSIDL_MYPICTURES = 0x0027
126 CSIDL_PROFILE = 0x0028
127 CSIDL_SYSTEMX86 = 0x0029
128 CSIDL_PROGRAM_FILESX86 = 0x002a
129 CSIDL_PROGRAM_FILES_COMMON = 0x002b
130 CSIDL_PROGRAM_FILES_COMMONX86 = 0x002c
131 CSIDL_COMMON_TEMPLATES = 0x002d
132 CSIDL_COMMON_DOCUMENTS = 0x002e
133 CSIDL_COMMON_ADMINTOOLS = 0x002f
134 CSIDL_ADMINTOOLS = 0x0030
135 CSIDL_CONNECTIONS = 0x0031
136 CSIDL_COMMON_MUSIC = 0x0035
137 CSIDL_COMMON_PICTURES = 0x0036
138 CSIDL_COMMON_VIDEO = 0x0037
139 CSIDL_RESOURCES = 0x0038
140 CSIDL_RESOURCES_LOCALIZED = 0x0039
141 CSIDL_COMMON_OEM_LINKS = 0x003a
142 CSIDL_CDBURN_AREA = 0x003b
143 CSIDL_COMPUTERSNEARME = 0x003d
144 CSIDL_PROFILES = 0x003e
145
146 CSIDL_FOLDER_MASK = 0x00ff
147
148 CSIDL_FLAG_PER_USER_INIT = 0x0800
149 CSIDL_FLAG_NO_ALIAS = 0x1000
150 CSIDL_FLAG_DONT_VERIFY = 0x4000
151 CSIDL_FLAG_CREATE = 0x8000
152
153 CSIDL_FLAG_MASK = 0xff00
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
179 _fields_ = [
180 ("cbSize", DWORD),
181 ("fMask", ULONG),
182 ("hwnd", HWND),
183 ("lpVerb", LPSTR),
184 ("lpFile", LPSTR),
185 ("lpParameters", LPSTR),
186 ("lpDirectory", LPSTR),
187 ("nShow", ctypes.c_int),
188 ("hInstApp", HINSTANCE),
189 ("lpIDList", LPVOID),
190 ("lpClass", LPSTR),
191 ("hkeyClass", HKEY),
192 ("dwHotKey", DWORD),
193 ("hIcon", HANDLE),
194 ("hProcess", HANDLE),
195 ]
196
201 hMonitor = property(__get_hMonitor, __set_hMonitor)
202
203 LPSHELLEXECUTEINFO = POINTER(SHELLEXECUTEINFO)
204
205
206
207
208
209
210
212 _CommandLineToArgvW = windll.shell32.CommandLineToArgvW
213 _CommandLineToArgvW.argtypes = [LPVOID, POINTER(ctypes.c_int)]
214 _CommandLineToArgvW.restype = LPVOID
215
216 if not lpCmdLine:
217 lpCmdLine = None
218 argc = ctypes.c_int(0)
219 vptr = ctypes.windll.shell32.CommandLineToArgvW(lpCmdLine, byref(argc))
220 if vptr == NULL:
221 raise ctypes.WinError()
222 argv = vptr
223 try:
224 argc = argc.value
225 if argc <= 0:
226 raise ctypes.WinError()
227 argv = ctypes.cast(argv, ctypes.POINTER(LPWSTR * argc) )
228 argv = [ argv.contents[i] for i in xrange(0, argc) ]
229 finally:
230 if vptr is not None:
231 LocalFree(vptr)
232 return argv
233
242
243 CommandLineToArgv = GuessStringType(CommandLineToArgvA, CommandLineToArgvW)
244
245
246
247
248
249
250
251
252
253 -def ShellExecuteA(hwnd = None, lpOperation = None, lpFile = None, lpParameters = None, lpDirectory = None, nShowCmd = None):
254 _ShellExecuteA = windll.shell32.ShellExecuteA
255 _ShellExecuteA.argtypes = [HWND, LPSTR, LPSTR, LPSTR, LPSTR, INT]
256 _ShellExecuteA.restype = HINSTANCE
257
258 if not nShowCmd:
259 nShowCmd = 0
260 success = _ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
261 success = ctypes.cast(success, c_int)
262 success = success.value
263 if not success > 32:
264 raise ctypes.WinError(success)
265
266 -def ShellExecuteW(hwnd = None, lpOperation = None, lpFile = None, lpParameters = None, lpDirectory = None, nShowCmd = None):
267 _ShellExecuteW = windll.shell32.ShellExecuteW
268 _ShellExecuteW.argtypes = [HWND, LPWSTR, LPWSTR, LPWSTR, LPWSTR, INT]
269 _ShellExecuteW.restype = HINSTANCE
270
271 if not nShowCmd:
272 nShowCmd = 0
273 success = _ShellExecuteW(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)
274 success = ctypes.cast(success, c_int)
275 success = success.value
276 if not success > 32:
277 raise ctypes.WinError(success)
278
279 ShellExecute = GuessStringType(ShellExecuteA, ShellExecuteW)
280
281
282
283
285 if isinstance(lpExecInfo, SHELLEXECUTEINFOA):
286 ShellExecuteExA(lpExecInfo)
287 elif isinstance(lpExecInfo, SHELLEXECUTEINFOW):
288 ShellExecuteExW(lpExecInfo)
289 else:
290 raise TypeError("Expected SHELLEXECUTEINFOA or SHELLEXECUTEINFOW, got %s instead" % type(lpExecInfo))
291
293 _ShellExecuteExA = windll.shell32.ShellExecuteExA
294 _ShellExecuteExA.argtypes = [LPSHELLEXECUTEINFOA]
295 _ShellExecuteExA.restype = BOOL
296 _ShellExecuteExA.errcheck = RaiseIfZero
297 _ShellExecuteExA(byref(lpExecInfo))
298
300 _ShellExecuteExW = windll.shell32.ShellExecuteExW
301 _ShellExecuteExW.argtypes = [LPSHELLEXECUTEINFOW]
302 _ShellExecuteExW.restype = BOOL
303 _ShellExecuteExW.errcheck = RaiseIfZero
304 _ShellExecuteExW(byref(lpExecInfo))
305
306
307
308
309
310
312 _FindExecutableA = windll.shell32.FindExecutableA
313 _FindExecutableA.argtypes = [LPSTR, LPSTR, LPSTR]
314 _FindExecutableA.restype = HINSTANCE
315
316 lpResult = ctypes.create_string_buffer(MAX_PATH)
317 success = _FindExecutableA(lpFile, lpDirectory, lpResult)
318 success = ctypes.cast(success, ctypes.c_void_p)
319 success = success.value
320 if not success > 32:
321 raise ctypes.WinError(success)
322 return lpResult.value
323
325 _FindExecutableW = windll.shell32.FindExecutableW
326 _FindExecutableW.argtypes = [LPWSTR, LPWSTR, LPWSTR]
327 _FindExecutableW.restype = HINSTANCE
328
329 lpResult = ctypes.create_unicode_buffer(MAX_PATH)
330 success = _FindExecutableW(lpFile, lpDirectory, lpResult)
331 success = ctypes.cast(success, ctypes.c_void_p)
332 success = success.value
333 if not success > 32:
334 raise ctypes.WinError(success)
335 return lpResult.value
336
337 FindExecutable = GuessStringType(FindExecutableA, FindExecutableW)
338
339
340
341
342
343
344
345
347 _SHGetFolderPathA = windll.shell32.SHGetFolderPathA
348 _SHGetFolderPathA.argtypes = [HWND, ctypes.c_int, HANDLE, DWORD, LPSTR]
349 _SHGetFolderPathA.restype = HRESULT
350 _SHGetFolderPathA.errcheck = RaiseIfNotZero
351
352 pszPath = ctypes.create_string_buffer(MAX_PATH + 1)
353 _SHGetFolderPathA(None, nFolder, hToken, dwFlags, pszPath)
354 return pszPath.value
355
357 _SHGetFolderPathW = windll.shell32.SHGetFolderPathW
358 _SHGetFolderPathW.argtypes = [HWND, ctypes.c_int, HANDLE, DWORD, LPWSTR]
359 _SHGetFolderPathW.restype = HRESULT
360 _SHGetFolderPathW.errcheck = RaiseIfNotZero
361
362 pszPath = ctypes.create_unicode_buffer(MAX_PATH + 1)
363 _SHGetFolderPathW(None, nFolder, hToken, dwFlags, pszPath)
364 return pszPath.value
365
366 SHGetFolderPath = DefaultStringType(SHGetFolderPathA, SHGetFolderPathW)
367
368
370
371
372 _IsUserAnAdmin = windll.shell32.IsUserAnAdmin
373 _IsUserAnAdmin.argtypes = []
374 _IsUserAnAdmin.restype = bool
375 return _IsUserAnAdmin()
376
377
378
379 _all = set(vars().keys()).difference(_all)
380 __all__ = [_x for _x in _all if not _x.startswith('_')]
381 __all__.sort()
382
383