Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help ! Want to meet deadline, I need code snippets. 1

Status
Not open for further replies.

hiptino

Programmer
May 27, 2000
18
0
0
NG
The deadline given me to deliver an application is fast approaching. There are still some outstanding modules that I need to implement. I need codes that I can finetune.

1)Could anyone give me code snippets on how to be able use
Win32 API functions(or other means, not network
neighborhood) to:
-get the names of all computers on a network
-fill a listbox with the names
-Select any computer in the list, and perform
(shutdown,restart,warmboot,lock)on selected item(s)

2)Does anybody knows what could cause this runtime error.

"Runtime error'-2147217887(80040e21)':
This field is too small to accept the amount of data you
attempted to add. Try Iserting or pasting less data."

This error does not occur on when running the app. on the
development machines(Win98 and NT Server).
It only pops up on the target machine (NT WrkStation).
And pops up when user needs to provide their password.
I couldn't find the error number in MSDN.

3)How good is VB for developing WinSock apps.
 
2. You usually get this error when you are trying to save a string to a database which is longer than the field length. You may also get the error when trying to insert a number > 32767 into an integer data type field, or a number > 2,147,483,647 into a long data type field.
Simon
 
A partial answer to your first question: This snippet returns all shared resources on your network. Actually more than you asked for, but it isn't hard to filter out all but the computer names. You will need a list box, a command button and a check box. The value of the check box determines whether the list box shows all resources or just the computer names. Use this code to show the computer names and then see thread711-47273 for a small discussion on shutting down remote workstations.

[tt]Option Explicit
Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCE_REMEMBERED = &H3
Private Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Private Const RESOURCEDISPLAYTYPE_FILE = &H4
Private Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Private Const RESOURCEDISPLAYTYPE_GROUP = &H5
Private Const RESOURCEDISPLAYTYPE_SERVER = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_UNKNOWN = &HFFFF
Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2
Private Const RESOURCEUSAGE_RESERVED = &H80000000
Private Const NO_ERROR = 0
Private Const ERROR_NO_MORE_ITEMS = 259&

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 Type NETRESOURCEWSTRINGS
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lLocalName As Long
lRemoteName As Long
lComment As Long
lProvider As Long
bByteArray(0 To 1024) As Byte
End Type

Private Declare Function VBlstrcpy Lib "kernel32.dll" Alias "lstrcpyA" _
(ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Private Declare Function VBlstrlen Lib "kernel32.dll" Alias "lstrlenA" _
(ByVal lpString As Long) As Long

Private Declare Function WNetOpenEnum Lib "mpr.dll" _
Alias "WNetOpenEnumA" _
(ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As NETRESOURCE, _
lphEnum As Long) As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" _
Alias "WNetEnumResourceA" (ByVal hEnum As Long, _
lpcCount As Long, lpBuffer As Any, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" _
(ByVal hEnum As Long) As Long

Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" _
Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, _
ByVal lpMessage As String, ByVal dwTimeout As Long, _
ByVal bForceAppsClosed As Long, _
ByVal bRebootAfterShutdown As Long) As Long

Dim TabCount As Integer

Private Function EnumerateFunc(nrPassed As NETRESOURCEWSTRINGS) As Boolean
Dim dwResult As Long, dwResultEnum As Long
Dim hEnum As Long, cEntries As Long
Dim nrLocal As NETRESOURCEWSTRINGS, nrInfo As NETRESOURCE
Dim tLocalName As String, tRemoteName As String

TabCount = TabCount + 1

With nrInfo
.dwScope = nrPassed.dwScope
.dwType = nrPassed.dwType
.dwDisplayType = nrPassed.dwDisplayType
.dwUsage = nrPassed.dwUsage
.lpLocalName = ParseResourceString(nrPassed.lLocalName)
.lpRemoteName = ParseResourceString(nrPassed.lRemoteName)
.lpComment = ParseResourceString(nrPassed.lComment)
.lpProvider = ParseResourceString(nrPassed.lProvider)
End With

cEntries = 1 ' enumerate entries one at a time

dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, _
RESOURCETYPE_ANY, 0, nrInfo, hEnum)
If Not (dwResult = NO_ERROR) Then
' An error occurred. Add code to determine what happened.
EnumerateFunc = False
TabCount = TabCount - 1
Exit Function
End If

Do
dwResultEnum = WNetEnumResource(hEnum, _
cEntries, nrLocal, Len(nrLocal))
If (dwResultEnum = NO_ERROR) Then
tRemoteName = ParseResourceString(nrLocal.lRemoteName)
If Check1.Value = 1 Then 'list all resources
List1.AddItem String(TabCount, vbTab) & tRemoteName
Else
If TabCount = 2 Then 'list only the computers....
'You will probably have to adjust this value depending
'on the machine where the code is run. Normally, the first
'entry would be a workgroup name, showing as a container
'for workstations, serving as containers for various shared resources.
'On a domain contoller, the first entry may show as the network provider,
'i.e. "Microsoft Windows Network", followed by workgroups, etc.

List1.AddItem tRemoteName
End If
End If
' If this NETRESOURCE is a container,
' call the function recursively.

If (RESOURCEUSAGE_CONTAINER = (nrLocal.dwUsage _
And RESOURCEUSAGE_CONTAINER)) _
Then
If Not (EnumerateFunc(nrLocal)) Then
DoEvents
End If
End If
ElseIf Not (dwResultEnum = ERROR_NO_MORE_ITEMS) Then
' An error occurred. Handle it here...
Exit Do
End If

Loop While Not (dwResultEnum = ERROR_NO_MORE_ITEMS)

dwResult = WNetCloseEnum(hEnum)
If Not (dwResult = NO_ERROR) Then
' An error occurred. Handle it here...
EnumerateFunc = False
TabCount = TabCount - 1
Exit Function
End If

EnumerateFunc = True
TabCount = TabCount - 1

End Function

Function ParseResourceString(lStringPtrIn As Long) As String
Dim sTmp As String
Dim lCount As Long

lCount = VBlstrlen(lStringPtrIn)
If lCount > 0 Then
sTmp = String$(255, 0)
VBlstrcpy sTmp, lStringPtrIn
ParseResourceString = Left(sTmp, lCount)
Else
ParseResourceString = ""
End If
End Function

Private Sub Command1_Click()
Dim NRCS As NETRESOURCEWSTRINGS
Dim bFinished As Boolean
List1.Clear
bFinished = EnumerateFunc(NRCS)
End Sub

Private Sub Form_Load()
Command1.Caption = "List Network Resources"
Check1.Caption = "Show All Resources"
End Sub[/tt]
VCA.gif

Alt255@Vorpalcom.Intranets.com

"If you can get people to ask the wrong questions, they'll never find the right answers."[tt]
Thomas Pynchon[/tt]

Perhaps the reverse is also true....
 
In response to your second question you may want to add some code to your form_load event. This code opens a recordset with no results. It allows you to retrieve field definitions from the db. You have to set the textbox.tag = to the fieldname so it will know which field it is. Hope it helps.

Private Sub ConfigureForm()
Dim rsFieldDefinitions As ADODB.Recordset
Dim ctl As Control
Dim i As Integer

On Error GoTo ConfigureError
Set rsFieldDefinitions = New ADODB.Recordset
rsFieldDefinitions.Open "Select * From YourTableNameHere Where 1 = 2", mcnProp, adOpenDynamic, adLockOptimistic, adCmdText
For i = 0 To rsFieldDefinitions.Fields.Count - 1
For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
If rsFieldDefinitions.Fields(i).Name = (ctl.Tag)Then
ctl.MaxLength = rsFieldDefinitions.Fields (i).DefinedSize
End If
End If
Next ctl
Next i
Set rsFieldDefinition = Nothing
Exit Sub
ConfigureError:
MsgBox "Error Code: " & Err.Number & _
vbCrLf & "Description: " & Err.Description & _
vbCrLf & "Source: " & Err.Source
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top