def EnumResourceLanguages(hModule, lpType, lpName): """ List languages of a resource module. Parameters ---------- hModule : handle Handle to the resource module. lpType : str : int The type or id of resource to enumerate. lpName : str : int The type or id of resource to enumerate. Returns ------- resource_languages : list List of the resource language ids. """ resource_languages = [] def callback(hModule, type_name, res_name, language_id, param): resource_languages.append(language_id) return True with _pywin32error(): _resource._EnumResourceLanguages(hModule, lpType, lpName, _resource.ENUMRESLANGPROC(callback), 0) return resource_languages
def EnumResourceNames(hModule, resType): """ Enumerates all the resources of the specified type within a module. Parameters ---------- hModule : handle The handle to the module. resType : str : int The type or id of resource to enumerate. Returns ------- resource_names : list The list of resource names (unicode strings) of the specific resource type in the module. """ resource_names = [] def callback(hModule, type_, type_name, param): resource_names.append(type_name) return True with _pywin32error(): _resource._EnumResourceNames( hModule, resType, _resource.ENUMRESNAMEPROC(callback), 0) return resource_names
def LoadResource(hModule, type, name, language=LANG_NEUTRAL): """ Find and Load a resource component. Parameters ---------- handle : hModule The handle of the module containing the resource. Use None for current process executable. type : str : int The type of resource to load. name : str : int The name or Id of the resource to load. language : int Language to use, default is LANG_NEUTRAL. Returns ------- resource : bytes The byte string blob of the resource """ with _pywin32error(): hrsrc = _resource._FindResourceEx(hModule, type, name, language) size = _resource._SizeofResource(hModule, hrsrc) hglob = _resource._LoadResource(hModule, hrsrc) if _backend == 'ctypes': pointer = _common.cast( _resource._LockResource(hglob), _common.c_char_p) else: pointer = _resource._LockResource(hglob) return _common._PyBytes_FromStringAndSize(pointer, size)
def LoadLibraryEx(fileName, handle, flags): """ Loads the specified DLL, and returns the handle. Parameters ---------- fileName : unicode The filename of the module to load. handle : int Reserved, always zero. flags : int The action to be taken when loading the module. Returns ------- handle : hModule The handle of the loaded module See also -------- - `LoadLibraryEx MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx>`_ """ if not handle == 0: raise ValueError("handle != 0 not supported") with _pywin32error(): return _kernel32._LoadLibraryEx(fileName, 0, flags)
def EnumResourceNames(hModule, resType): """ Enumerates all the resources of the specified type within a module. Parameters ---------- hModule : handle The handle to the module. resType : str : int The type of resource to enumerate. If ``resType`` is a string starting with '#' is should be followed by the decimal number that define the integer resource type identifier. Returns ------- resource_names : list The list of resource names (unicode strings) of the specific resource type in the module. See also -------- - `EnumResourceNames MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648037(v=vs.85).aspx>`_ - `Predefined resource types <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648009(v=vs.85).aspx>`_ """ resource_names = [] def callback(hModule, type_, type_name, param): resource_names.append(type_name) return True with _pywin32error(): _kernel32._EnumResourceNames( hModule, resType, _kernel32.ENUMRESNAMEPROC(callback), 0) return resource_names
def EnumResourceLanguages(hModule, lpType, lpName): """ List languages of a resource module. Parameters ---------- hModule : handle Handle to the resource module. lpType : str : int The type or id of resource to enumerate. lpName : str : int The type or id of resource to enumerate. Returns ------- resource_languages : list List of the resource language ids. """ resource_languages = [] def callback(hModule, type_name, res_name, language_id, param): resource_languages.append(language_id) return True with _pywin32error(): _resource._EnumResourceLanguages( hModule, lpType, lpName, _resource.ENUMRESLANGPROC(callback), 0) return resource_languages
def LoadLibraryEx(fileName, handle, flags): """ Loads the specified DLL, and returns the handle. Parameters ---------- fileName : unicode The filename of the module to load. handle : Reserved, always zero. flags : The action to be taken when loading the module. Returns ------- hModule : The handle of the loaded module See also -------- - `LoadLibraryEx MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx>`_ """ if not handle == 0: raise ValueError("handle != 0 not supported") with _pywin32error(): return _kernel32._LoadLibraryEx(fileName, 0, flags)
def EnumResourceTypes(hModule): """ Enumerates resource types within a module. Parameters ---------- hModule : handle The handle to the module. Returns ------- resource_types : list The list of resource types in the module. See also -------- - `EnumResourceTypes MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648039(v=vs.85).aspx>`_ """ 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 EnumResourceNames(hModule, resType): """ Enumerates all the resources of the specified type within a module. Parameters ---------- hModule : handle The handle to the module. resType : str : int The type or id of resource to enumerate. Returns ------- resource_names : list The list of resource names (unicode strings) of the specific resource type in the module. """ resource_names = [] def callback(hModule, type_, type_name, param): resource_names.append(type_name) return True with _pywin32error(): _resource._EnumResourceNames(hModule, resType, _resource.ENUMRESNAMEPROC(callback), 0) return resource_names
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 EnumResourceNames(hModule, resType): """ Enumerates all the resources of the specified type within a module. Parameters ---------- hModule : handle The handle to the module. resType : str : int The type of resource to enumerate. If ``resType`` is a string starting with '#' is should be followed by the decimal number that define the integer resource type identifier. Returns ------- resource_names : list The list of resource names (unicode strings) of the specific resource type in the module. See also -------- - `EnumResourceNames MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648037(v=vs.85).aspx>`_ - `Predefined resource types <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648009(v=vs.85).aspx>`_ """ resource_names = [] def callback(hModule, type_, type_name, param): resource_names.append(type_name) return True with _pywin32error(): _kernel32._EnumResourceNames(hModule, resType, _kernel32.ENUMRESNAMEPROC(callback), 0) return resource_names
def LoadResource(hModule, type, name, language=LANG_NEUTRAL): """ Find and Load a resource component. Parameters ---------- handle : hModule The handle of the module containing the resource. Use None for current process executable. type : str : int The type of resource to load. name : str : int The name or Id of the resource to load. language : int Language to use, default is LANG_NEUTRAL. Returns ------- resource : bytes The byte string blob of the resource """ with _pywin32error(): hrsrc = _resource._FindResourceEx(hModule, type, name, language) size = _resource._SizeofResource(hModule, hrsrc) hglob = _resource._LoadResource(hModule, hrsrc) if _backend == 'ctypes': pointer = _common.cast(_resource._LockResource(hglob), _common.c_char_p) else: pointer = _resource._LockResource(hglob) return _common._PyBytes_FromStringAndSize(pointer, size)
def EnumResourceTypes(hModule): """ Enumerates resource types within a module. Parameters ---------- hModule : handle The handle to the module. Returns ------- resource_types : list The list of resource types in the module. See also -------- - `EnumResourceTypes MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648039(v=vs.85).aspx>`_ """ 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 GetSystemDirectory(): """ Get the ``System`` directory. Returns ------- result : str The path to the ``System`` directory. """ with _pywin32error(): # Note: pywin32 returns str on py27, unicode (which is str) on py3 return str(_system_information._GetSystemDirectory())
def FreeLibrary(hModule): """ Free the loaded dynamic-link library (DLL) module. If necessary, decrements its reference count. Parameters ---------- handle : hModule The handle to the library as returned by the LoadLibrary function. """ with _pywin32error(): return _dll._FreeLibrary(hModule)
def EndUpdateResource(handle, discard): """ End the update resource of the handle. Parameters ---------- handle : hModule The handle of the resource as it is returned by :func:`BeginUpdateResource` discard : bool When True all writes are discarded. """ with _pywin32error(): _resource._EndUpdateResource(handle, discard)
def CredWrite(Credential, Flags=CRED_PRESERVE_CREDENTIAL_BLOB): """ Creates or updates a stored credential. Parameters ---------- Credential : dict A dictionary corresponding to the PyWin32 ``PyCREDENTIAL`` structure. Flags : int Always pass ``CRED_PRESERVE_CREDENTIAL_BLOB`` (i.e. 0). """ c_creds = _authentication.CREDENTIAL.fromdict(Credential, Flags) c_pcreds = _authentication.PCREDENTIAL(c_creds) with _pywin32error(): _authentication._CredWrite(c_pcreds, 0)
def GetSystemDirectory(): """ Get the ``System`` directory. Returns ------- result : str The path to the ``System`` directory. See also -------- - `GetSystemDirectory MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms724373(v=vs.85).aspx>`_ """ with _pywin32error(): # Note: pywin32 returns str on py27, unicode (which is str) on py3 return str(_kernel32._GetSystemDirectory())
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(): _authentication._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 BeginUpdateResource(filename, delete): """ Get a handle that can be used by the :func:`UpdateResource`. Parameters ---------- fileName : unicode The filename of the module to load. delete : bool When true all existing resources are deleted Returns ------- result : hModule Handle of the resource. """ with _pywin32error(): return _resource._BeginUpdateResource(filename, delete)
def EndUpdateResource(handle, discard): """ End the update resource of the handle. Parameters ---------- handle : hModule The handle of the resource as it is returned by :func:`BeginUpdateResource` discard : bool When True all writes are discarded. See also -------- - `EndUpdateResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648032(v=vs.85).aspx>`_ """ with _pywin32error(): _kernel32._EndUpdateResource(handle, discard)
def LoadResource(hModule, type, name, language=LANG_NEUTRAL): """ Find and Load a resource component. Parameters ---------- handle : hModule The handle of the module containing the resource. Use None for current process executable. type : str : int The type of resource to load. name : str : int The name or Id of the resource to load. language : int Language to use, default is LANG_NEUTRAL. Returns ------- resource : bytes The byte string blob of the resource See also -------- - `FindResourceEx MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648043(v=vs.85).aspx>`_ - `SizeofResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648048(v=vs.85).aspx>`_ - `LoadResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648046(v=vs.85).aspx>`_ - `LockResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648047(v=vs.85).aspx>`_ - `Predefined resource types <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648009(v=vs.85).aspx>`_ - `Predefined resource language ids <https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx>`_ """ with _pywin32error(): hrsrc = _kernel32._FindResourceEx(hModule, type, name, language) size = _kernel32._SizeofResource(hModule, hrsrc) hglob = _kernel32._LoadResource(hModule, hrsrc) if _backend == 'ctypes': pointer = _common.cast(_kernel32._LockResource(hglob), _common.c_char_p) else: pointer = _kernel32._LockResource(hglob) return _common._PyBytes_FromStringAndSize(pointer, size)
def EnumResourceLanguages(hModule, lpType, lpName): """ List languages of a resource module. Parameters ---------- hModule : handle Handle to the resource module. lpType : str : int The type of resource to enumerate. If ``lpType`` is a string starting with '#', it should be followed by the decimal number that define the integer resource type identifier. lpName : str : int The name of resource to enumerate. If ``lpType`` is a string starting with '#', it should be followed by the decimal number that define the integer resource type identifier. Returns ------- resource_languages : list List of the resource language ids. See also -------- - `EnumResourceLanguages MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648035(v=vs.85).aspx>`_ - `Predefined resource types <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648009(v=vs.85).aspx>`_ - `Predefined resource language ids <https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx>`_ """ resource_languages = [] def callback(hModule, type_name, res_name, language_id, param): resource_languages.append(language_id) return True with _pywin32error(): _kernel32._EnumResourceLanguages(hModule, lpType, lpName, _kernel32.ENUMRESLANGPROC(callback), 0) return resource_languages
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 ``None`` if the target name was not found or a dictionary containing the following: - UserName: the retrieved username - CredentialBlob: the password (as an utf-16 encoded 'string') """ 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 EnumResourceLanguages(hModule, lpType, lpName): """ List languages of a resource module. Parameters ---------- hModule : handle Handle to the resource module. lpType : str : int The type of resource to enumerate. If ``lpType`` is a string starting with '#' is should be followed by the decimal number that define the integer resource type identifier. lpName : str : int The name of resource to enumerate. If ``lpType`` is a string starting with '#' is should be followed by the decimal number that define the integer resource type identifier. Returns ------- resource_languages : list List of the resource language ids. See also -------- - `EnumResourceLanguages MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648035(v=vs.85).aspx>`_ - `Predefined resource types <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648009(v=vs.85).aspx>`_ - `Predefined resource language ids <https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx>`_ """ resource_languages = [] def callback(hModule, type_name, res_name, language_id, param): resource_languages.append(language_id) return True with _pywin32error(): _kernel32._EnumResourceLanguages( hModule, lpType, lpName, _kernel32.ENUMRESLANGPROC(callback), 0) return resource_languages
def BeginUpdateResource(filename, delete): """ Get a handle that can be used by the :func:`UpdateResource`. Parameters ---------- fileName : unicode The filename of the module to load. delete : bool When true all existing resources are deleted Returns ------- result : hModule Handle of the resource. See also -------- - `BeginUpdateResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648030(v=vs.85).aspx>`_ """ with _pywin32error(): return _kernel32._BeginUpdateResource(filename, delete)
def UpdateResource(handle, type, name, data, language=LANG_NEUTRAL): """ Update a resource. Parameters ---------- handle : hModule The handle of the resource file as returned by :func:`BeginUpdateResource`. type : str : int The type of resource to update. name : str : int The name or Id of the resource to update. data : bytes A bytes like object is expected. .. note:: PyWin32 version 219, on Python 2.7, can handle unicode inputs. However, the data are stored as bytes and it is not really possible to convert the information back into the original unicode string. To be consistent with the Python 3 behaviour of PyWin32, we raise an error if the input cannot be converted to `bytes`. language : int Language to use, default is LANG_NEUTRAL. """ with _pywin32error(): try: lp_data = bytes(data) except UnicodeEncodeError: raise TypeError( "a bytes-like object is required, not a 'unicode'") _resource._UpdateResource( handle, type, name, language, lp_data, len(lp_data))
def UpdateResource(handle, type, name, data, language=LANG_NEUTRAL): """ Update a resource. Parameters ---------- handle : hModule The handle of the resource file as returned by :func:`BeginUpdateResource`. type : str : int The type of resource to update. name : str : int The name or Id of the resource to update. data : bytes A bytes like object is expected. language : int Language to use, default is LANG_NEUTRAL. See also -------- - `UpdateResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648049(v=vs.85).aspx>`_ """ with _pywin32error(): try: lp_data = bytes(data) except UnicodeEncodeError: # FIXME: In python 2.7 pipywin32219 can handle unicode. # However the data are stored as bytes and it # is not really possible to convert the information # back into the original unicode string. This looks # like a bug so we follow the python 3 behavior. raise TypeError("a bytes-like object is required, not a 'unicode'") _kernel32._UpdateResource(handle, type, name, language, lp_data, len(lp_data))
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 ``None`` if the target name was not found or A dictionary corresponding to the PyWin32 ``PyCREDENTIAL`` structure. """ if Type != CRED_TYPE_GENERIC: raise ValueError("Type != CRED_TYPE_GENERIC not yet supported") flag = 0 with _pywin32error(): if _backend == 'cffi': ppcreds = _authentication.PPCREDENTIAL() _authentication._CredRead(TargetName, Type, flag, ppcreds) pcreds = _common.dereference(ppcreds) else: pcreds = _authentication.PCREDENTIAL() _authentication._CredRead( TargetName, Type, flag, _common.byreference(pcreds)) try: return _authentication.credential2dict(_common.dereference(pcreds)) finally: _authentication._CredFree(pcreds)
def LoadLibraryEx(fileName, handle, flags): """ Loads the specified DLL, and returns the handle. Parameters ---------- fileName : unicode The filename of the module to load. handle : int Reserved, always zero. flags : int The action to be taken when loading the module. Returns ------- handle : hModule The handle of the loaded module """ if not handle == 0: raise ValueError("handle != 0 not supported") with _pywin32error(): return _dll._LoadLibraryEx(fileName, 0, flags)
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 ``None`` if the target name was not found or A dictionary corresponding to the PyWin32 ``PyCREDENTIAL`` structure. """ if Type != CRED_TYPE_GENERIC: raise ValueError("Type != CRED_TYPE_GENERIC not yet supported") flag = 0 with _pywin32error(): if _backend == 'cffi': ppcreds = _authentication.PPCREDENTIAL() _authentication._CredRead(TargetName, Type, flag, ppcreds) pcreds = _common.dereference(ppcreds) else: pcreds = _authentication.PCREDENTIAL() _authentication._CredRead(TargetName, Type, flag, _common.byreference(pcreds)) try: return _authentication.credential2dict(_common.dereference(pcreds)) finally: _authentication._CredFree(pcreds)
def LoadResource(hModule, type, name, language=LANG_NEUTRAL): """ Find and Load a resource component. Parameters ---------- handle : The handle of the module containing the resource. Use None for currrent process executable. type : str : int The type of resource to load. name : The name or Id of the resource to load. language : int Language to use, default is LANG_NEUTRAL. Returns ------- hModule : Handle of the loaded source. See also -------- - `FindResourceEx MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648043(v=vs.85).aspx>`_ - `SizeofResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648048(v=vs.85).aspx>`_ - `LoadResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648046(v=vs.85).aspx>`_ - `LockResource MSDN reference <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648047(v=vs.85).aspx>`_ - `Predefined resource types <https://msdn.microsoft.com/en-us/library/windows/desktop/ms648009(v=vs.85).aspx>`_ - `Predefined resource language ids <https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx>`_ """ 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 EnumResourceTypes(hModule): """ Enumerates resource types within a module. Parameters ---------- hModule : handle The handle to the module. Returns ------- resource_types : list The list of resource types in the module. """ resource_types = [] def callback(hModule, type_, param): resource_types.append(type_) return True with _pywin32error(): _resource._EnumResourceTypes(hModule, _resource.ENUMRESTYPEPROC(callback), 0) return resource_types
def EnumResourceTypes(hModule): """ Enumerates resource types within a module. Parameters ---------- hModule : handle The handle to the module. Returns ------- resource_types : list The list of resource types in the module. """ resource_types = [] def callback(hModule, type_, param): resource_types.append(type_) return True with _pywin32error(): _resource._EnumResourceTypes( hModule, _resource.ENUMRESTYPEPROC(callback), 0) return resource_types