示例#1
0
    def __init__(self, atlasPath=None, direction=2):
        """Intialise the Atlas translation engine
        Args: atlasPath: a path to atlas; will try and figure it out from registry if not given
              direction: 1 = JP to ENG, 2 = ENG to JP
        """
        if atlasPath is None:
            try:
                atlasPath = findAtlasPath()
            except MissingAtlasException:
                print("Could not find ATLAS Translator")

        atlecont = win32api.LoadLibraryEx(
            os.path.join(atlasPath, "AtleCont.dll"), 0,
            LOAD_WITH_ALTERED_SEATCH_PATH)

        self.createEngine = createEngineType(
            win32api.GetProcAddress(atlecont, "CreateEngine"))
        self.destroyEngine = destroyEngineType(
            win32api.GetProcAddress(atlecont, "DestroyEngine"))
        self.translatePair = translatePairType(
            win32api.GetProcAddress(atlecont, "TranslatePair"))
        self.atlInitEngineData = atlInitEngineDataType(
            win32api.GetProcAddress(atlecont, "AtlInitEngineData"))
        self.freeAtlasData = freeAtlasDataType(
            win32api.GetProcAddress(atlecont, "FreeAtlasData"))

        intarray1 = intarrayType()
        intarray2 = intarrayType()
        genString = b'General'
        ret = self.atlInitEngineData(c_int(0), c_int(2), byref(intarray1),
                                     c_int(0), byref(intarray2))
        if ret != 0: raise AtlasInitErrorException()
        ret2 = self.createEngine(c_int(1), c_int(direction), c_int(0),
                                 c_char_p(genString))
        if ret2 != 1: raise AtlasInitErrorException()
示例#2
0
def loadDLL(dllName):

    dllPath = win32api.ExpandEnvironmentStrings(dllName)
    LOGGER.debug("Loading library {}".format(dllPath))
    dllHandle = win32api.LoadLibraryEx(dllPath, 0,
                                       win32con.LOAD_LIBRARY_AS_DATAFILE)
    return dllHandle
示例#3
0
def DisplayAsMime(grph, node, entity_ids_arr):
    """It receives as CGI arguments, the entity type which is "HttpUrl_MimeDocument", and the filename.
    It must then return the content of the file, with the right MIME type,"""

    file_name = entity_ids_arr[0]
    group_name = entity_ids_arr[1]

    # Using LoadLibrary (rather than CreateFile) is required otherwise
    # LoadResource, FindResource and others will fail
    hlib = win32api.LoadLibraryEx(file_name, 0, 2)

    try:
        group_name = int(group_name)
    except:
        pass

    # The destructor will remove the temporary file.
    obj_temp_file = IconToFile(hlib, group_name)

    rsrc_fil_nam = obj_temp_file.Name

    try:
        lib_util.copy_mime_file_to_output(mimeTypeResource, rsrc_fil_nam)
    except Exception as exc:
        logging.error("Copy rsrc_fil_nam=%s FAILED", rsrc_fil_nam)
        lib_common.ErrorMessageHtml(
            "DisplayAsMime rsrc_fil_nam=%s, caught:%s" %
            (rsrc_fil_nam, str(exc)))
示例#4
0
def DisplayAsMime(grph, node, entity_ids_arr):

    fileName = entity_ids_arr[0]
    groupName = entity_ids_arr[1]

    sys.stderr.write("fileName=%s groupName=%s\n" % (fileName, groupName))

    # Using LoadLibrary (rather than CreateFile) is required otherwise
    # LoadResource, FindResource and others will fail
    hlib = win32api.LoadLibraryEx(fileName, 0, 2)

    sys.stderr.write("fileName=%s groupName=%s\n" % (fileName, str(groupName)))
    try:
        groupName = int(groupName)
    except:
        pass

    # The destructor will remove the temporary file.
    objTempFile = IconToFile(hlib, groupName)

    rsrcFilNam = objTempFile.Name

    try:
        lib_util.CopyFile(mimeTypeResource, rsrcFilNam)
    except Exception:
        exc = sys.exc_info()[1]
        lib_common.ErrorMessageHtml(
            "DisplayAsMime rsrcFilNam=%s, mime_type=%s caught:%s" %
            (rsrcFilNam, mime_type, str(exc)))
示例#5
0
def decode(pathnm):
    h = win32api.LoadLibraryEx(pathnm, 0, LOAD_LIBRARY_AS_DATAFILE)
    nm = win32api.EnumResourceNames(h, RT_VERSION)[0]
    data = win32api.LoadResource(h, RT_VERSION, nm)
    vs = VSVersionInfo()
    j = vs.fromRaw(data)
    if TEST:
        print vs
        if data[:j] != vs.toRaw():
            print "AAAAAGGHHHH"
        txt = repr(vs)
        glbls = {}
        glbls['VSVersionInfo'] = VSVersionInfo
        glbls['FixedFileInfo'] = FixedFileInfo
        glbls['StringFileInfo'] = StringFileInfo
        glbls['StringTable'] = StringTable
        glbls['StringStruct'] = StringStruct
        glbls['VarFileInfo'] = VarFileInfo
        glbls['VarStruct'] = VarStruct
        vs2 = eval(txt + '\n', glbls)
        if vs.toRaw() != vs2.toRaw():
            print
            print 'reconstruction not the same!'
            print vs2
    win32api.FreeLibrary(h)
    return vs
def read_default_windows7_tasklist_xml():
    handle = win32api.LoadLibraryEx(
        "shell32.dll", None, win32con.LOAD_LIBRARY_AS_DATAFILE
        | win32con.DONT_RESOLVE_DLL_REFERENCES)
    #TODO: Is the resource-id (#21) same for all builds of Windows 7?
    xml = win32api.LoadResource(handle, "XML", 21)
    return xml
示例#7
0
def load_icon(module, index, as_data=False, size=ICON_SIZE):
    handle = win32api.LoadLibraryEx(module, 0, 0x20 | 0x2)
    try:
        ids = win32api.EnumResourceNames(handle, win32con.RT_GROUP_ICON)
        grp_id = -index if index < 0 else ids[index]
        data = win32api.LoadResource(handle, win32con.RT_GROUP_ICON, grp_id)
        pos = 0
        reserved, idtype, count = struct.unpack_from(b'<HHH', data, pos)
        pos += 6
        fmt = b'<BBBBHHIH'
        ssz = struct.calcsize(fmt)
        icons = []
        Icon = namedtuple('Icon', 'size width height id')
        while count > 0:
            count -= 1
            width, height, color_count, reserved, planes, bitcount, isize, icon_id = struct.unpack_from(
                fmt, data, pos)
            icons.append(Icon(isize, width, height, icon_id))
            pos += ssz
        # Unfortunately we cannot simply choose the icon with the largest
        # width/height as the width and height are not recorded for PNG images
        pixmaps = []
        for icon in icons:
            ans = read_icon(handle, icon)
            if ans is not None and not ans.isNull():
                pixmaps.append(ans)
        pixmaps.sort(key=lambda p: p.size().width(), reverse=True)
        pixmap = copy_to_size(pixmaps[0], size=size)
        if as_data:
            pixmap = pixmap_to_data(pixmap)
        return pixmap
    finally:
        win32api.FreeLibrary(handle)
示例#8
0
def CopyIcons(dstpath, srcpath):
    import os.path, string
    index = None
    try:
        srcpath, index = map(string.strip, string.split(srcpath, ','))
        index = int(index)
    except:
        pass
    print "I: PATH, INDEX", srcpath, index
    srcext = os.path.splitext(srcpath)[1]
    if string.lower(srcext) == '.ico':
        return CopyIcons_FromIco(dstpath, srcpath)
    if index is not None:
        print "I: Updating icons from", srcpath, ", %d to" % index, dstpath
    else:
        print "I: Updating icons from", srcpath, "to", dstpath
    import win32api  #, win32con
    hdst = win32api.BeginUpdateResource(dstpath, 0)
    hsrc = win32api.LoadLibraryEx(srcpath, 0, LOAD_LIBRARY_AS_DATAFILE)
    if index is None:
        grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[0]
    elif index >= 0:
        grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[index]
    else:
        grpname = -index
    data = win32api.LoadResource(hsrc, RT_GROUP_ICON, grpname)
    win32api.UpdateResource(hdst, RT_GROUP_ICON, grpname, data)
    for iconname in win32api.EnumResourceNames(hsrc, RT_ICON):
        data = win32api.LoadResource(hsrc, RT_ICON, iconname)
        win32api.UpdateResource(hdst, RT_ICON, iconname, data)
    win32api.FreeLibrary(hsrc)
    win32api.EndUpdateResource(hdst, 0)
示例#9
0
def decode(pathnm):
    h = win32api.LoadLibraryEx(pathnm, 0, LOAD_LIBRARY_AS_DATAFILE)
    nm = win32api.EnumResourceNames(h, pefile.RESOURCE_TYPE['RT_VERSION'])[0]
    data = win32api.LoadResource(h, pefile.RESOURCE_TYPE['RT_VERSION'], nm)
    vs = VSVersionInfo()
    j = vs.fromRaw(data)
    win32api.FreeLibrary(h)
    return vs
示例#10
0
def GetIconNamesList(pathName):
    # Using LoadLibrary (rather than CreateFile) is required otherwise
    # LoadResource, FindResource and others will fail
    hlib = win32api.LoadLibraryEx(pathName, 0, 2)

    # get icon groups, default is the first group
    icon_groups = win32api.EnumResourceNames(hlib, win32con.RT_GROUP_ICON)

    return icon_groups
示例#11
0
 def _GetModule(self):
     if not self._module:
         # Specify a full path to LoadLibraryEx to prevent
         # it from searching the path.
         input_file = os.path.abspath(self.input_file)
         _LOGGER.info('Loading input_file from "%s"', input_file)
         self._module = win32api.LoadLibraryEx(
             input_file, None, win32con.LOAD_LIBRARY_AS_DATAFILE)
     return self._module
示例#12
0
def ExtractIconFromExe(pathName, tmpDirName):
    # Using LoadLibrary (rather than CreateFile) is required otherwise
    # LoadResource, FindResource and others will fail
    hlib = win32api.LoadLibraryEx(pathName, 0, 2)

    # get icon groups, default is the first group
    icon_groups = win32api.EnumResourceNames(hlib, win32con.RT_GROUP_ICON)

    for group_name in icon_groups:
        IconToFile(hlib, tmpDirName, group_name)
示例#13
0
def CopyIcons(dstpath, srcpath):
    import os.path

    if type(srcpath) in StringTypes:
        srcpath = [srcpath]

    def splitter(s):
        try:
            srcpath, index = s.split(',')
            return srcpath.strip(), int(index)
        except ValueError:
            return s, None

    srcpath = list(map(splitter, srcpath))
    logger.info("SRCPATH %s", srcpath)

    if len(srcpath) > 1:
        # At the moment, we support multiple icons only from .ico files
        srcs = []
        for s in srcpath:
            e = os.path.splitext(s[0])[1]
            if e.lower() != '.ico':
                raise ValueError(
                    'Multiple icons supported only from .ico files')
            if s[1] is not None:
                raise ValueError('index not allowed for .ico files')
            srcs.append(s[0])
        return CopyIcons_FromIco(dstpath, srcs)

    srcpath, index = srcpath[0]
    srcext = os.path.splitext(srcpath)[1]
    if srcext.lower() == '.ico':
        return CopyIcons_FromIco(dstpath, [srcpath])
    if index is not None:
        logger.info("Updating icons from %s, %d to %s", srcpath, index,
                    dstpath)
    else:
        logger.info("Updating icons from %s to %s", srcpath, dstpath)
    import win32api  #, win32con
    hdst = win32api.BeginUpdateResource(dstpath, 0)
    hsrc = win32api.LoadLibraryEx(srcpath, 0, LOAD_LIBRARY_AS_DATAFILE)
    if index is None:
        grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[0]
    elif index >= 0:
        grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[index]
    else:
        grpname = -index
    data = win32api.LoadResource(hsrc, RT_GROUP_ICON, grpname)
    win32api.UpdateResource(hdst, RT_GROUP_ICON, grpname, data)
    for iconname in win32api.EnumResourceNames(hsrc, RT_ICON):
        data = win32api.LoadResource(hsrc, RT_ICON, iconname)
        win32api.UpdateResource(hdst, RT_ICON, iconname, data)
    win32api.FreeLibrary(hsrc)
    win32api.EndUpdateResource(hdst, 0)
示例#14
0
def get_manifest_from_dll(dll):
    import win32api, pywintypes
    LOAD_LIBRARY_AS_DATAFILE = 2
    d = win32api.LoadLibraryEx(os.path.abspath(dll), 0, LOAD_LIBRARY_AS_DATAFILE)
    try:
        resources = win32api.EnumResourceNames(d, 24)
    except pywintypes.error as err:
        if err.winerror == 1812:
            return None, None  # no resource section (probably a .pyd file)
        raise
    if resources:
        return resources[0], win32api.LoadResource(d, 24, resources[0])
    return None, None
示例#15
0
def FormatMessage(eventLogRecord, logType="Application"):
    """Given a tuple from ReadEventLog, and optionally where the event
    record came from, load the message, and process message inserts.

    Note that this function may raise win32api.error.  See also the
    function SafeFormatMessage which will return None if the message can
    not be processed.
    """

    # From the event log source name, we know the name of the registry
    # key to look under for the name of the message DLL that contains
    # the messages we need to extract with FormatMessage. So first get
    # the event log source name...
    keyName = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s" % (
        logType,
        eventLogRecord.SourceName,
    )

    # Now open this key and get the EventMessageFile value, which is
    # the name of the message DLL.
    handle = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName)
    try:
        dllNames = win32api.RegQueryValueEx(handle,
                                            "EventMessageFile")[0].split(";")
        # Win2k etc appear to allow multiple DLL names
        data = None
        for dllName in dllNames:
            try:
                # Expand environment variable strings in the message DLL path name,
                # in case any are there.
                dllName = win32api.ExpandEnvironmentStrings(dllName)

                dllHandle = win32api.LoadLibraryEx(
                    dllName, 0, win32con.LOAD_LIBRARY_AS_DATAFILE)
                try:
                    data = win32api.FormatMessageW(
                        win32con.FORMAT_MESSAGE_FROM_HMODULE,
                        dllHandle,
                        eventLogRecord.EventID,
                        langid,
                        eventLogRecord.StringInserts,
                    )
                finally:
                    win32api.FreeLibrary(dllHandle)
            except win32api.error:
                pass  # Not in this DLL - try the next
            if data is not None:
                break
    finally:
        win32api.RegCloseKey(handle)
    return data or ""  # Don't want "None" ever being returned.
示例#16
0
def getico(loc):
	#Better way?
	try:
		b=win32api.LoadLibraryEx (loc, 0, LOAD_LIBRARY_AS_DATAFILE)
		for e in win32api.EnumResourceNames (b, RT_ICON):
			data = win32api.LoadResource(b, RT_ICON, e)
			stream = StringIO.StringIO(ICON_HEADER + data)
			ir=Image.open(stream)
			if(ir.getextrema()[1] > 200):
				if(ir.size[-1] > B_SZ-10): #32
					break
	except:
		ir =Image.open(ICON_DEFAULT)
	return ir
示例#17
0
def GetResources(filename, types=None, names=None, languages=None):
    """
    Get resources from dll/exe file.

    types = a list of resource types to search for (None = all)
    names = a list of resource names to search for (None = all)
    languages = a list of resource languages to search for (None = all)
    Return a dict of the form {type_: {name: {language: data}}} which
    might also be empty if no matching resources were found.
    """
    hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
    res = _GetResources(hsrc, types, names, languages)
    win32api.FreeLibrary(hsrc)
    return res
示例#18
0
def GetIconNamesList(pathName):
    # Using LoadLibrary (rather than CreateFile) is required otherwise
    # LoadResource, FindResource and others will fail
	try:
		hlib = win32api.LoadLibraryEx(pathName, 0, 2)

		# get icon groups, default is the first group
		iconGroups = win32api.EnumResourceNames(hlib, win32con.RT_GROUP_ICON)
	except Exception:
		exc = sys.exc_info()[1]
		lib_common.ErrorMessageHtml("GetIconNamesList pathName=%s caught:%s" % ( pathName, str(exc) ) )

    # Strings and integers.
	return iconGroups
示例#19
0
def load_string_resource(pointer):
    """Resolve a @dllname,ordinal string resource pointer"""

    if pointer[0] != "@":
        return pointer

    resfile, resid = pointer[1:].split(",")
    resid = int(resid)

    hRes = Api.LoadLibraryEx(resfile, 0, Con.LOAD_LIBRARY_AS_DATAFILE)
    val = Api.LoadString(hRes, -resid, 1024)
    Api.FreeLibrary(hRes)

    return val.split('\x00', 1)[0]
示例#20
0
def GetIconNamesList(path_name):
    """For a Windows executable file, it returns the list of resource icons groups it contains."""

    # Using LoadLibrary (instead of CreateFile) is required otherwise LoadResource, FindResource and others will fail.
    try:
        hlib = win32api.LoadLibraryEx(path_name, 0, 2)

        # get icon groups, default is the first group
        icon_groups = win32api.EnumResourceNames(hlib, win32con.RT_GROUP_ICON)
    except Exception as exc:
        lib_common.ErrorMessageHtml("GetIconNamesList pathName=%s caught:%s" %
                                    (path_name, str(exc)))

    # Strings and integers.
    return icon_groups
def read_mui_string_from_dll(id):
    assert id.startswith("@") and ",-" in id, \
           "id has invalid format. Expected format is '@dllfilename,-id'"

    m = re.match("@([^,]+),-([0-9]+)(;.*)?", id)
    if m:
        dll_filename = _expand_path_variables(m.group(1))
        string_id = long(m.group(2))
    else:
        raise Exception("Error parsing dll-filename and string-resource-id from '%s'" % id)

    h = win32api.LoadLibraryEx(
        dll_filename,
        None,
        win32con.LOAD_LIBRARY_AS_DATAFILE | win32con.DONT_RESOLVE_DLL_REFERENCES)
    if h:
        s = win32api.LoadString(h, string_id)
        return s
    return None
def extract_strings_from_dll(dll_filename, output_filename):
    if os.path.isfile(output_filename):
        print "ERROR: File %s already exists." % output_filename
        return 0

    h = win32api.LoadLibraryEx(
        dll_filename,
        None,
        win32con.LOAD_LIBRARY_AS_DATAFILE | win32con.DONT_RESOLVE_DLL_REFERENCES)
    if not h:
        return 0

    dll_filename = os.path.basename(dll_filename)

    extracted = 0
    #strings = []

    with open(output_filename, "w+") as output_file:
        #for id in win32api.EnumResourceNames(h, win32con.RT_STRING):
        for id in range(0, 9999999):
            try:
                s = win32api.LoadString(h, id)
                if s == "%s":
                    continue
            except Exception, e:
                continue
            s = unicodedata.normalize(
                'NFKD',
                s
                ).encode('ascii', 'ignore').lower()

            if "&" in s:
                s = s.replace("&", "")
            #print id, s
            #strings.append((id, s))
            output_file.write("%d: %s\n" % (id, s))

            extracted += 1
示例#23
0
    def laod_resmodule(self, fpath):
        self._table = {}

        if _winapi:
            self._winhandle = win32api.LoadLibraryEx(fpath, 0,
                    win32con.LOAD_LIBRARY_AS_DATAFILE|
                    win32con.LOAD_WITH_ALTERED_SEARCH_PATH)
            if self._winhandle:
                return

        with open(fpath, "rb") as f:
            data = f.read()
            f.close()
        base = data[:]

        uint32 = struct.Struct("<L")
        uint16 = struct.Struct("<H")

        # MZ header
        if "MZ" <> data[:2]:
            raise Exception("")
        e_lfanew = uint32.unpack(data[60:64])[0]
        data = data[e_lfanew:]

        # PE header
        if "PE\0\0" <> data[:4]:
            raise Exception("")
        data = data[4:]
        number_of_section = uint16.unpack(data[2:4])[0]
        size_of_option_header = uint16.unpack(data[16:18])[0]
        data = data[20:]

        # Resource section
        res_addr_rva = uint32.unpack(data[112:116])[0]
        data = data[size_of_option_header:]
        res_size = 0
        res_addr = 0
        for _i in xrange(number_of_section):
            rva = uint32.unpack(data[12:16])[0]
            if ".rsrc" == data[:5] or res_addr_rva == rva:
                res_addr_rva = rva
                res_size = uint32.unpack(data[16:20])[0]
                res_addr = uint32.unpack(data[20:24])[0]
                break
            data = data[40:]
        if res_size == 0:
            raise Exception("")
        data = base[res_addr:]

        # IMAGE_RESOURCE_DIRECTORY (Frame 1)
        num_name = uint16.unpack(data[12:14])[0]
        num_id = uint16.unpack(data[14:16])[0]
        data = data[16:]
        for _i in xrange(num_name + num_id):
            # IMAGE_RESOURCE_DIRECTORY_ENTRY (Frame 1)
            w1 = uint32.unpack(data[:4])[0]
            w2 = uint32.unpack(data[4:8])[0]
            data = data[8:]
            name1 = self._res_name(base, res_addr, uint16, w1)
            if (w2 & 0x80000000) == 0:
                raise Exception("")

            # IMAGE_RESOURCE_DIRECTORY (Frame 2)
            data2 = base[(w2 & ~0x80000000) + res_addr:]
            num_name = uint16.unpack(data2[12:14])[0]
            num_id = uint16.unpack(data2[14:16])[0]
            data2 = data2[16:]
            for _j in xrange(num_name + num_id):
                # IMAGE_RESOURCE_DIRECTORY_ENTRY (Frame 2)
                w1 = uint32.unpack(data2[:4])[0]
                w2 = uint32.unpack(data2[4:8])[0]
                data2 = data2[8:]
                name2 = self._res_name(base, res_addr, uint16, w1)

                if (w2 & 0x80000000) == 0:
                    raise Exception("")

                # IMAGE_RESOURCE_DIRECTORY (Frame 3)
                data3 = base[(w2 & ~0x80000000) + res_addr:]
                num_name = uint16.unpack(data3[12:14])[0]
                num_id = uint16.unpack(data3[14:16])[0]
                data3 = data3[16:]
                if num_name + num_id < 1:
                    raise Exception("")

                # IMAGE_RESOURCE_DIRECTORY_ENTRY (Frame 3)
                # ignore w1
                w2 = uint32.unpack(data3[4:8])[0]
                if 0 <> (w2 & 0x80000000):
                    raise Exception("")

                # IMAGE_RESOURCE_DATA_ENTRY
                res = base[w2+res_addr:]
                offset_to_data = uint32.unpack(res[:4])[0] - res_addr_rva + res_addr
                size = uint32.unpack(res[4:8])[0]

                res_data = base[offset_to_data:offset_to_data+size]

                if not name1 in self._table:
                    self._table[name1] = {}
                self._table[name1][name2] = res_data
示例#24
0
def DispOneIcon(pathName, tmpDirName, group_name):
    # Using LoadLibrary (rather than CreateFile) is required otherwise
    # LoadResource, FindResource and others will fail
    hlib = win32api.LoadLibraryEx(pathName, 0, 2)

    return IconToFile(hlib, tmpDirName, group_name)
示例#25
0
    def load_lib(self):
        """
        Load the libOmexMeta C API binary. This methods incorporates
        flexibility to load libOmexMeta from multiple locations and works
        from the source, binary and install trees and under site-pacakges.
        Returns:

        """

        # todo note that we are currently using two different strategies for locating the library (see get_version())
        #    consolidate this code
        extensions = [
            f"-{get_version()}.dll", f'-{get_version()}.so.{get_version()}',
            f'.so.{get_version()}', f'.{get_version()}.dylib',
            f'-{get_version()}.dylib'
        ]
        # "" for windows, lib for linux
        prefixes = ["", "lib"]

        # when working directory is different from __file__
        current_working_dir = os.getcwd()

        # when in install tree we look this current directory
        pyomexmeta_init_dir = os.path.abspath(os.path.dirname(__file__))

        # when in the build tree we look in ../../lib
        build_tree_bin_dir = os.path.abspath(
            os.path.join(
                os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
                "bin"))
        build_tree_lib_dir = os.path.abspath(
            os.path.join(
                os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
                "lib"))

        search_directories = [
            current_working_dir, pyomexmeta_init_dir, build_tree_bin_dir,
            build_tree_lib_dir
        ] + _EXTRA_SEARCH_PATHS

        found_library_files = []
        candidates = []
        for direct in search_directories:
            for ex in extensions:
                for pre in prefixes:
                    cand = os.path.join(direct, f"{pre}OmexMetaCAPI{ex}")
                    candidates.append(cand)
                    if os.path.isfile(cand):
                        # print(f"Found library at {cand}")
                        found_library_files.append(cand)

        if not found_library_files:
            err = 'Cannot locate libOmexMeta library in any of the search locations:\n'
            for c in candidates:
                err += "\t" + c + "\n"
            raise FileNotFoundError(err)

        lib = None
        for lib_path in found_library_files:
            try:
                lib = ct.CDLL(lib_path)
            except Exception:
                dll_handle = win32api.LoadLibraryEx(
                    lib_path, 0, win32con.LOAD_WITH_ALTERED_SEARCH_PATH)
                lib = ct.WinDLL(lib_path, handle=dll_handle)
                continue
        if not lib:
            raise ValueError("Could not load library")

        return lib