Avatar billede slapstick Nybegynder
10. juli 2003 - 13:24 Der er 5 kommentarer og
1 løsning

problem med class modul

jeg har et class modul der kan bruges til at mappe netværksdrev
men når jeg prøver at kalde den sub der connecter fra min form så siger den: sub or function not defined
hvad sker der?

koden kommer her:

formkode:

Private Sub cmdConnect_Click()

    Call Connect(txtIP.Text, txtUser.Text, txtPass.Text)

End Sub



class modul kode:

'**************************************
' Name: Class_NetUse
' Description:A class to map/unmap netwo
'    rk drives
' By: Waty Thierry
'
'This code is copyrighted and has' limited warranties.Please see http://w
'    ww.Planet-Source-Code.com/vb/scripts/Sho
'    wCode.asp?txtCodeId=1071&lngWId=1'for details.'**************************************

' #VBIDEUtils#**************************
'    **********************************
' * Programmer Name : Waty Thierry
' * Web Site : www.geocities.com/Researc
'    hTriangle/6311/
' * E-Mail: waty.thierry@usa.net
' * Date : 25/09/98
' * Time : 11:18
' * Module Name : class_NetUse
' * Module Filename : NetUse.cls
' **************************************
'    ********************************
' * Comments : Map / Unmap network drive
'    s
' *
' *
' **************************************
'    ********************************
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 sim
    '    ilar to
    'WNetAddConnection, except that it takes
    '    a pointer
    'to a NETRESOURCE structure to describe
    '    the network
    'resource to connect to. It also takes t
    '    he addition
    'parameters lpUserID and dwFlags.
    'lpNetResource
    'Specifies the network resource to conne
    '    ct to.
    'The following fields must be set when m
    '    aking a
    'connection, the others are ignored.
    ' lpRemoteName: Specifies the network re
    '    source
    'to connect to. This is limited
    'to MAX_PATH.
   
    ' lpLocalName: This specifies the name o
    '    f 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 resour
    '    ce
    'is made without making a redirection.
   
    ' lpProvider: Specifies the NP to connec
    '    t 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 RESOUR
    '    CETYPE_PRINT
    ' if lpLocalName is not the empty string
    '    . It may
    ' also be RESOURCETYPE_ANY if lpLocalNam
    '    e is the
    ' empty string.
   
    'lpPassword
    'Specifies the password to be used in ma
    '    king the
    'connection, normally the password assoc
    '    iated 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 wis
    '    hes to connect
    'to a resource, but has a different user
    '    name or
    'account assigned to him for that resour
    '    ce. This
    'identification represents a security co
    '    ntext, and
    'is NP specific.
    '
    'dwFlags
    'This is a bit mask which may have any o
    '    f the
    'following bits set:
    '
    ' CONNECT_UPDATE_PROFILE: If the connect
    '    ion 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_GENERI
    '    C = &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 va
    '    lue is
    'NO_ERROR (0). If the user cancels out o
    '    f the
    'dialog box, it is &HFFFFFFFF.
    ConnectNetworkDialog = WNetConnectionDialog(0&, RESOURCETYPE_DISK)
End Function


Public Function DisconnectNetworkDialog() As Long
    ' >>> Show the dialog to discon
    '    nect mapped a drive
    'If the function succeeds, the return va
    '    lue is
    'NO_ERROR (0). If the user cancels out o
    '    f 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 va
    '    lue is
    'NO_ERROR (0). If the user cancels out o
    '    f the
    'dialog box, it is &HFFFFFFFF.
    ConnectPrintDialog = WNetConnectionDialog(0&, RESOURCETYPE_PRINT)
End Function


Public Function DisconnectPrintDialog() As Long
    ' >>> Show the dialog to discon
    '    nect network printer
    'If the function succeeds, the return va
    '    lue is
    'NO_ERROR (0). If the user cancels out o
    '    f 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 driv
    '    e
    'attempts to connect to the passed netwo
    '    rk
    '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
Avatar billede kedde65 Praktikant
10. juli 2003 - 13:27 #1
du skal huske at have en instanc af din klasse før du kan kalde metoder i den.

Private Sub cmdConnect_Click()
    dim myClass as ClassName 'erstat ClassName med navnet på din klasse
    set myClass = new ClassName 'erstat ClassName med navnet på din klasse
    Call myClass.Connect(txtIP.Text, txtUser.Text, txtPass.Text)

End Sub

VH CK
Avatar billede slapstick Nybegynder
10. juli 2003 - 13:32 #2
ah ok hehe se det vidste jeg ik
tak :))
Avatar billede kedde65 Praktikant
10. juli 2003 - 13:33 #3
men er hele den smørre bare for at mappe et netværksdrev?

VH CK
Avatar billede slapstick Nybegynder
10. juli 2003 - 14:49 #4
ja
har du en bedre metode?
det eneste jeg skal kunne er at mappe og disconnecte et netværksdrev
feks 10.0.0.1/c$
Avatar billede skwat Praktikant
10. juli 2003 - 14:52 #5
hmmmm....

shell "net use k: \\123.234.123.234\c$"

shell "net use k: /delete"
Avatar billede slapstick Nybegynder
10. juli 2003 - 14:54 #6
yay den virker det er os den jeg bruger nu
men ville gerne have holdt det hele internt i mit program
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester

Seneste spørgsmål Seneste aktivitet
I går 10:21 Ny printer, men hvilken Af mort1 i Printere
13/0819:41 USB på Linux Af Uvanga i Linux
12/0818:26 Hvilken type mus Af mort1 i PC
11/0813:21 ms Forms Af leahcim i Andet software
10/0821:30 Blokeret på Snapchat? Af LineP i Chat & Messaging