我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用win32api.GetModuleFileName()。
def RegisterCoreDLL(coredllName = None): """Registers the core DLL in the registry. If no params are passed, the name of the Python DLL used in the current process is used and registered. """ if coredllName is None: coredllName = win32api.GetModuleFileName(sys.dllhandle) # must exist! else: try: os.stat(coredllName) except os.error: print "Warning: Registering non-existant core DLL %s" % coredllName hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey()) try: win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName) finally: win32api.RegCloseKey(hKey) # Lastly, setup the current version to point to me. win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
def which_file(fname): """prints paths for fname where fname can be found, in case of .dll loads it""" files = [] path = win32api.GetEnvironmentVariable('PATH') # try paths as described in MSDN dirs = [os.getcwd(), win32api.GetSystemDirectory(), win32api.GetWindowsDirectory()] + path.split(';') dirs_norm = [] dirs_l = [] for d in dirs: dn = d.lower() if dn not in dirs_l: dirs_l.append(dn) dirs_norm.append(d) for d in dirs_norm: fname2 = os.path.join(d, fname) if os.path.exists(fname2): if fname2 not in files: files.append(fname2) if files: print('\n'.join([get_file_info(f) for f in files])) h = 0 if fname.lower().endswith('.dll'): print('\ttrying to load "%s" ...' % (fname)) try: h = win32api.LoadLibrary(fname) if h: dll_name = win32api.GetModuleFileName(h) print('\t%s loaded' % (dll_name)) except: print('\tCannot load "%s" !!!' % (fname))
def RegisterCoreDLL(coredllName = None): """Registers the core DLL in the registry. If no params are passed, the name of the Python DLL used in the current process is used and registered. """ if coredllName is None: coredllName = win32api.GetModuleFileName(sys.dllhandle) # must exist! else: try: os.stat(coredllName) except os.error: print("Warning: Registering non-existant core DLL %s" % coredllName) hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey()) try: win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName) finally: win32api.RegCloseKey(hKey) # Lastly, setup the current version to point to me. win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
def testHost(self): file = win32api.GetFullPathName(os.path.join(win32com.axscript.__path__[0], "test\\testHost.py")) cmd = '%s "%s"' % (win32api.GetModuleFileName(0), file) if verbose: print "Testing Python Scripting host" win32com.test.util.ExecuteShellCommand(cmd, self)
def testHost(self): file = win32api.GetFullPathName(os.path.join(win32com.axscript.__path__[0], "test\\testHost.py")) cmd = '%s "%s"' % (win32api.GetModuleFileName(0), file) if verbose: print("Testing Python Scripting host") win32com.test.util.ExecuteShellCommand(cmd, self)
def RegisterPythonServer(filename, progids=None, verbose=0): if progids: if isinstance(progids, str): progids = [progids] # we know the CLSIDs we need, but we might not be an admin user # and otherwise unable to register them. So as long as the progids # exist and the DLL points at our version, assume it already is. why_not = None for progid in progids: try: clsid = pythoncom.MakeIID(progid) except pythoncom.com_error: # no progid - not registered. break # have a CLSID - open it. try: HKCR = winreg.HKEY_CLASSES_ROOT hk = winreg.OpenKey(HKCR, "CLSID\\%s" % clsid) dll = winreg.QueryValue(hk, "InprocServer32") except WindowsError: # no CLSID or InProcServer32 - not good! break ok_files = [os.path.basename(pythoncom.__file__), 'pythoncomloader%d%d.dll' % (sys.version_info[0], sys.version_info[1])] if os.path.basename(dll) not in ok_files: why_not = "%r is registered against a different Python version (%s)" % (progid, dll) break else: #print "Skipping registration of '%s' - already registered" % filename return # needs registration - see if its likely! try: from win32com.shell.shell import IsUserAnAdmin except ImportError: print("Can't import win32com.shell - no idea if you are an admin or not?") is_admin = False else: try: is_admin = IsUserAnAdmin() except pythoncom.com_error: # old, less-secure OS - assume *is* admin. is_admin = True if not is_admin: msg = "%r isn't registered, but I'm not an administrator who can register it." % progids[0] if why_not: msg += "\n(registration check failed as %s)" % why_not # throw a normal "class not registered" exception - we don't report # them the same way as "real" errors. raise pythoncom.com_error(winerror.CO_E_CLASSSTRING, msg, None, -1) # so theoretically we are able to register it. cmd = '%s "%s" --unattended > nul 2>&1' % (win32api.GetModuleFileName(0), filename) if verbose: print("Registering engine", filename) # print cmd rc = os.system(cmd) if rc: print("Registration command was:") print(cmd) raise RuntimeError("Registration of engine '%s' failed" % filename)
def RegisterPythonServer(filename, progids=None, verbose=0): if progids: if isinstance(progids, basestring): progids = [progids] # we know the CLSIDs we need, but we might not be an admin user # and otherwise unable to register them. So as long as the progids # exist and the DLL points at our version, assume it already is. why_not = None for progid in progids: try: clsid = pythoncom.MakeIID(progid) except pythoncom.com_error: # no progid - not registered. break # have a CLSID - open it. try: HKCR = _winreg.HKEY_CLASSES_ROOT hk = _winreg.OpenKey(HKCR, "CLSID\\%s" % clsid) dll = _winreg.QueryValue(hk, "InprocServer32") except WindowsError: # no CLSID or InProcServer32 - not good! break ok_files = [os.path.basename(pythoncom.__file__), 'pythoncomloader%d%d.dll' % (sys.version_info[0], sys.version_info[1])] if os.path.basename(dll) not in ok_files: why_not = "%r is registered against a different Python version (%s)" % (progid, dll) break else: #print "Skipping registration of '%s' - already registered" % filename return # needs registration - see if its likely! try: from win32com.shell.shell import IsUserAnAdmin except ImportError: print "Can't import win32com.shell - no idea if you are an admin or not?" is_admin = False else: try: is_admin = IsUserAnAdmin() except pythoncom.com_error: # old, less-secure OS - assume *is* admin. is_admin = True if not is_admin: msg = "%r isn't registered, but I'm not an administrator who can register it." % progids[0] if why_not: msg += "\n(registration check failed as %s)" % why_not # throw a normal "class not registered" exception - we don't report # them the same way as "real" errors. raise pythoncom.com_error(winerror.CO_E_CLASSSTRING, msg, None, -1) # so theoretically we are able to register it. cmd = '%s "%s" --unattended > nul 2>&1' % (win32api.GetModuleFileName(0), filename) if verbose: print "Registering engine", filename # print cmd rc = os.system(cmd) if rc: print "Registration command was:" print cmd raise RuntimeError("Registration of engine '%s' failed" % filename)