[pywin32-bugs] [ pywin32-Bugs-2052253 ] Problem calling WMI method
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: SourceForge.net <no...@so...> - 2008-09-04 00:49:11
|
Bugs item #2052253, was opened at 2008-08-14 19:06 Message generated for change (Comment added) made by rupole You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=551954&aid=2052253&group_id=78018 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Ramon Garcia (ramong) Assigned to: Nobody/Anonymous (nobody) Summary: Problem calling WMI method Initial Comment: An exception is issued if a WMI method call is attempted. The following code shows it. wmiservice = Dispatch("WbemScripting.SWbemLocator") wmi = wmiservice.ConnectServer("localhost", "root\cimv2") moniker = r"winmgmts:" \ "{impersonationLevel=impersonate,(Security,Backup)}!\\\\" + \ computer + r"\root\cimv2" wmi = GetObject(moniker) log_files = wmi.ExecQuery \ ("SELECT * FROM Win32_NTEventLogFile") for log_file in log_files: base = re.sub("event$", "", log_file.FileName) backup_filename = base + u".evt" log_file.BackupEventlog(backup_filename) The following exception is raised File "c:\python25\lib\site-packages\win32com\client\dynamic.py", line 495, in __getattr__ raise pythoncom.com_error, details com_error: (-2147352567, 'Ocurri\xf3 una excepci\xf3n.', (0, u'SWbemObjectEx', u'Invalid parameter ', None, 0, -2147217400), None) We traced this problem to WMI issing an unexecpected return code when it is attempted to get a property that is a method. The function __getattr__ in class CDispatch tries to resolve an attribute request as an attribute first, and then, if an error is raisen by COM, as a method: if details[0] in ERRORS_BAD_CONTEXT: # May be a method. self._olerepr_.mapFuncs[attr] = retEntry return self._make_method_(attr) raise pythoncom.com_error, details But the list of such errors seems not be enough. WMI is returning error code winerror.DISP_E_EXCEPTION in this case. We workarounded the problem adding winerror.DISP_E_EXCEPTION to ERRORS_BAD_CONTEXT in dynamic.py, but perhaps there is a more specific solution. Best regards Ramon Garcia ra...@cn... Comisión Nacional de Mercado de Valores ---------------------------------------------------------------------- >Comment By: Roger Upole (rupole) Date: 2008-09-03 19:49 Message: Logged In: YES user_id=771074 Originator: NO The main problem is that there are any number of reasons why that particular error may be returned, and any number of error codes that might be returned when an object does not receive required parameters. No matter how specific you attempt to make it, it will still be too general. The best that can be done is to make a specific wrapper just for WMI objects. Also, as demonstrated WMI does not give you a way to actually retrieve a method object thru normal Invoke semantics. This is why it provides Methods_ and ExecMethod_ for dynamic languages such as Python. For example: log_file.Methods_('BackupEventlog') Tim Golden's WMI module uses these special methods to hide the details so that you can use natural syntax. I highly recommend it. ---------------------------------------------------------------------- Comment By: Ramon Garcia (ramong) Date: 2008-09-03 19:03 Message: Logged In: YES user_id=2163452 Originator: YES Thanks for looking at this issue. In the interest of easy of use I think that pythoncom should behave as close as posible to windows Wscript interpreter, because it is what the script developer expects. object.method(arg) would work like under WScript. The solution that we are using, adding winerror.DISP_E_EXCEPTION to ERRORS_BAD_CONTEXT is somewhat quick and dirty, but works for us. What I would do would be to add a more specific check, like checking that if winerror.DISP_E_EXCEPTION and the errorcode -2147217400 inside the wrapped exception object, and if that happens then make object.method return an object that can be called. The only risk that I can see in our method is the risk of breaking working code. Using a more specific solution, it should not have any risk. (Excuse me if there are any mistakes in this comment. I don't have the machine at hand, I and writting from what I can remember). ---------------------------------------------------------------------- Comment By: Roger Upole (rupole) Date: 2008-09-03 17:05 Message: Logged In: YES user_id=771074 Originator: NO I think the underlying problem here is that WMI does not make a distiction between asking for a method and executing it. Both of these produce the error you describe: log_file._oleobj_.Invoke(id, 0, pythoncom.DISPATCH_METHOD, True) log_file._oleobj_.Invoke(id, 0, pythoncom.DISPATCH_PROPERTYGET, True) And both succeed if you pass a filename parameter as an extra arg. This indicates that the error is not produced by attempting to get the attribute, but rather from actually calling the method without a filename parameter. You may want to look at using Tim Golden's WMI module, which provides a much easier to use wrapper for these objects. http://timgolden.me.uk/python/wmi.html ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=551954&aid=2052253&group_id=78018 |