Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
WNetAddConnection2
Message
De
16/07/2001 14:56:46
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Divers
Thread ID:
00531144
Message ID:
00531154
Vues:
441
This message has been marked as the solution to the initial question of the thread.
Nadya,
WNetAddConnection2() needs a structure. I've written a wrapper which uses Christof Lange's struct class. I think you find it in the download section. Otherwise let me know and I can mail it to you.
I hope the following code gets you started.
*  Program...........: NetAddConnect
*  Author............: Daniel Gramunt
*  Created...........: 22.09.2000  14:45:27
*) Description.......: Wrapper to 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

*-- optional parameters
tcUserName = IIF(TYPE("tcUserName") = "C", tcUserName, "")
tcPassword = IIF(TYPE("tcPassword") = "C", tcPassword, "")

*-- 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 NetAddConnect --------------------------------------------------------------------

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
>Hi everyone,
>
>Could you please help me with the declaration and usage of WNetAddConnection2 function? I found Erik Moore sample in API section, but it's for WNetAddConnection function only.
>
>Thanks in advance.
Daniel
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform