def GetUncPathOf(filePath): buf = create_string_buffer(1024) dwBufSize = DWORD(1024) err = WNetGetUniversalName(filePath, UNIVERSAL_NAME_INFO_LEVEL, buf, byref(dwBufSize)) if err == 0: return cast(buf, POINTER(UNIVERSAL_NAME_INFO)).contents.lpUniversalName elif err == ERROR_NOT_CONNECTED: pass else: print "GetUncPathOf Error:", err, FormatError(err) return filePath
def RunAs(filePath, asAdministrator, *args): sei = SHELLEXECUTEINFO() sei.cbSize = sizeof(SHELLEXECUTEINFO) sei.fMask = (SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS) if asAdministrator: sei.lpVerb = u"runas" else: sei.lpVerb = u"" sei.lpFile = GetUncPathOf(filePath) sei.lpParameters = " ".join( ['"%s"' % arg.replace('"', '""') for arg in args]) sei.nShow = SW_SHOWNORMAL if not ctypes.windll.shell32.ShellExecuteExW(byref(sei)): err = GetLastError() raise WindowsError(err, "ShellExecuteEx: %s" % FormatError(err)) return sei.hProcess