Exemplo n.º 1
0
def TestConnection():
    if len(possible_shares) == 0:
        print("Couldn't find any potential shares to connect to")
        return
    localName = findUnusedDriveLetter() + ':'
    for share in possible_shares:
        print("Attempting connection of", localName, "to", share.lpRemoteName)
        try:
            win32wnet.WNetAddConnection2(share.dwType, localName,
                                         share.lpRemoteName)
        except win32wnet.error as details:
            print("Couldn't connect: " + details.strerror)
            continue
        # Have a connection.
        try:
            fname = os.path.join(localName + "\\",
                                 os.listdir(localName + "\\")[0])
            try:
                print("Universal name of '%s' is '%s'" %
                      (fname, win32wnet.WNetGetUniversalName(fname)))
            except win32wnet.error as details:
                print("Couldn't get universal name of '%s': %s" %
                      (fname, details.strerror))
            print("User name for this connection is",
                  win32wnet.WNetGetUser(localName))
        finally:
            win32wnet.WNetCancelConnection2(localName, 0, 0)
        # and do it again, but this time by using the more modern
        # NETRESOURCE way.
        nr = win32wnet.NETRESOURCE()
        nr.dwType = share.dwType
        nr.lpLocalName = localName
        nr.lpRemoteName = share.lpRemoteName
        win32wnet.WNetAddConnection2(nr)
        win32wnet.WNetCancelConnection2(localName, 0, 0)

        # and one more time using WNetAddConnection3
        win32wnet.WNetAddConnection3(0, nr)
        win32wnet.WNetCancelConnection2(localName, 0, 0)

        # Only do the first share that succeeds.
        break
Exemplo n.º 2
0
                    fname, details.strerror)
            print "User name for this connection is", win32wnet.WNetGetUser(
                localName)
        finally:
            win32wnet.WNetCancelConnection2(localName, 0, 0)
        # and do it again, but this time by using the more modern
        # NETRESOURCE way.
        nr = win32wnet.NETRESOURCE()
        nr.dwType = share.dwType
        nr.lpLocalName = localName
        nr.lpRemoteName = share.lpRemoteName
        win32wnet.WNetAddConnection2(nr)
        win32wnet.WNetCancelConnection2(localName, 0, 0)

        # and one more time using WNetAddConnection3
        win32wnet.WNetAddConnection3(0, nr)
        win32wnet.WNetCancelConnection2(localName, 0, 0)

        # Only do the first share that succeeds.
        break


def TestGetUser():
    u = win32wnet.WNetGetUser()
    print "Current global user is", repr(u)
    if u != win32wnet.WNetGetUser(None):
        raise RuntimeError("Default value didnt seem to work!")


TestGetUser()
TestOpenEnum()