home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / network / wnet / wnet.frm < prev    next >
Text File  |  1993-08-20  |  12KB  |  430 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    Caption         =   "Test of WNet Functions"
  4.    ClientHeight    =   4020
  5.    ClientLeft      =   1095
  6.    ClientTop       =   1485
  7.    ClientWidth     =   7365
  8.    Height          =   4425
  9.    Left            =   1035
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4020
  12.    ScaleWidth      =   7365
  13.    Top             =   1140
  14.    Width           =   7485
  15.    Begin CommandButton disconnect 
  16.       Caption         =   "Disconnect"
  17.       Height          =   495
  18.       Left            =   3120
  19.       TabIndex        =   7
  20.       Top             =   3360
  21.       Width           =   1215
  22.    End
  23.    Begin CommandButton Connect 
  24.       Caption         =   "Connect..."
  25.       Height          =   495
  26.       Left            =   480
  27.       TabIndex        =   6
  28.       Top             =   3360
  29.       Width           =   1215
  30.    End
  31.    Begin ListBox List1 
  32.       Height          =   1785
  33.       Left            =   120
  34.       TabIndex        =   4
  35.       Top             =   1080
  36.       Width           =   7215
  37.    End
  38.    Begin CommandButton Command1 
  39.       Caption         =   "Ok"
  40.       Height          =   495
  41.       Left            =   5880
  42.       TabIndex        =   3
  43.       Top             =   3360
  44.       Width           =   1215
  45.    End
  46.    Begin Label Label5 
  47.       Caption         =   "WNetCancelConnection"
  48.       Height          =   255
  49.       Left            =   2640
  50.       TabIndex        =   9
  51.       Top             =   3000
  52.       Width           =   2415
  53.    End
  54.    Begin Label Label4 
  55.       Caption         =   "WNetAddConnection"
  56.       Height          =   255
  57.       Left            =   120
  58.       TabIndex        =   8
  59.       Top             =   3000
  60.       Width           =   2415
  61.    End
  62.    Begin Label Label3 
  63.       Caption         =   "WNetGetConnection"
  64.       Height          =   255
  65.       Index           =   1
  66.       Left            =   120
  67.       TabIndex        =   5
  68.       Top             =   840
  69.       Width           =   1935
  70.    End
  71.    Begin Label Label3 
  72.       Caption         =   "WNetGetUser"
  73.       Height          =   255
  74.       Index           =   0
  75.       Left            =   120
  76.       TabIndex        =   2
  77.       Top             =   120
  78.       Width           =   1335
  79.    End
  80.    Begin Label Label2 
  81.       BorderStyle     =   1  'Fixed Single
  82.       Caption         =   "Label2"
  83.       Height          =   255
  84.       Left            =   5280
  85.       TabIndex        =   1
  86.       Top             =   480
  87.       Width           =   2055
  88.    End
  89.    Begin Label Label1 
  90.       BorderStyle     =   1  'Fixed Single
  91.       Caption         =   "Label1"
  92.       Height          =   255
  93.       Left            =   120
  94.       TabIndex        =   0
  95.       Top             =   480
  96.       Width           =   5055
  97.    End
  98. End
  99. Option Explicit
  100. ' This is a demonstration program to use the common
  101. ' WNET... routines for Windows Network support
  102. ' Author: K. True, Intel Corp., 1993
  103.  
  104. Declare Function WNetGetUser% Lib "USER" (ByVal szUser As String, iBufSize As Integer)
  105. Declare Function WnetAddConnection% Lib "user" (ByVal lpszNetPath As Any, ByVal lpszPassword As Any, ByVal lpszLocalName As Any)
  106. Declare Function GetDriveType% Lib "kernel" (ByVal nDrive%)
  107. Declare Function WNetGetConnection% Lib "user" (ByVal lpszLocalName As String, ByVal lpszRmtName As String, iRmtLen As Integer)
  108. Declare Function WNetCancelConnection% Lib "user" (ByVal lpszLocalName As String, ByVal iForce As Integer)
  109. Declare Function WNetBrowseDialog% Lib "user" (ByVal hWParent As Integer, ByVal iType As Integer, ByVal lpszPath As String)
  110.  
  111.  
  112. ' WNet constants
  113. Const WN_Success = &H0
  114. Const WN_Not_Supported = &H1
  115. Const WN_Net_Error = &H2
  116. Const WN_More_Data = &H3
  117. Const WN_Bad_Pointer = &H4
  118. Const WN_Bad_Value = &H5
  119. Const WN_Bad_Password = &H6
  120. Const WN_Access_Denied = &H7
  121. Const WN_Function_Busy = &H8
  122. Const WN_Bad_User = &HA
  123. Const Wn_Out_Of_Memory = &HB
  124. Const WN_Cancelled = &HC
  125. Const WN_Not_Connected = &H30
  126. Const WN_Open_Files = &H31
  127. Const WN_Bad_NetName = &H32
  128. Const WN_Bad_Localname = &H33
  129. Const WN_Already_Connected = &H34
  130. Const WN_Bad_JobID = &H40
  131. Const WN_Job_Not_Found = &H41
  132. Const WN_Job_Not_Held = &H42
  133. Const WN_Bad_Queue = &H43
  134. Const WN_Bad_Handle = &H44
  135. Const WN_Cant_Set_Copies = &H45
  136. Const WN_Already_Locked = &H46
  137. Const WN_No_Error = &H50
  138. Const WNBD_Conn_Disktree = 1
  139. Const WNBD_Conn_PrintQ = 3
  140. Const WNBD_CONN_SelAll = 0
  141.  
  142. 'GetDriveType constants
  143. Const Drive_Unknown = 0
  144. Const Drive_Removable = 2
  145. Const Drive_Fixed = 3
  146. Const Drive_Remote = 4
  147.  
  148. Dim LF As String
  149.  
  150. Sub Command1_Click ()
  151.    Unload Form1
  152. End Sub
  153.  
  154. Sub Connect_Click ()
  155. Dim iListIndx As Integer
  156. Dim szName As String
  157. Dim szLocal As String
  158. Dim iPos As Integer
  159. Dim szRmtName As String
  160. Dim iRmtLen As Integer
  161. Dim iRC As Integer, iRC1 As Integer
  162. Dim szPriorName As String
  163.  
  164.  
  165.   iListIndx = List1.ListIndex
  166.   If iListIndx < 0 Then Exit Sub
  167.  
  168.   szName = TrimNullStr$(List1.List(iListIndx))
  169.   iPos = InStr(szName, " ")   ' parse at blank to get device
  170.   If iPos = 0 Then iPos = Len(szName) + 1
  171.   szLocal = Mid$(szName, 1, iPos - 1)
  172.  
  173.   szRmtName = String$(255, 0)
  174.   iRmtLen = Len(szRmtName)
  175.   szPriorName = ""
  176.   iRC = WNetGetConnection(szLocal, szRmtName, iRmtLen)
  177.   Select Case iRC
  178.     Case WN_Success
  179.       szPriorName = szRmtName ' save old connected name
  180.       iRC1 = WNetCancelConnection(szLocal, 0)
  181.       If iRC1 = WN_Success Then
  182.     List1.List(iListIndx) = szLocal
  183.        Else
  184.     MsgBox "Unable to disconnect. RC=" & iRC1 & LF & WNErrText(iRC1), 0, "Disconnect Error"
  185.       End If
  186.     Case WN_Not_Connected
  187.       ' do nothing..
  188.     Case Else
  189.       MsgBox "WNetCancelConnection returns rc=" & iRC & LF & WNErrText(iRC), 0, "Disconnect Error"
  190.   End Select
  191.  
  192.   If InStr(szLocal, "LPT") > 0 Then ' Do Printer Browse
  193.     iRC = WNetBrowseDialog(Form1.hWnd, WNBD_Conn_PrintQ, szRmtName)
  194.    Else
  195.     iRC = WNetBrowseDialog(Form1.hWnd, WNBD_Conn_Disktree, szRmtName)
  196.   End If
  197.  
  198.   If iRC = WN_Success Then
  199.     iRC = WnetAddConnection(szRmtName, "", szLocal)
  200.     If iRC = WN_Success Then
  201.       List1.List(iListIndx) = szLocal & "  " & TrimNullStr$(szRmtName)
  202.     Else
  203.       MsgBox "WnetAddConnection RC=" & iRC & LF & WNErrText(iRC), 0, "Connect Error:"
  204.     End If
  205.    Else
  206.     If Len(szPriorName) > 0 Then ' reset to prior state
  207.       iRC = WnetAddConnection(szPriorName, "", szLocal)
  208.       If iRC = WN_Success Then
  209.     List1.List(iListIndx) = szLocal & "  " & TrimNullStr$(szRmtName)
  210.       Else
  211.     MsgBox "WnetAddConnection RC=" & iRC & LF & WNErrText(iRC), 0, "Connect Error:"
  212.       End If
  213.     End If
  214.   End If
  215.  
  216. End Sub
  217.  
  218. Sub disconnect_Click ()
  219.  
  220. Dim iListIndx As Integer
  221. Dim szName As String
  222. Dim szLocal As String
  223. Dim iPos As Integer
  224. Dim szRmtName As String
  225. Dim iRmtLen As Integer
  226. Dim iRC As Integer, iRC1 As Integer
  227.  
  228.  
  229.   iListIndx = List1.ListIndex
  230.   If iListIndx < 0 Then Exit Sub
  231.  
  232.   szName = TrimNullStr$(List1.List(iListIndx))
  233.   iPos = InStr(szName, " ")   ' parse at blank to get device
  234.   If iPos = 0 Then iPos = Len(szName) + 1
  235.   szLocal = Mid$(szName, 1, iPos - 1)
  236.  
  237.   szRmtName = String$(255, 0)
  238.   iRmtLen = Len(szRmtName)
  239.   iRC = WNetGetConnection(szLocal, szRmtName, iRmtLen)
  240.   Select Case iRC
  241.     Case WN_Success
  242.       iRC1 = WNetCancelConnection(szLocal, 0)
  243.       If iRC1 = WN_Success Then
  244.     List1.List(iListIndx) = szLocal
  245.        Else
  246.     MsgBox "Unable to disconnect. RC=" & iRC1 & LF & WNErrText(iRC1), 0, "Disconnect Error"
  247.       End If
  248.     Case WN_Not_Connected
  249.       ' do nothing..
  250.     Case Else
  251.       MsgBox "WNetCancelConnection returns rc=" & iRC & LF & WNErrText(iRC), 0, "Disconnect Error"
  252.   End Select
  253. End Sub
  254.  
  255. Sub Form_Load ()
  256. Dim iRC As Integer
  257. Dim szUserName As String
  258. Dim iSize%
  259.  
  260.  LF = String$(1, 10)' a nice LineFeed character
  261.  
  262. 'WNetGetUser -- retrieve user name
  263.   szUserName = String$(255, 0)
  264.   iSize = Len(szUserName)
  265.   iRC = WNetGetUser%(szUserName, iSize)
  266.   Label2.Caption = "RC = " & iRC & " iSize=" & iSize
  267.   Label1.Caption = szUserName
  268.  
  269. 'WNetGetConnection -- show current LPT/Disk connections
  270.  LoadListBox
  271. End Sub
  272.  
  273.    Function IsSuccess% (ReturnCode%, Msg$)
  274.  
  275.  
  276.  
  277.    If ReturnCode% = WN_Success Then
  278.       IsSuccess% = True
  279.    Else
  280.       IsSuccess% = False
  281.       Select Case ReturnCode%
  282.  
  283.  
  284.  
  285.      Case WN_Success:
  286.         'Drive1.Refresh
  287.      Case WN_Not_Supported:
  288.         Msg$ = "Function is not supported."
  289.      Case Wn_Out_Of_Memory:
  290.         Msg$ = "Out of Memory."
  291.      Case WN_Net_Error:
  292.         Msg$ = "An error occurred on the network."
  293.      Case WN_Bad_Pointer:
  294.         Msg$ = "The Pointer was Invalid."
  295.      Case WN_Bad_NetName:
  296.         Msg$ = "Invalid Network Resource Name."
  297.      Case WN_Bad_Password:
  298.         Msg$ = "The Password was Invalid."
  299.      Case WN_Bad_Localname:
  300.         Msg$ = "The local device name was invalid."
  301.      Case WN_Access_Denied:
  302.         Msg$ = "A security violation occurred."
  303.      Case WN_Already_Connected:
  304.         Msg$ = "The local device was connected to a remote resource."
  305.      Case Else:
  306.         Msg$ = "Unrecognized Error " + Str$(ReturnCode%) + "."
  307.  
  308.  
  309.  
  310.       End Select
  311.    End If
  312.  
  313.  
  314.  
  315.    End Function
  316.  
  317. Sub LoadListBox ()
  318.  
  319. Dim iRC As Integer
  320. Dim Msg As String
  321.  
  322. Dim i As Integer
  323. Dim szRmtName As String
  324. Dim iRmtLen As Integer
  325.  
  326. Dim iType As Integer
  327.  
  328. 'WNetGetConnection -- show current LPT/Disk connections
  329.   
  330.   For i = 1 To 3
  331.     szRmtName = String(255, 0)
  332.     iRmtLen = Len(szRmtName)
  333.     Msg = "LPT" & i
  334.     iRC = WNetGetConnection(Msg, szRmtName, iRmtLen)
  335.  
  336.     Msg = Msg & ": "
  337.     Select Case iRC
  338.        Case WN_Success
  339.      Msg = Msg & Mid$(szRmtName, 1, iRmtLen)
  340.        Case WN_Not_Connected
  341.      Msg = Msg & "Local Device"
  342.        Case Else
  343.      Msg = Msg & " (rc=" & iRC & ") " & " " & WNErrText(iRC)
  344.     End Select
  345.  
  346.     List1.AddItem Msg
  347.   Next i
  348.  
  349. ' Do drives
  350.   For i = 0 To 25
  351.     iType = GetDriveType(i)   ' 0=A, 1=B...
  352.  
  353.     Msg = Chr$(i + 65) & ":  "
  354.  
  355.     Select Case iType
  356.       Case 0
  357.      Msg = Msg & " "
  358.       Case Drive_Removable
  359.      Msg = Msg & " (floppy drive)"
  360.       Case Drive_Fixed
  361.      Msg = Msg & " (local hard disk)"
  362.       Case Drive_Remote
  363.     szRmtName = String(255, 0)
  364.     iRmtLen = Len(szRmtName)
  365.     iRC = WNetGetConnection(Chr$(i + 65) & ":", szRmtName, iRmtLen)
  366.     Select Case iRC
  367.        Case WN_Success
  368.          Msg = Msg & Mid$(szRmtName, 1, iRmtLen)
  369.        Case WN_Not_Connected
  370.          Msg = Msg & "Not Connected"
  371.        Case Else
  372.          Msg = Msg & " (rc=" & iRC & ") " & " " & WNErrText(iRC)
  373.     End Select
  374.     Case Else
  375.       Msg = Msg & " (GetDriveType=" & iRC & ") " & " " & WNErrText(iRC)
  376.       End Select
  377.  
  378.     List1.AddItem Msg
  379.   Next i
  380.  
  381. End Sub
  382.  
  383. Function TrimNullStr$ (ByVal given As Variant)
  384.     Dim i As Integer
  385.     i = InStr(given, Chr$(0))
  386.     If i > 0 Then
  387.       TrimNullStr$ = Trim$(Left$(given, i - 1))
  388.     Else
  389.       TrimNullStr$ = Trim$(given)
  390.     End If
  391. End Function
  392.  
  393. Function WNErrText (iRC As Integer) As String
  394. Dim Msg$
  395.   
  396.   Msg$ = ""
  397.   Select Case iRC
  398.      Case WN_Success:
  399.         'Drive1.Refresh
  400.      Case WN_Not_Supported:
  401.         Msg$ = "Function is not supported."
  402.      Case WN_More_Data
  403.         Msg$ = "Buffer too small to hold data."
  404.      Case Wn_Out_Of_Memory:
  405.         Msg$ = "Out of Memory."
  406.      Case WN_Net_Error:
  407.         Msg$ = "An error occurred on the network."
  408.      Case WN_Bad_Pointer:
  409.         Msg$ = "The Pointer was Invalid."
  410.      Case WN_Bad_NetName:
  411.         Msg$ = "Invalid Network Resource Name."
  412.      Case WN_Bad_Password:
  413.         Msg$ = "The Password was Invalid."
  414.      Case WN_Bad_Localname:
  415.         Msg$ = "The local device name was invalid."
  416.      Case WN_Access_Denied:
  417.         Msg$ = "A security violation occurred."
  418.      Case WN_Already_Connected:
  419.         Msg$ = "The local device was connected to a remote resource."
  420.      
  421.      Case Else:
  422.         Msg$ = "WNET error " + Str$(iRC) + "."
  423.  
  424.       End Select
  425.  
  426.    WNErrText = Msg$
  427.  
  428. End Function
  429.  
  430.