def LoadResource(hModule, type_, name, language): with pywin32error(): hrsrc = _kernel32._FindResourceEx(hModule, type_, name, language) size = _kernel32._SizeofResource(hModule, hrsrc) hglob = _kernel32._LoadResource(hModule, hrsrc) pointer = _kernel32._LockResource(hglob) return _common._PyBytes_FromStringAndSize(pointer, size)
def CredWrite(Credential, Flags=CRED_PRESERVE_CREDENTIAL_BLOB): """ Creates or updates a stored credential. Parameters ---------- Credential : dict Parameters to be passed to win32 API CredWrite/ Flags : int Always pass CRED_PRESERVE_CREDENTIAL_BLOB (i.e. 0). Returns ------- credentials : dict A dictionary containing the following: - Type: the type of credential (see MSDN) - TargetName: the target to use (string) - Persist: see MSDN - UserName: the retrieved username - CredentialBlob: the password (as a *string*, not an encoded binary stream - this function takes care of the encoding). - Comment: a string """ c_creds = _advapi32.CREDENTIAL.fromdict(Credential, Flags) c_pcreds = _advapi32.PCREDENTIAL(c_creds) with pywin32error(): _advapi32._CredWrite(c_pcreds, 0)
def EnumResourceLanguages(hModule, type_, name): resource_languages = [] def callback(hModule, type_name, res_name, language_id, param): resource_languages.append(language_id) return True with pywin32error(): _kernel32._EnumResourceLanguages( hModule, type_, name, _kernel32.ENUMRESLANGPROC(callback), 0) return resource_languages
def EnumResourceNames(hModule, type_): resource_names = [] def callback(hModule, type_, type_name, param): resource_names.append(type_name) return True with pywin32error(): _kernel32._EnumResourceNames( hModule, type_, _kernel32.ENUMRESNAMEPROC(callback), 0) return resource_names
def EnumResourceTypes(hModule): resource_types = [] def callback(hModule, type_, param): resource_types.append(type_) return True with pywin32error(): _kernel32._EnumResourceTypes( hModule, _kernel32.ENUMRESTYPEPROC(callback), 0) return resource_types
def CredWrite(credential, flag): """ Mimic pywin32 win32cred.CredWrite. Parameters ---------- credential: dict Dict of parameters to be passed to win32 API CredWrite flag: int Returns ------- credentials: dict A dictionary containing the following: - Type: the type of credential (see MSDN) - TargetName: the target to use (string) - Persist: see MSDN - UserName: the retrieved username - CredentialBlob: the password (as a *string*, not an encoded binary stream - this function takes care of the encoding). - Comment: a string """ unsupported = set(credential.keys()) - _advapi32.SUPPORTED_CREDKEYS if len(unsupported): raise ValueError("Unsupported keys: {0}".format(unsupported)) if flag != 0: raise ValueError("flag != 0 not yet supported") c_creds = _advapi32.CREDENTIAL() c_pcreds = ctypes.pointer(c_creds) ctypes.memset(c_pcreds, flag, ctypes.sizeof(c_creds)) for key in _advapi32.SUPPORTED_CREDKEYS: if key in credential: if key != 'CredentialBlob': setattr(c_creds, key, credential[key]) else: blob = _make_blob(credential['CredentialBlob']) blob_data = ctypes.create_unicode_buffer(blob) # Create_unicode_buffer adds a NULL at the end of the string # we do not want that. c_creds.CredentialBlobSize = \ ctypes.sizeof(blob_data) - ctypes.sizeof(ctypes.c_wchar) c_creds.CredentialBlob = ctypes.cast( blob_data, _common.LPBYTE) with pywin32error(): _advapi32._CredWrite(c_pcreds, 0)
def CredDelete(TargetName, Type): """ Remove the given target name from the stored credentials. Mimic pywin32 win32cred.CreadDelete. Parameters ---------- TargetName: str-like The target name to fetch from the keyring. """ if not Type == CRED_TYPE_GENERIC: raise ValueError("Type != CRED_TYPE_GENERIC not yet supported.") with pywin32error(): _advapi32._CredDelete(TargetName, Type, 0)
def CredDelete(TargetName, Type, Flags=0): """ Remove the given target name from the stored credentials. Parameters ---------- TargetName : unicode The target name to fetch from the keyring. Type : int One of the CRED_TYPE_* constants. Flags : int Reserved, always use 0. """ if not Type == CRED_TYPE_GENERIC: raise ValueError("Type != CRED_TYPE_GENERIC not yet supported.") with pywin32error(): _advapi32._CredDelete(TargetName, Type, 0)
def CredRead(TargetName, Type): """ Mimic pywin32 win32cred.CreadRead. Parameters ---------- TargetName: str-like The target name to fetch from the keyring. Returns ------- credentials: dict A dictionary containing the following: - UserName: the retrieved username - CredentialBlob: the password (as an utf-16 encoded 'string') None if the target name was not found. """ if not Type == CRED_TYPE_GENERIC: raise ValueError("Type != CRED_TYPE_GENERIC not yet supported") flag = 0 c_pcreds = _advapi32.PCREDENTIAL() with pywin32error(): _advapi32._CredRead(TargetName, Type, flag, ctypes.byref(c_pcreds)) try: c_creds = c_pcreds.contents credential = {} for key in _advapi32.SUPPORTED_CREDKEYS: if key != 'CredentialBlob': credential[key] = getattr(c_creds, key) else: blob = _common._PyBytes_FromStringAndSize( c_creds.CredentialBlob, c_creds.CredentialBlobSize) credential['CredentialBlob'] = blob return credential finally: _advapi32._CredFree(c_pcreds)
def CredRead(TargetName, Type, Flags=0): """ Retrieves a stored credential. Parameters ---------- TargetName : unicode The target name to fetch from the keyring. Type : int One of the CRED_TYPE_* constants. Flags : int Reserved, always use 0. Returns ------- credentials : dict A dictionary containing the following: - UserName: the retrieved username - CredentialBlob: the password (as an utf-16 encoded 'string') ``None`` if the target name was not found. """ if Type != CRED_TYPE_GENERIC: raise ValueError("Type != CRED_TYPE_GENERIC not yet supported") flag = 0 with pywin32error(): if _backend == 'cffi': ppcreds = _advapi32.PPCREDENTIAL() _advapi32._CredRead(TargetName, Type, flag, ppcreds) pcreds = _common.dereference(ppcreds) else: pcreds = _advapi32.PCREDENTIAL() _advapi32._CredRead( TargetName, Type, flag, _common.byreference(pcreds)) try: return _advapi32.credential2dict(_common.dereference(pcreds)) finally: _advapi32._CredFree(pcreds)
def FreeLibrary(hModule): with pywin32error(): return _kernel32._FreeLibrary(hModule)
def LoadLibraryEx(FileName, handle, flags): if not handle == 0: raise ValueError("handle != 0 not supported") with pywin32error(): return _kernel32._LoadLibraryEx(FileName, 0, flags)