Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #27
All content and source code is Copyright (c) 1998 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

NEW! Online Catalog now available!

We have entered a partnership with VBXtras to provide many development tools available for sale on our web site. For a limited time we are offering a FREE one year subscription for anyone who purchases any products through our web site. If you are currently shopping for some cool tools, check out http://www.codeoftheweek.com/catalog.html

This area is new and does not yet have all products online. If there is something particular you are looking for (and can't find it on our site), please let us know. We probably can get it for you at a great price!

Advertising

If you are interested in advertising in COTW please email us at sponsor@codeoftheweek.com Our rates are VERY reasonable, actually they are almost FREE. We reach over three thousand Visual Basic developers each week.


Requirements for this Issue

The source code in this issue is designed for Visual Basic 5.0 and higher. It will probably work in VB4 32-bit with some minor changes (such as the enum).

In this Issue

This issue introduces a class module for manipulating network connections and retrieving the login name.

cNetworkInterface Class

Description

You might have seen the dialogs built into Windows 95/NT for making and removing network connections. This class makes the necessary calls to the Networking API to show those dialogs. It also provides a property to retrieve the username of the currently logged in user. These functions will only work under Windows 95 and Windows NT. They depend on a DLL called MPR.DLL which should exists on all 95/NT systems that have networking installed. The dialogs are displayed by calling the API functions WNetConnectionDialog and WNetDisconnectDialog. The Login Name is retrieved by calling the API function WNetGetUser.

Declaration

Public Sub ConnectDiskDialog(Optional hParentWnd As Long)
Public Sub ConnectPrinterDialog(Optional hParentWnd As Long)
Public Sub DisconnectDiskDialog(Optional hParentWnd As Long)
Public Sub DisconnectPrinterDialog(Optional hParentWnd As Long)
Public Property Get LoginName() As String

Parameters

Returns

Only LoginName returns a value. It will return the username the person logged in with.

Sample Usage

This particular example assumes you have a form with six command buttons on it arranged in a control array. The button captions should be as follows:

The number on the left of the equal sign should be the Index property value and the text on the right hand sign of the equal sign should be the Caption property.

Private Sub cmdActions_Click(Index As Integer)

    Dim Network As New cNetworkInterface

    Select Case Index
        Case 0 ' connect to disk
            Network.ConnectDiskDialog
        Case 1 ' connect to printer
            Network.ConnectPrinterDialog
        Case 2 ' disconnect a disk
            Network.DisconnectDiskDialog
        Case 3 ' disconnect a printer
            Network.DisconnectPrinterDialog
        Case 4 ' show login name
            MsgBox "Current Login Name is: " & Network.LoginName
        Case 5 ' close
            Unload Me
    End Select
End Sub

Button 0 will show the connect to disk resource dialog. Button 1 will show the connect to printer resource dialog. Button 2 will show the disconnect disk resource dialog. Button 3 will show the disconnect printer resource dialog. Button 4 will show a message box with the text "Current Login Name is: " where the will be replaced with the persons username. Button 5 will exit the program.

Next Week

Next week will we expand on this class to show how you can make and remove the network connections directly from your application without requiring the display of the dialog boxes.


Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0027.html


NEW! Online Catalog now available!

We have entered a partnership with VBXtras to provide many development tools available for sale on our web site. For a limited time we are offering a FREE one year subscription for anyone who purchases any products through our web site. If you are currently shopping for some cool tools, check out http://www.codeoftheweek.com/catalog.html

This area is new and does not yet have all products online. If there is something particular you are looking for (and can't find it on our site), please let us know. We probably can get it for you at a great price!

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to http://www.codeoftheweek.com/membersonly/bi/0025.html

Summary

That concludes this issue of COTW. We hope you find the source code useful in your development.

The below describes the ways you can supply us some feedback about COTW. We would like to see our members help mold COTW into the best Visual Basic source code resource available. But to do that we need your feedback about what you like and what you do not like about COTW.

Advertising

If you are interested in advertising in COTW please email us at sponsor@codeoftheweek.com Our rates are VERY reasonable, actually they are almost FREE. We reach over three thousand Visual Basic developers each week.

How to tell us what you think

If you have any suggestions for topics you would like to see covered or questions about this issue, please email them to info@codeoftheweek.com or use online feedback form at http://www.codeoftheweek.com/feedback.html.

If you have any source code you would like to submit for possible inclusion in COTW, please fill out our online submission form at http://www.codeoftheweek.com/submission.html.

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com

Subscription Update

Thank you for trying Code of the Week for Visual Basic.

Your free trial expires after you receive your fourth issue. If you want to continue to receive Code of the Week you can get 52 issues of COTW for only $19.95. This is a full year of Visual Basic source code and information to help with all your development. So don't wait, subscribe now! The quickest way to subscribe is to jump to our online order form at http://www.codeoftheweek.com/order.html