CTypes wnetaddconnection2

Wiktor Sadowski art at wiktorsadowski.com
Mon May 26 00:45:51 EDT 2003


Matthias Janes wrote:
>>I have some scripts using the "Mark Hammond's win32all"
>>win32all-153.exe extention.
>>I would like for certain reason to use the CTypes module.
>>Could not figure out some things - maybe you could help me - with some
>>kind of hints.

Try the code below.
regards,
Wiktor Sadowski
http://www.wiktorsadowski.com

from ctypes import*

LPTSTR = c_char_p
DWORD = c_ulong

#winnetwk.h add more if needed
CONNECT_UPDATE_PROFILE=0x00000001
RESOURCE_CONNECTED=0x00000001
RESOURCE_GLOBALNET=0x00000002
RESOURCETYPE_ANY=0x00000000
RESOURCETYPE_DISK=0x00000001
RESOURCEDISPLAYTYPE_SHARE=0x00000003
RESOURCEDISPLAYTYPE_FILE=0x00000004
RESOURCEUSAGE_CONNECTABLE=0x00000001

#winerror.h
ErrInfo={
58:"ERROR_ACCESS_DENIED",
85:"ERROR_ALREADY_ASSIGNED",
66:"ERROR_BAD_DEV_TYPE",
1200:"ERROR_BAD_DEVICE",
67:"ERROR_BAD_NET_NAME",
1206:"ERROR_BAD_PROFILE",
1204:"ERROR_BAD_PROVIDER",
170:"ERROR_BUSY",
1223:"ERROR_CANCELLED",
1205:"ERROR_CANNOT_OPEN_PROFILE",
1202:"ERROR_DEVICE_ALREADY_REMEMBERED",
1208:"ERROR_EXTENDED_ERROR",
86:"ERROR_INVALID_PASSWORD",
1203:"ERROR_NO_NET_OR_BAD_PATH"
}

class NetRESOURCE(Structure):
           _fields_=[("dwScope",DWORD),
                        ("dwType",DWORD),
                        ("dwDisplayType",DWORD),
                        ("dwUsage",DWORD),
                        ("lpLocalName",LPTSTR),
                        ("lpRemoteName",LPTSTR),
                        ("lpComment",LPTSTR),
                        ("lpProvider",LPTSTR)]


IP = "10.199.61.3"
RemoteMachine = "\\\\" + IP

NetRc=NetRESOURCE()
NetRc.dwScope = RESOURCE_GLOBALNET
NetRc.dwType = RESOURCETYPE_ANY
NetRc.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetRc.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetRc.lpLocalName = "X:" #connect with no device (if undefined)
NetRc.lpRemoteName = RemoteMachine
#NetRc.lpComment = "optional"
#NetRc.lpProvider =    you can leave this undefined

WNetAddConnection2=windll.mpr.WNetAddConnection2A

err =
WNetAddConnection2(pointer(NetRc),"PrivilegedUser","PrivilegedUserPassword",
      CONNECT_UPDATE_PROFILE)
#winerror.h for possible errors

if err in ErrInfo.keys():
      print  ErrInfo[err]






More information about the Python-list mailing list