dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: map network drive

  1. #1
    Michael C Guest

    map network drive


    I am using WNetUseConnection to map a network drive. It maps ok when I run
    it interactively. However, when I run the program as a windows 2000 service
    it fails to map the drive.


    Does anyone have an example of mapping drives in a windows nt or 2000 service?

    Thanks!

  2. #2
    Bob Butler Guest

    Re: map network drive

    "Michael C" <mcoward@lortobco.com> wrote in message
    news:3d454d1a$1@10.1.10.29
    > I am using WNetUseConnection to map a network drive. It maps ok when
    > I run it interactively. However, when I run the program as a
    > windows 2000 service it fails to map the drive.


    I assume you mean WNetAddConnection...

    Is the service running under the 'local system' account? If so, it has no
    network access. Try running under another user and/or the
    WNetAddConnection2 or WNetAddConnection3 calls where you can specify both
    username & password.




  3. #3
    Michael C. Guest

    Re: map network drive


    I followed your prescription. Now I get the following error when trying to
    map using a service.
    1312, ERROR_NO_SUCH_LOGON_SESSION, A specified logon session does not exist.
    I am running this service on my machine which is on the same domain as the
    drive I want to map to and does not require a password. I am also running
    it on another machine which is on a separate domain requiring a username
    and password.

    Is there a service that must run before the mapping can take place?


    Thanks for your time.




    "Bob Butler" <butlerbob@earthlink.net> wrote:
    >"Michael C" <mcoward@lortobco.com> wrote in message
    >news:3d454d1a$1@10.1.10.29
    >> I am using WNetUseConnection to map a network drive. It maps ok when
    >> I run it interactively. However, when I run the program as a
    >> windows 2000 service it fails to map the drive.

    >
    >I assume you mean WNetAddConnection...
    >
    >Is the service running under the 'local system' account? If so, it has

    no
    >network access. Try running under another user and/or the
    >WNetAddConnection2 or WNetAddConnection3 calls where you can specify both
    >username & password.
    >
    >
    >



  4. #4
    Automan Guest

    Re: map network drive


    "Michael C." <mcoward@lortobco.com> wrote:
    >
    >I followed your prescription. Now I get the following error when trying

    to
    >map using a service.
    >1312, ERROR_NO_SUCH_LOGON_SESSION, A specified logon session does not exist.
    > I am running this service on my machine which is on the same domain as

    the
    >drive I want to map to and does not require a password. I am also running
    >it on another machine which is on a separate domain requiring a username
    >and password.
    >
    >Is there a service that must run before the mapping can take place?
    >
    >
    >Thanks for your time.
    >
    >
    >
    >
    >"Bob Butler" <butlerbob@earthlink.net> wrote:
    >>"Michael C" <mcoward@lortobco.com> wrote in message
    >>news:3d454d1a$1@10.1.10.29
    >>> I am using WNetUseConnection to map a network drive. It maps ok when
    >>> I run it interactively. However, when I run the program as a
    >>> windows 2000 service it fails to map the drive.

    >>
    >>I assume you mean WNetAddConnection...
    >>
    >>Is the service running under the 'local system' account? If so, it has

    >no
    >>network access. Try running under another user and/or the
    >>WNetAddConnection2 or WNetAddConnection3 calls where you can specify both
    >>username & password.
    >>
    >>
    >>

    >


    Hehehe... I was working on this very code tonight, here is What I ended up
    with, I took the code from http://www.mvps.org/vbnet/index.html...upcallback.htm
    and from Microsoft's MSDN library and modified it to allow me to provide
    a password and username.


    Public Const NO_ERROR = 0
    Public Const CONNECT_UPDATE_PROFILE = &H1
    ' The following includes all the constants defined for NETRESOURCE,
    ' not just the ones used in this Module.
    Public Const RESOURCETYPE_DISK = &H1
    Public Const RESOURCETYPE_PRINT = &H2
    Public Const RESOURCETYPE_ANY = &H0
    Public Const RESOURCE_CONNECTED = &H1
    Public Const RESOURCE_REMEMBERED = &H3
    Public Const RESOURCE_GLOBALNET = &H2
    Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
    Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
    Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
    Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Public Const RESOURCEUSAGE_CONNECTABLE = &H1
    Public Const RESOURCEUSAGE_CONTAINER = &H2
    ' Error Constants:
    Public Const ERROR_ACCESS_DENIED = 5&
    Public Const ERROR_ALREADY_ASSIGNED = 85&
    Public Const ERROR_BAD_DEV_TYPE = 66&
    Public Const ERROR_BAD_DEVICE = 1200&
    Public Const ERROR_BAD_NET_NAME = 67&
    Public Const ERROR_BAD_PROFILE = 1206&
    Public Const ERROR_BAD_PROVIDER = 1204&
    Public Const ERROR_BUSY = 170&
    Public Const ERROR_CANCELLED = 1223&
    Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
    Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
    Public Const ERROR_EXTENDED_ERROR = 1208&
    Public Const ERROR_INVALID_PASSWORD = 86&
    Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&


    Private Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As String
    lpRemoteName As String
    lpComment As String
    lpProvider As String
    End Type


    Private Declare Function WNetAddConnection2 Lib "mpr" _
    Alias "WNetAddConnection2A" _
    (lpNetResource As NETRESOURCE, _
    ByVal lpPassword As String, _
    ByVal lpUserName As String, _
    ByVal dwFlags As Long) As Long


    '----------------------------------------------
    'WNetAddConnection2

    'Allows the caller to redirect (connect) a local
    'device to a network resource. It is similar to
    'WNetAddConnection, except that it takes a pointer
    'to a NETRESOURCE structure to describe the network
    'resource to connect to. It also takes the addition
    'parameters lpUserID and dwFlags.

    'lpNetResource
    'Specifies the network resource to connect to.
    'The following fields must be set when making a
    'connection, the others are ignored.

    ' lpRemoteName: Specifies the network resource
    ' to connect to. This is limited
    ' to MAX_PATH.

    ' lpLocalName: This specifies the name of a local
    ' device to be redirected, such as "F:"
    ' or "LPT1". The string is treated in a
    ' case insensitive manner, and may be
    ' the empty string (or NULL) in which
    ' case a connection to the network resource
    ' is made without making a redirection.

    ' lpProvider: Specifies the NP to connect to. If NULL
    ' or empty string, Windows will try each
    ' NP in turn. The caller should set
    ' lpProvider only if it knows for sure
    ' which network it wants. Otherwise, it
    ' is preferable to let Windows determine
    ' which NP the network name maps to.
    ' If this is non NULL, Windows will try
    ' the named NP and no other.

    ' dwType: Specifies the type of resource to connect to.
    ' It must be RESOURCETYPE_DISK or RESOURCETYPE_PRINT
    ' if lpLocalName is not the empty string. It may
    ' also be RESOURCETYPE_ANY if lpLocalName is the
    ' empty string.

    'lpPassword
    'Specifies the password to be used in making the
    'connection, normally the password associated with
    'lpUserID. A NULL value or string may be passed in
    'to indicate to the function to use the current
    'default password.
    '
    'lpUserID
    'This specifies the identity of the user needed to
    'make the connection. If NULL, a default will be
    'applied. This is used when the user wishes to connect
    'to a resource, but has a different user name or
    'account assigned to him for that resource. This
    'identification represents a security context, and
    'is NP specific.
    '
    'dwFlags
    'This is a bit mask which may have any of the
    'following bits set:
    '
    ' CONNECT_UPDATE_PROFILE: If the connection should
    ' be made persistent. If set,
    ' Windows automatically restores
    ' this connection when the user
    ' logs on to the network. A connection
    ' is only made persistent if the
    ' connection was successful.


    Private Declare Function WNetCancelConnection2 Lib "mpr" _
    Alias "WNetCancelConnection2A" _
    (ByVal lpName As String, _
    ByVal dwFlags As Long, _
    ByVal fForce As Long) As Long




    Public Function ConnectNextFreeNetworkDrive(sServer As String, _
    sUserID As String, sPassword As String) As String

    Dim NETR As NETRESOURCE
    Dim ErrInfo As Long
    Dim x As Long
    Dim testDrv As String

    'set the first drive as C (ASCII 67), and
    'then if unsuccessful try incrementing by
    'one until the last drive is reached.
    x = 67

    Do

    'increment drive by one
    '(making the first
    'drive to test D
    x = x + 1
    testDrv = Chr$(x) & ":"

    With NETR
    .dwScope = RESOURCE_GLOBALNET
    .dwType = RESOURCETYPE_DISK
    .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    .dwUsage = RESOURCEUSAGE_CONNECTABLE
    .lpRemoteName = sServer
    .lpLocalName = testDrv
    End With

    ErrInfo = WNetAddConnection2(NETR, _
    sPassword, _
    sUserID, _
    CONNECT_UPDATE_PROFILE)

    Loop Until x = 90 Or ErrInfo = NO_ERROR '90 = "z"


    'return the drive if successful
    If ErrInfo = NO_ERROR Then
    ConnectNextFreeNetworkDrive = testDrv
    Else: ConnectNextFreeNetworkDrive = "no dice"
    End If

    End Function


    Public Function ConnectThisNetworkDrive(sServer As String, _
    sDrv As String, _
    sUserID As String, _
    sPassword As String) As Boolean

    'attempts to connect to the passed network
    'connection to the specified drive.
    'ErrInfo=ERROR_SUCCESS if successful.

    Dim NETR As NETRESOURCE
    Dim ErrInfo As Long

    With NETR
    .dwScope = RESOURCE_GLOBALNET
    .dwType = RESOURCETYPE_DISK
    .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    .dwUsage = RESOURCEUSAGE_CONNECTABLE
    .lpRemoteName = sServer
    .lpLocalName = sDrv
    End With

    ErrInfo = WNetAddConnection2(NETR, _
    sPassword, _
    sUserID, _
    CONNECT_UPDATE_PROFILE)

    ConnectThisNetworkDrive = ErrInfo = ERROR_SUCCESS

    End Function


    Public Function DisconnectThisNetworkDrive(sDrv As String) As Boolean

    Dim ErrInfo As Long

    ErrInfo = WNetCancelConnection2(sDrv, _
    CONNECT_UPDATE_PROFILE, False)
    If ErrInfo = NO_ERROR Then
    'Disconnect was successfull
    DisconnectThisNetworkDrive = True
    Else
    'Disconnection Failed!
    DisconnectThisNetworkDrive = False
    End If


    End Function


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center