Andrew R.
September 12th, 2001, 11:15 AM
Maybe because I didn’t say what my final task is.
The task is: find a specific file on the specific computer in the network.
It is given: name of the file (“master.mdf”) and name of the computer (“SQLServer”).
Conditions: computer may belong to other domain.
It looks simple, but it’s not because of mentioned condition.
So, to start search file-search procedure requires start folder. And start folder cannot be only a computer name "\\SQLServer" but should also contain a share name "\\SQLServer\Share1". But I don’t know name of the share and I don’t know name of the domain! I’ve tried to use vbLib, but its functions do not work with domain other than default system domain.
So, is there any way to accomplish my task or not?
Plese, help.
PS: what I would want to do is to somehow loop through ALL network components (domains, computers, shares) and compare all computer names with given one. And then I'd like to find shares ot that computer to run search procedure on each one.
DSJ
September 12th, 2001, 11:51 AM
Have you looked at wNetOpenEnum and wNetEnumResource API functions?
DSJ
September 12th, 2001, 11:58 AM
NetConnectionEnum might be of some interest as well...
Andrew R.
September 12th, 2001, 12:59 PM
Yes, I'm using the code you just sent me as an example of how to use it.
So, when I do
nStatus = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, &H0, byval 0&, hEnum)
It returns only one enry - "Microsoft Windows Network is drive".
When I do
nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, &H0, byval 0&, hEnum)
It returns what I need "\\SQLServer\Sare1 is drive" but only if I previously browsed to thad drive with Windows Explorer!
I need it to be returned all the time, not only after I connected to that resource.
John G Duffy
September 12th, 2001, 04:38 PM
Here is a sample program that will eneumerate all resources available at the time of invocation. No previous accessing need be done.
Start a new project. Add a module. Paste this code into the module. RUn the program. The output is in the Debug immediate window.
option Explicit
'Add this code to a module and set the Project's Startup Object to 'Sub Main'
' (-> Project Menu -> Project Properties -> General Tab)
private Const RESOURCE_CONNECTED as Long = &H1&
private Const RESOURCE_GLOBALNET as Long = &H2&
private Const RESOURCE_REMEMBERED as Long = &H3&
private Const RESOURCEDISPLAYTYPE_DIRECTORY& = &H9
private Const RESOURCEDISPLAYTYPE_DOMAIN& = &H1
private Const RESOURCEDISPLAYTYPE_FILE& = &H4
private Const RESOURCEDISPLAYTYPE_GENERIC& = &H0
private Const RESOURCEDISPLAYTYPE_GROUP& = &H5
private Const RESOURCEDISPLAYTYPE_NETWORK& = &H6
private Const RESOURCEDISPLAYTYPE_ROOT& = &H7
private Const RESOURCEDISPLAYTYPE_SERVER& = &H2
private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
private Const RESOURCEDISPLAYTYPE_SHAREADMIN& = &H8
private Const RESOURCETYPE_ANY as Long = &H0&
private Const RESOURCETYPE_DISK as Long = &H1&
private Const RESOURCETYPE_PRINT as Long = &H2&
private Const RESOURCETYPE_UNKNOWN as Long = &HFFFF&
private Const RESOURCEUSAGE_ALL as Long = &H0&
private Const RESOURCEUSAGE_CONNECTABLE as Long = &H1&
private Const RESOURCEUSAGE_CONTAINER as Long = &H2&
private Const RESOURCEUSAGE_RESERVED as Long = &H80000000
private Const NO_ERROR = 0
private Const ERROR_MORE_DATA = 234 'L // dderror
private Const RESOURCE_ENUM_ALL as Long = &HFFFF
private Type NETRESOURCE
dwScope as Long
dwType as Long
dwDisplayType as Long
dwUsage as Long
pLocalName as Long
pRemoteName as Long
pComment as Long
pProvider as Long
End Type
private Type NETRESOURCE_REAL
dwScope as Long
dwType as Long
dwDisplayType as Long
dwUsage as Long
sLocalName as string
sRemoteName as string
sComment as string
sProvider as string
End Type
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 WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (byval dwScope as Long, byval dwType as Long, byval dwUsage as Long, lpNetResource as Any, lphEnum as Long) as Long
private Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (byval hEnum as Long, lpcCount as Long, lpBuffer as NETRESOURCE, lpBufferSize as Long) as Long
private Declare Function WNetCloseEnum Lib "mpr.dll" (byval hEnum as Long) as Long
private Declare Function VarPtrAny Lib "vb40032.dll" Alias "VarPtr" (lpObject as Any) as Long
private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (lpTo as Any, lpFrom as Any, byval lLen as Long)
private Declare Sub CopyMemByPtr Lib "kernel32" Alias "RtlMoveMemory" (byval lpTo as Long, byval lpFrom as Long, byval lLen as Long)
private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (byval lpString1 as string, byval lpString2 as Any) as Long
private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (byval lpString as Any) as Long
private Declare Function getusername Lib "advapi32.dll" Alias "GetUserNameA" (byval lpBuffer as string, nSize as Long) as Long
private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (byval lpBuffer as string, nSize as Long) as Long
public strUserName as string
public strMachinerName as string
Sub main()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'-> This sample was created by Donald Grover
Const MAX_RESOURCES = 256
Const NOT_A_CONTAINER = -1
Dim bFirstTime as Boolean
Dim lReturn as Long
Dim hEnum as Long
Dim lCount as Long
Dim lMin as Long
Dim lLength as Long
Dim l as Long
Dim lBufferSize as Long
Dim lLastIndex as Long
Dim uNetApi(0 to MAX_RESOURCES) as NETRESOURCE
Dim uNet() as NETRESOURCE_REAL
bFirstTime = true
Do
If bFirstTime then
lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_ALL, byval 0&, hEnum)
bFirstTime = false
else
If uNet(lLastIndex).dwUsage And RESOURCEUSAGE_CONTAINER then
lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_ALL, uNet(lLastIndex), hEnum)
else
lReturn = NOT_A_CONTAINER
hEnum = 0
End If
lLastIndex = lLastIndex + 1
End If
If lReturn = NO_ERROR then
lCount = RESOURCE_ENUM_ALL
Do
lBufferSize = UBound(uNetApi) * len(uNetApi(0)) / 2
lReturn = WNetEnumResource(hEnum, lCount, uNetApi(0), lBufferSize)
If lCount > 0 then
ReDim Preserve uNet(0 to lMin + lCount - 1) as NETRESOURCE_REAL
for l = 0 to lCount - 1
'Each Resource will appear here as uNet(i)
uNet(lMin + l).dwScope = uNetApi(l).dwScope
uNet(lMin + l).dwType = uNetApi(l).dwType
uNet(lMin + l).dwDisplayType = uNetApi(l).dwDisplayType
uNet(lMin + l).dwUsage = uNetApi(l).dwUsage
If uNetApi(l).pLocalName then
lLength = lstrlen(uNetApi(l).pLocalName)
uNet(lMin + l).sLocalName = Space$(lLength)
CopyMem byval uNet(lMin + l).sLocalName, byval uNetApi(l).pLocalName, lLength
End If
If uNetApi(l).pRemoteName then
lLength = lstrlen(uNetApi(l).pRemoteName)
uNet(lMin + l).sRemoteName = Space$(lLength)
CopyMem byval uNet(lMin + l).sRemoteName, byval uNetApi(l).pRemoteName, lLength
End If
If uNetApi(l).pComment then
lLength = lstrlen(uNetApi(l).pComment)
uNet(lMin + l).sComment = Space$(lLength)
CopyMem byval uNet(lMin + l).sComment, byval uNetApi(l).pComment, lLength
End If
If uNetApi(l).pProvider then
lLength = lstrlen(uNetApi(l).pProvider)
uNet(lMin + l).sProvider = Space$(lLength)
CopyMem byval uNet(lMin + l).sProvider, byval uNetApi(l).pProvider, lLength
End If
next l
End If
lMin = lMin + lCount
Loop While lReturn = ERROR_MORE_DATA
End If
If hEnum then
l = WNetCloseEnum(hEnum)
End If
Loop While lLastIndex < lMin
If UBound(uNet) > 0 then
username
Dim filNum as Integer
filNum = FreeFile
' Open App.Path & "\" & LCase(App.EXEName) & ".txt" for Output Shared as #filNum
'Open "d:\" & App.EXEName & ".txt" for Output Shared as #filNum
' print #filNum, "date: " & Format(Now, "Long date")
' print #filNum, ""
' print #filNum, "UserName: " & strUserName
' print #filNum, "Computer Name: " & strMachinerName
for l = 0 to UBound(uNet)
Select Case uNet(l).dwDisplayType
Case RESOURCEDISPLAYTYPE_DIRECTORY&
Debug.print "Directory...",
' print #filNum, "Directory...",
Case RESOURCEDISPLAYTYPE_DOMAIN
Debug.print "Domain...",
' print #filNum, "Domain...",
Case RESOURCEDISPLAYTYPE_FILE
Debug.print "File...",
' print #filNum, "File...",
Case RESOURCEDISPLAYTYPE_GENERIC
Debug.print "Generic...",
' print #filNum, "Generic...",
Case RESOURCEDISPLAYTYPE_GROUP
Debug.print "Group...",
' print #filNum, "Group...",
Case RESOURCEDISPLAYTYPE_NETWORK&
Debug.print "Network...",
' print #filNum, "Network...",
Case RESOURCEDISPLAYTYPE_ROOT&
Debug.print "Root...",
' print #filNum, "Root...",
Case RESOURCEDISPLAYTYPE_SERVER
Debug.print "Server...",
' print #filNum, "Server...",
Case RESOURCEDISPLAYTYPE_SHARE
Debug.print "Share...",
' print #filNum, "Share...",
Case RESOURCEDISPLAYTYPE_SHAREADMIN&
Debug.print "ShareAdmin...",
' print #filNum, "ShareAdmin...",
End Select
Debug.print uNet(l).sRemoteName, uNet(l).sComment
' print #filNum, uNet(l).sRemoteName, uNet(l).sComment
next l
End If
' Close #filNum
' MsgBox "File " + App.Path & "\" & LCase(App.EXEName) & ".txt created" + vbCrLf + "Open it in a text editor to see the results", vbInformation
End Sub
private Sub username()
on error resume next
'Create a buffer
strUserName = string(255, Chr$(0))
'get the username
getusername strUserName, 255
'strip the rest of the buffer
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
'Create a buffer
strMachinerName = string(255, Chr$(0))
GetComputerName strMachinerName, 255
'remove the unnecessary chr$(0)'s
strMachinerName = Left$(strMachinerName, InStr(1, strMachinerName, Chr$(0)) - 1)
End Sub
John G
Andrew R.
September 13th, 2001, 08:31 AM
That's exactly what I need!
Thank you very much!