I want my python script to read the windows service description, In order to decide whether to stop it \ make it run only manually..
The point is that I don't find the method that reads the Description.
(may be something like 'GetServiceDescription').
help anyone?
You can use wmi library also follow other answers like List running processes on 64-bit Windows.
i haven't tested this but this should be the short snippet to list them reference,
import wmi
c = wmi.WMI()
for service in c.Win32_Service(): #if its win32
print service.Name
Follow this tutorial
If you find it tough to follow may be looking at the tutorial may help or come again with another question.
Related
I want python to click Enter. However, I don't want to install any outside extensions. I want it to click enter without using them. I found many resources like win32 but I want to do it without using external resources. I don't mind which version of python it is on.
Is this possible?
If so, how?
I have looked on the web but couldn't find anything. Please, can someone help?
Thank you in advance.
Yes.
It should definitely be possible using the ctypes library to talk directly to the Windows dlls
If you don't want to install a helper module that has done all this for you, then you pretty much have to do it old school by going after the really low level code.
In short, call the SendInput function of user32.dll by using ctypes.windll.user32.SendInput. Good luck with getting the parameters correct - I don't have the patience to figure it out for you.
Here are the docs for the user32.dll SendInput API
Here is a helpful resource for figuring out the data types. This is actually part of ctypes and can be imported by import ctypes.wintypes but I find it instructional to read the actual code.
So for example, to create a WORD with the value of the Virtual Key Code VK_RETURN, I think it would be
>>> ctypes.wintypes.WORD(0x0D)
c_ushort(13)
But there is a lot more you have to piece together from there. Just build up your parameters and call the function.
Last hint, use the examples in the second link for how to build "C" type structures. Then build one yourself using ctypes.Structure
I will try to keep this question as tight as possible, but if it seems that I am saying insane things, it is almost certainly because I am ignorant of some key point, so please do correct me.
I am writing a program, in a Windows environment, that will interface with an existing application that has a COM interface to allow 3rd-party software to interact with it.
I have read all of the documentation for this application, and it says that there is a TLB file that defines the functions and data available via COM.
How do I use the TLB file with python? How do I discover the progID of the application so that I can interface with it (this isn't given in the documentation).
I'm pretty lost. I have a fair amount of experience with Python, but I am completely new to developing in a Windows environment. Any help would be enormously helpful. I have been reading all the documentation on win32com, but I still have no clue what to do, as no one addresses -- as far as I have seen -- bringing in a TLB file.
The questions asked is to link the custom TLB file with COM client to be developed in python. I have done a small example code for my COM server developed in C# and same is accessed by python client using "comtypes" package.
The below snippet of code gives:
import comtypes.client as CC
import comtypes
ccHandle = CC.CreateObject("CSharpServer.InterfaceImplementation")
print (ccHandle)
import comtypes.gen.CSharpServer as CS
InterfaceHandle = ccHandle.QueryInterface(CS.IManagedInterface)
print ("output of PrintHi function = ", InterfaceHandle.PrintHi("World"))
The above python script is for the C# COM server code available at http://msdn.microsoft.com/en-us/library/aa645738(v=vs.71).aspx (refer to the File 1: CSharpServer.cs).
OK, it's been a while since I've done this, and I'm not a COM expert by any means. Read the COM chapter from Python Programming on Windows to see how to do this. Follow along with the examples (trying things out against Excel) to get a feel of how things work.
First off, install the PyWin32 Extensions if you haven't already. This is the package that gives you pythonwin.exe and the COM interface modules. Get it from here.
Then you are going to open the "COM Makepy Utility" from PythonWin's Tools menu. Browse through the list of registered COM components (some will be typelibs, others DLLs) until you identify the one you have (you have to do a bit of detective work). Click OK to generate the Python glue code. You will then need to run it again with the -i command-line argmument to generate the boilerplate code so your python script can use this glue. Here's a paraphrase of the O'Reilly example for the Microsoft Excel Object library:
import win32com.client
from win32com.client import gencache
gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 2)
earlyBound = win32com.client.Dispatch("Excel.Application")
lateBound = win32com.client.dynamic.Dispatch("Excel.Application")
print earlyBound.ActiveCell()
Using early-bound objects is optional, but it does improve performance.
To find the ProgID is again a bit of detective work, although this answer seems to imply it's going to be hard. Try poking around the HKEY_CLASSES_ROOT hive of the registry with RegEdit to see if you see a ProgID that looks promising.
im willing to create a project same as JUNOS cli or cisco cli,
I came through gnu readline, but im confused as there are too many functions and methods to implement. any how i want a cli with auto complete using tab and space bar with question mark to display commands with help text.
I have two questions :
I have found code in python and perl but the im not use to python that code is complete and i just want to know if i should continue with python. im more experienced in perl but the code i found so far isn't complete for perl.
readline.parse_and_bind('tab: complete')
should i use the same complete function for both help and autocomplete feature. where as i have gone through another function
readline.set_completion_display_matches_hook(print_suggestions)
what you suggest :P im completely new to it!
I must say that I struggle to understand which specific point you are addressing in your question. But here is a suggestion anyway:
Start out with the cmd module. It gives you a nice little framework to build a command-line interpreter. It supports tab completion out of the box (provided readline is available). Start implementing your command-line interface. Once this stabilizes you can think about adding more comfort, e.g. tab completion for command arguments, help keys, and the like. This way, you have a working app to deal with, and can address readline details more specifically when you really need them. I wouldn't wade through the whole readline API upfront, if I were in your shoes.
We had to create a cli like JunOS/Cisco/VyOS and we built it on top of ishell, which uses readline for this job.
From the project page:
ishell helps you to easily create an interactive shell for your application. It supports command completion, dynamic arguments, a command history, and chaining of commands.
You can check the project at github: https://github.com/italorossi/ishell
Cisco example:
PS: I'm the author :).
I'm trying to extract some version information from a DLL using python. I read this question:
Python windows File Version attribute
It was helpful, but I also need to get the 'Assembly version' from the DLL. It's there when I right click and look on the versions tab, but not sure how I extract this with python.
On this page:
http://timgolden.me.uk/python/win32_how_do_i/get_dll_version.html
Tim Golden says:
You can use the slightly more messy
language-dependent code in the demos
which come with pywin32 to find the
strings in the box beneath it.
Can someone point me to the example that might be useful? I looked in the win32api directories but there's nothing obvious. Would I find a solution there?
If you would rather not introduce a dependency on Python.Net, you can also use the win32 api directly:
from win32api import GetFileVersionInfo, LOWORD, HIWORD
def get_version_number (filename):
info = GetFileVersionInfo (filename, "\\")
ms = info['FileVersionMS']
ls = info['FileVersionLS']
return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls)
Source: http://timgolden.me.uk/python/win32_how_do_i/get_dll_version.html
I'm not sure you can get at this information by using native code. The usual way of obtaining the assembly info is by running .Net code (e.g. C#). So I'm guessing in order to be able to do the same from python you'll need to run some .Net python interpreter. See for example http://pythonnet.github.io/
I've seen numerous ways of retrieving installed programs on WinXP+ in python. What is the proper and most robust way of doing this?
Currently I'm accessing HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall and reading each of the keys from there to get a list. (I've been told this isn't the proper way of doing things) I've seen examples of using WMI/Win32com to do this as well but have seen comments along with those implementations that WMI might be turned off on certain machines and that it's not a very reliable solution.
Is there a method which is both proper, and reliable to get a list of installed programs? None of the WMI examples I've seen have worked on this machine (hence my reluctance to use it, I'm only running WinFLP; which is a stripped vers of XP.)
I seem to have also found the TechNet article which my searches have turned up which is provided to a similar answer on my question: http://gallery.technet.microsoft.com/ScriptCenter/en-us/154dcae0-57a1-4c6e-8f9f-b215904485b7 Note that Vista/7 listed under Platforms very clearly says "Nope"...won't work. So the WMI deal seems like it's a no-go...
Being able to retrieve the installed path would be an upside as well, as right now my current code does not account for someone installing on another drive, or in a non-default directory.
The technet script you refer to perfectly works under Win 7 (with Python 2.5 32bits), and I really cannot see why it shouldn't.
Actually, the real weakness of the WMI approach is that it only lists products installed through the Windows Installer. So it's will not give you the full list. Many programs use different installers. Just compare the results between the (Select * from Win32_Product) and what is displayed in the Control Panel. So, unless you are sure that the program that interset you in your listing are installed with MSI, WMI is definitely not an answer.
So it may be not very pythonic, but the best way, as far as I know, is to use the registry as you've done. This is actually how the control panel works, so at least Windows considers it to be the most robust way to do it.
WMI is the correct way to look for installed programs as it will work across different versions of the OS and will be supported going forward. Looking for specific regkeys may work fine for specific versions of Windows but is not guaranteed to work in the future. Here is some simple python code to check for Box Sync which I just tried on Windows 7. Note that not all fields will be available for every product so be aware these will be 'None.'
import wmi
w = wmi.WMI()
for p in w.Win32_Product():
if 'Box, Inc.' == p.Vendor and p.Caption and 'Box Sync' in p.Caption:
print 'Installed {}'.format(p.Version)
The downside I have seen with WMI is it is very slow to start up.