Click to See Complete Forum and Search --> : Help


senthil
December 2nd, 1999, 10:28 AM
Can anyone help me in getting the computer name??

I have placed the code below. Please go thru it and tell me the mistake. THank you

Private Type SHITEMID
cb As Long
abID As Byte
End Type

Private Type ITEMIDLIST
mkid As SHITEMID
End Type

Private Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type

Public Enum ShFolder
fldrPrograms = &H2
fldrControls = &H3
fldrPrinters = &H4
fldrPersonal = &H5
fldrFavorites = &H6
fldrStartup = &H7
fldrRecent = &H8
fldrSendTo = &H9
fldrBitBucket = &HA
fldrStartMenu = &HB
fldrDeskTopDir = &H10
fldrDrives = &H11
fldrNetWork = &H12
fldrNetHood = &H13
fldrFonts = &H14
fldrTemplates = &H15
fldrCommonStartMenu = &H16
fldrCommonPrograms = &H17
fldrCommonStartup = &H18
fldrCommonDeskTopDir = &H19
fldrCommonAppData = &H1A
End Enum

Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const BIF_BROWSEFORCOMPUTER = 4096
Private Const MAX_PATH = 260

Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long

Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long

Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long

Private Sub Command1_Click()
Dim IDL As ITEMIDLIST
Dim lpIDList As Long
Dim sBuffer As String * 255
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo
Dim Path As String * 255

lret = SHGetSpecialFolderLocation(0, fldrNetWork, IDL)

szTitle = "Select new server"
With tBrowseInfo
.hwndOwner = Me.hWnd
.pIDLRoot = IDL.mkid.cb
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_BROWSEFORCOMPUTER
.pszDisplayName = StrPtr(Path)
End With

lpIDList = SHBrowseForFolder(tBrowseInfo)

If (lpIDList) Then
sBuffer = Space(MAX_PATH)
lret = SHGetPathFromIDList(lpIDList, sBuffer)
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
MsgBox sBuffer & Path
End If
End Sub

Allen Noakes
December 2nd, 1999, 02:46 PM
Use this code to get the computer name:

Private Declare Function GetComputerName& Lib "kernel32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, nSize As Long)

Private Const MAX_COMPUTERNAME_LENGTH = 15


Then place the following in the sub/function that you want to find the name of the computer in:

Dim sName As String
Dim lRetVal As Long
Dim lSize As Long

sName = String$(MAX_COMPUTERNAME_LENGTH + 1, ¯)

lSize = MAX_COMPUTERNAME_LENGTH + 1
lRetVal = GetComputerName(sName, lSize)

sName = Replace(sName, Chr(0), "")


Allen Noakes
VB Programmer/Analyst
Dames & Moore Group
misan@dames.com

atul b
December 2nd, 1999, 06:01 PM
Hi Senthil
The code fragment code sent in your mail is working absolutely fine on my machine. My machine is WinNT workstation having VB 6.0, VC 6.0 installed with service pack 3. So try on the m/c with above config.

atul b
December 2nd, 1999, 08:42 PM
Hi senthil
Earlier, I tried with the favourites but not with network option which was in your code and is failing on my machine, with SHGetPathFromIDList(lpIDList, sBuffer) function. Some other options are also failing with the same function like CSIDL_DRIVES, CSIDL_PRINTERS with the same function.
Rightnow don't know the reason. Will get back with reason.
bye