Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
WNetAddConnection2
Message
De
20/03/2002 11:33:01
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Divers
Thread ID:
00634984
Message ID:
00635013
Vues:
30
Maria,

I've written a wrapper which uses Christof Lange's struct class. I think you find it in the download section. I hope this gives you some ideas.
************************************************
PROCEDURE WNetAddConnection2
************************************************
*  Author............: Daniel Gramunt
*  Created...........: 22.09.2000  14:45:27
*) Description.......: Wrapper around the API function WNetAddConnection2().
*)                   : This program is using Christof Lange's struct class.
*)                   : Returns 0 if successful
*  Calling Samples...: NetAddConnect("\\MyServer\apps\", "K:", "UserId", "Password")
*  Parameter List....:
*  Major change list.:
*--------------------------------------------------------------------------------------------------
LPARAMETER tcNetworkName, tcDriveLetter, tcUserName, tcPassword

*--------------------------------------------------------------------------------------------------
*--  Bit flag for fdwConnection in WNetAddConnection2()
*--------------------------------------------------------------------------------------------------
#DEFINE CONNECT_UPDATE_PROFILE   0x00000001

*--------------------------------------------------------------------------------------------------
*--  Error values returned by WNetAddConnection2()
*--------------------------------------------------------------------------------------------------
#DEFINE NO_ERROR                              0
#DEFINE ERROR_ACCESS_DENIED                   5
#DEFINE ERROR_ALREADY_ASSIGNED               85
#DEFINE ERROR_BAD_DEV_TYPE                   66
#DEFINE ERROR_BAD_DEVICE                   1200
#DEFINE ERROR_BAD_NET_NAME                   67
#DEFINE ERROR_BAD_PROFILE                  1206
#DEFINE ERROR_BAD_PROVIDER                 1204
#DEFINE ERROR_BUSY                          170
#DEFINE ERROR_CANCELLED                    1223      
#DEFINE ERROR_CANNOT_OPEN_PROFILE          1205
#DEFINE ERROR_DEVICE_ALREADY_REMEMBERED    1202
#DEFINE ERROR_EXTENDED_ERROR               1208
#DEFINE ERROR_INVALID_PASSWORD               86
#DEFINE ERROR_NO_NET_OR_BAD_PATH           1203
#DEFINE ERROR_NO_NETWORK                   1222

*--------------------------------------------------------------------------------------------------
*-- NETRESOURCE structure: scope of enumeration (dwScope)
*--------------------------------------------------------------------------------------------------
#DEFINE RESOURCE_CONNECTED   0x00000001
#DEFINE RESOURCE_GLOBALNET   0x00000002
#DEFINE RESOURCE_REMEMBERED  0x00000003

*--------------------------------------------------------------------------------------------------
*-- NETRESOURCE structure: bitmask of resource type (dwType)
*--------------------------------------------------------------------------------------------------
#DEFINE RESOURCETYPE_ANY     0x00000000
#DEFINE RESOURCETYPE_DISK    0x00000001
#DEFINE RESOURCETYPE_PRINT    0x00000002

*--------------------------------------------------------------------------------------------------
*-- NETRESOURCE structure: display type (dwDisplayType)
*--------------------------------------------------------------------------------------------------
#DEFINE RESOURCEDISPLAYTYPE_GENERIC  0x00000000
#DEFINE RESOURCEDISPLAYTYPE_DOMAIN   0x00000001
#DEFINE RESOURCEDISPLAYTYPE_SERVER   0x00000002
#DEFINE RESOURCEDISPLAYTYPE_SHARE    0x00000003

*--------------------------------------------------------------------------------------------------
*-- NETRESOURCE structure: bitmask of resource usage (dwUsage)
*--------------------------------------------------------------------------------------------------
#DEFINE RESOURCEUSAGE_CONNECTABLE    0x00000001
#DEFINE RESOURCEUSAGE_CONTAINER      0x00000002

*-- check parameters
ASSERT TYPE("tcNetworkName") = "C" AND NOT EMPTY(tcNetworkName) 
ASSERT TYPE("tcDriveLetter") = "C" AND NOT EMPTY(tcDriveLetter)

tcDriveLetter = LEFT(UPPER(tcDriveLetter),1)

*-- make sure drive letter is between G and Z
IF NOT BETWEEN(ASC(tcDriveLetter), 71, 90)
   WAIT WINDOW NOWAIT "Drive letter must be between G and Z!"
   RETURN -1
ELSE
   *-- add colon to drive letter
   tcDriveLetter = tcDriveLetter + ":"
ENDIF

*-- Initialize optional parameters with .NULL. if not passed. 
*-- This will force WinNT/Win2K to use the current user/password
tcUserName = IIF(TYPE("tcUserName") = "C", tcUserName, .NULL.)
tcPassword = IIF(TYPE("tcPassword") = "C", tcPassword, .NULL.)

*-- declare API function
Declare Integer WNetAddConnection2 in Win32API ;
    String lpNetResource, ;
    String lpPassword, ;
    String lpUsername, ;
    Integer fdwConnection

*-- create structure
LOCAL loNetResource
SET CLASSLIB TO Struct ADDITIVE

loNetResource = CreateObject("NETRESOURCE")

With loNetResource 
   .dwScope        = RESOURCE_GLOBALNET
   .dwType         = RESOURCETYPE_DISK          && required by WNetAddConnection2
   .dwDisplayType  = 0
   .dwUsage        = RESOURCEUSAGE_CONNECTABLE
   .lpLocalName    = tcDriveLetter              && required by WNetAddConnection2
   .lpRemoteName   = tcNetworkName              && required by WNetAddConnection2
   .lpComment      = .NULL.
   .lpProvider     = .NULL.                     && required by WNetAddConnection2
EndWith

lpNetResource = loNetResource.GetString()
lpPassword    = tcPassword
lpUsername    = tcUsername
fdwConnection = CONNECT_UPDATE_PROFILE

*-- call API function
lnRetVal = WNetAddConnection2(lpNetResource, ;
                              lpPassword, ;
                              lpUsername, ;
                              fdwConnection)

RELEASE CLASSLIB Struct

RETURN lnRetVal
*-- EOF Function WNetAddConnection2 --------------------------------------------------------------------

DEFINE CLASS NETRESOURCE AS struct
dwScope        = 0
dwType         = 0       && required by WNetAddConnection2
dwDisplayType  = 0
dwUsage        = 0
lpLocalName    = .NULL.  && required by WNetAddConnection2
lpRemoteName   = .NULL.  && required by WNetAddConnection2
lpComment      = .NULL.
lpProvider     = .NULL.  && required by WNetAddConnection2

cMembers = "l:dwScope, l:dwType, l:dwDisplayType, l:dwUsage, "+;
           "pz:lpLocalName, pz:lpRemoteName, pz:lpComment, pz:lpProvider"
ENDDEFINE
>I am looking for a way to connect to a server and map a network drive by using a user's logon name and password. I found the WNetAddConnection2 function, but it requires the NETRESOURCE structure as a parameter. Is there any way to do this in Visual FoxPro (6 or 7) without writing a VB dll to make the call? Does anyone know of any other function that I could use in Visual FoxPro that would allow me to use the user's logon name and password?
>
>Any help would be greatly appreciated!
>
>Thanks,
>Maria
Daniel
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform