Results 1 to 5 of 5

Thread: SendKeys Help!

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Gainesville, Ga
    Posts
    50

    Unhappy

    I'm having a problem ,
    using vb5 I'm trying to map a network drive
    using explorer. It works fine until the
    (Map Network Drive) window opens then that
    window doesn't recognize the commands.
    The only thing I can think of is the problem
    is that' That window is not linked to an .exe file.

    -

    Private Sub Form_Load()
    Dim i
    i = Shell("c:\windows\explorer.exe", 3) ' 3=maximized

    SendKeys "%TN", True ' Opens the (Map)
    ' window fine


    SendKeys "%P", True
    OR ' Neather one of these work to change focus
    SendKeys "{TAB}", True


    End

    End Sub
    -
    -
    Also I haven't figured out how to send
    three held down keys such as Crtl+Alt+Del
    to shut down or reboot.
    Scott Cato
    VB6s

  2. #2
    Guest
    Sendkeys is the last thing you want to use. You usually use api before anything. To shutdown or reboot your computer, refer to this link:

    http://www.vbsquare.com/tips/tip179.html

    And to map a network drive:

    Code:
    Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As
    String, ByVal lpszLocalName As String) As Long
    
    
    'Code:
    Dim strLocalDriveLetter As String
    Dim strPassword As String
    Dim strNetworkPathName As String
    
    strLocalDriveLetter = "K:"                  'Local drive letter to be mapped
    strPassword = ""                            'specify network password if required
    strNetworkPathName = "\\NETWORKPATH\VOL1"   'path to network drive
    
    If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
    MsgBox ("An Error occurred mapping the drive")
    Else
    MsgBox ("Drive successfully mapped!")
    End If
    [Edited by Matthew Gates on 06-29-2000 at 02:39 AM]

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Gainesville, Ga
    Posts
    50

    Question Hung up

    The code Matthew sent is great and I'm sure
    thats the best way, but I just want to know
    more about SendKeys.

    Why won't the focus change in the Map window.
    Private Sub Form_Load()

    Code:
    s = Shell("c:\windows\explorer.exe")  ' 3=maximized
    
      For I = 1 To 10000                  ' delay
        For j = 1 To 10                   ' delay
        Next j                            ' delay
      Next I                              ' delay
      
    SendKeys "%TN", True                  '  %=alt
    
      For I = 1 To 10000                  ' delay
        For j = 1 To 10                   ' delay
        Next j                            ' delay
      Next I                              ' delay
      
    AppActivate "Map Network Drive"
    SendKeys "%P"                         ' "{TAB}"
    
        
    End
    
    End Sub
    [Edited by catocom on 07-02-2000 at 10:30 PM]
    Scott Cato
    VB6s

  4. #4
    Guest
    Code:
    Connect/Disconnect shared network drives 
    
    Option Explicit
    
    Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) As Long
    
    Const WN_Success = &H0
    Const WN_Not_Supported = &H1
    Const WN_Net_Error = &H2
    Const WN_Bad_Pointer = &H4
    Const WN_Bad_NetName = &H32
    Const WN_Bad_Password = &H6
    Const WN_Bad_Localname = &H33
    Const WN_Access_Denied = &H7
    Const WN_Out_Of_Memory = &HB
    Const WN_Already_Connected = &H34
    
    '-- Error number and message
    Public ErrorNum         As Long
    Public ErrorMsg         As String
    
    Public rc               As Long
    
    Private Const ERROR_NO_CONNECTION = 8
    Private Const ERROR_NO_DISCONNECT = 9
    
    
    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
    
    '----------------------------------------------
    '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 WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
    Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
    Private Declare Function WNetConnectionDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As Long
    Private Declare Function WNetDisconnectDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As Long
    
    'Public Const RESOURCE_CONNECTED = &H1
    'Public Const RESOURCE_REMEMBERED = &H3
    'Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
    'Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
    'Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
    'Public Const RESOURCEUSAGE_CONTAINER = &H2
    
    Const NO_ERROR = 0
    Const CONNECT_UPDATE_PROFILE = &H1
    Const RESOURCETYPE_DISK = &H1
    Const RESOURCETYPE_PRINT = &H2
    Const RESOURCETYPE_ANY = &H0
    Const RESOURCE_GLOBALNET = &H2
    Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Const RESOURCEUSAGE_CONNECTABLE = &H1
    
    Public Sub Connect(sDrive As String, sService As String, Optional sPassword As String = "")
    
       On Error GoTo Err_Connect
       Me.ErrorNum = 0
       Me.ErrorMsg = ""
       rc = WNetAddConnection(sService & Chr(0), sPassword & Chr(0), sDrive & Chr(0))
       If rc <> 0 Then GoTo Err_Connect
    
       Exit Sub
    
    Err_Connect:
       Me.ErrorNum = rc
       Me.ErrorMsg = WnetError(rc)
    
    End Sub
    
    Public Sub DisConnect(sDrive As String)
    
       On Error GoTo Err_DisConnect
       Me.ErrorNum = 0
       Me.ErrorMsg = ""
       rc = WNetCancelConnection(sDrive + Chr(0), 0)
       If rc <> 0 Then GoTo Err_DisConnect
    
       Exit Sub
    Err_DisConnect:
       Me.ErrorNum = rc
       Me.ErrorMsg = WnetError(rc)
    
    End Sub
    
    Private Function WnetError(Errcode As Long) As String
    
       Select Case Errcode
          Case WN_Not_Supported:
             WnetError = "Function is not supported."
          Case WN_Out_Of_Memory:
             WnetError = "Out of Memory."
          Case WN_Net_Error:
             WnetError = "An error occurred on the network."
          Case WN_Bad_Pointer:
             WnetError = "The Pointer was Invalid."
          Case WN_Bad_NetName:
             WnetError = "Invalid Network Resource Name."
          Case WN_Bad_Password:
             WnetError = "The Password was Invalid."
          Case WN_Bad_Localname:
             WnetError = "The local device name was invalid."
          Case WN_Access_Denied:
             WnetError = "A security violation occurred."
          Case WN_Already_Connected:
             WnetError = "The local device was connected to a remote resource."
          Case Else:
             WnetError = "Unrecognized Error " + Str(Errcode) + "."
       End Select
    
    End Function
    
    Public Function ConnectNetworkDialog() As Long
       ' *** Show the dialog to map a drive
       
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
       
       ConnectNetworkDialog = WNetConnectionDialog(0&, RESOURCETYPE_DISK)
    
    End Function
    
    Public Function DisconnectNetworkDialog() As Long
       ' *** Show the dialog to disconnect mapped a drive
       
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
       
       DisconnectNetworkDialog = WNetDisconnectDialog(0&, RESOURCETYPE_DISK)
    
    End Function
    
    Public Function ConnectPrintDialog() As Long
       ' *** Show the dialog to map a network printer, Windows
       
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
       
       ConnectPrintDialog = WNetConnectionDialog(0&, RESOURCETYPE_PRINT)
    
    End Function
    
    Public Function DisconnectPrintDialog() As Long
       ' *** Show the dialog to disconnect network printer
       
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
       
       DisconnectPrintDialog = WNetDisconnectDialog(0&, RESOURCETYPE_PRINT)
    
    End Function
    
    Public Function ConnectUserPassword(sDrive As String, sService As String, Optional sUser As String = "", Optional sPassword As String = "") As Boolean
       ' *** Connect to a network drive
       
       'attempts to connect to the passed network
       'connection to the specified drive.
       'ErrInfo=NO_ERROR 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 = sDrive
          .lpLocalName = sService
       End With
       
       errInfo = WNetAddConnection2(NETR, sPassword, sUser, CONNECT_UPDATE_PROFILE)
       
       ConnectUserPassword = errInfo = NO_ERROR
       
    End Function

  5. #5
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    Matthew, you're a machine. =)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width