SourceForge has been redesigned. Learn more.
Close

#377 Problem calling WMI method

open
nobody
None
5
2008-09-04
2008-08-15
No

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
ramong@cnmv.es
Comisión Nacional de Mercado de Valores

Discussion

  • Roger Upole

    Roger Upole - 2008-09-03

    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

     
  • Roger Upole

    Roger Upole - 2008-09-03
    • status: open --> pending
     
  • Ramon Garcia

    Ramon Garcia - 2008-09-04

    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).

     
  • Ramon Garcia

    Ramon Garcia - 2008-09-04
    • status: pending --> open
     
  • Roger Upole

    Roger Upole - 2008-09-04

    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.

     
  • Mark Hammond

    Mark Hammond - 2008-09-04

    Logged In: YES
    user_id=14198
    Originator: NO

    We should look inside the exception object if we don't already - but we still need to find a "well known" error code in it. I can't find an error definition for -2147217400, but if we can show it is used exclusively by WMI to mean "invalid param", then I'd be happy to add it to the list.

     
  • Ramon Garcia

    Ramon Garcia - 2008-09-04

    Logged In: YES
    user_id=2163452
    Originator: YES

    The constant HRESULT -2147217400 (hexadecimal 0x80041008) is WBEM_E_INVALID_PARAMETER "One of the parameters to the call is not correct"

    http://msdn.microsoft.com/en-us/library/aa394559.aspx

     
  • Mark Hammond

    Mark Hammond - 2008-09-04

    Logged In: YES
    user_id=14198
    Originator: NO

    Excellent - nice find - come to think of it, I didn't actually google, just looked locally in the platform sdk. If you can come up with a patch I'd be happy to check it in, otherwise it will need to wait until I get to it (but hopefully before the next release). Note I'd be happy for the patch to include a "local" definition of that error code - eg:

    WBEM_E_INVALID_PARAMETER = -2147217400

    at the top of the file would be fine (although a patch including all WBEM_ error codes in winerror or somewhere else suitable might be even better ;)

     
  • Ramon Garcia

    Ramon Garcia - 2008-09-07

    Logged In: YES
    user_id=2163452
    Originator: YES

    Thanks for the help. I will try to prepare a patch.