CTypes wnetaddconnection2

Thomas Heller theller at python.net
Tue May 27 07:28:41 EDT 2003


> 
> when i use:
> 
> WNetAddConnection2=windll.mpr.WNetAddConnection2A
> 
> err =WNetAddConnection2(pointer(NetRc),"PrivilegedUser","PrivilegedUserPassword",CONNECT_UPDATE_PROFILE)
> 
> print err
> 
> 
> it returns:2202
> 
> I think this is an error.

WNetAddConnection2 returns windows error codes.
ctypes has a WinError function:

>>> from ctypes import *
>>> raise WinError(2202)
>>> from ctypes import *
>>> raise WinError(2202)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
WindowsError: [Errno 2202] Der angegebene Benutzername ist unzulässig.
>>>

In english: The username is invalid.

Even more convenient is to use the restype attribute of the
WNetAddConnection2 function:

def check_error(code):
    if value == 0:
        return
    raise WinError(code)

WNetAddConnection2.restype = check_error

This would raise the error automatically.
See the ctypes documentation for details!

Thomas




More information about the Python-list mailing list