Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Undocumented Shell Path APIs (Testing Path).

Total Hit ( 4224)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Way back when Microsoft released Internet Explorer 4.0, they bundled with it a number of upgrades to the operating system including a new DLL called SHLWAPI.DLL (Shell Lightweight Utility APIs). That DLL provided programmers with some useful path manipulation functions (amongst other things), but obviously any applications that made use of those functions would also require that the user had installed Internet Explorer. Of course, now that Microsoft has 'integrated' their web browser into the operating system, the availability of SHLWAPI.DLL is no longer as much of an issue, so this article may seem a bit dated (it's been a while since I've had a chance to update this site). However, if you're still writing code for Windows 95 and NT 4.0, you might find it helpful to know that many of the functions in SHLWAPI.DLL are already available in a regular installation of Windows 95 in SHELL32.DLL - they're just not documented.

Now lets see different Path related APIs with examples

Testing Path

To start off with, we're going to look at some functions for testing paths.

Click here to copy the following block
'////////////////////////////////////////////////
'//Path Testing
'////////////////////////////////////////////////
'Mime types for Content type checking
Const MT_WORD_DOC = "application/msword"
Const MT_TEXT_FILE = "text/plain"
Const MT_DLL_EXE = "application/x-msdownload"
Const MT_HTML = "text/html"
Const MT_IMAGE_BMP = "image/bmp"
Const MT_ADOBE_PDF = "application/pdf"
Const MT_IMAGE_JPG = "image/jpeg"
Const MT_XML = "text/xml"

Private Const GCT_INVALID = &H0
Private Const GCT_LFNCHAR = &H1
Private Const GCT_SEPARATOR = &H8
Private Const GCT_SHORTCHAR = &H2
Private Const GCT_WILD = &H4

Private Declare Function PathParseIconLocation Lib "shlwapi.dll" Alias "PathParseIconLocationA" ( _
    ByVal pszIconFile As String) As Long
Private Declare Function PathIsContentType Lib "shlwapi.dll" Alias "PathIsContentTypeA" ( _
    ByVal pszPath As String, _
    ByVal pszContentType As String) As Long
Private Declare Function PathIsDirectory Lib "shlwapi.dll" Alias "PathIsDirectoryA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsDirectoryEmpty Lib "shlwapi.dll" Alias "PathIsDirectoryEmptyA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsFileSpec Lib "shlwapi.dll" Alias "PathIsFileSpecA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsLFNFileSpec Lib "shlwapi.dll" Alias "PathIsLFNFileSpecA" ( _
    ByVal lpName As String) As Long
Private Declare Function PathIsNetworkPath Lib "shlwapi.dll" Alias "PathIsNetworkPathA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsPrefix Lib "shlwapi.dll" Alias "PathIsPrefixA" ( _
    ByVal pszPrefix As String, _
    ByVal pszPath As String) As Long
Private Declare Function PathIsRelative Lib "shlwapi.dll" Alias "PathIsRelativeA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsSameRoot Lib "shlwapi.dll" Alias "PathIsSameRootA" ( _
    ByVal pszPath1 As String, _
    ByVal pszPath2 As String) As Long
Private Declare Function PathIsRoot Lib "shlwapi.dll" Alias "PathIsRootA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsSystemFolder Lib "shlwapi.dll" Alias "PathIsSystemFolderA" ( _
    ByVal pszPath As String, _
    ByVal dwAttrb As Long) As Long
Private Declare Function PathIsUNC Lib "shlwapi.dll" Alias "PathIsUNCA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsUNCServer Lib "shlwapi.dll" Alias "PathIsUNCServerA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsUNCServerShare Lib "shlwapi.dll" Alias "PathIsUNCServerShareA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathIsURL Lib "shlwapi.dll" Alias "PathIsURLA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathMakeSystemFolder Lib "shlwapi.dll" Alias "PathMakeSystemFolderA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathFindOnPath Lib "shlwapi.dll" Alias "PathFindOnPathA" ( _
    ByVal pszPath As String, ByVal ppszOtherDirs As String) As Boolean
Private Declare Function PathGetCharType Lib "shlwapi.dll" Alias "PathGetCharTypeA" ( _
    ByVal ch As Byte) As Long
Private Declare Function PathGetDriveNumber Lib "shlwapi.dll" Alias "PathGetDriveNumberA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathMatchSpec Lib "shlwapi.dll" Alias "PathMatchSpecA" ( _
    ByVal pszFile As String, _
    ByVal pszSpec As String) As Long


Private Sub Demo3()
  Dim strPath1 As String, strPath2 As String

  MsgBox "PathIsURL >> " & "index is " & PathParseIconLocation("c:\test.bmp,3"), , "icon index demo"

  '//////////////////////
  strPath1 = "c:\test.bmp"
  'strPath1 = "c:\test.tif"

  'Determines if a file's registered content type matches the specified content type.
  If PathIsContentType(strPath1, MT_IMAGE_BMP) Then
    MsgBox "PathIsContentType >> " & strPath1 & " is " & MT_IMAGE_BMP, vbInformation, "Content Type Demo"
  Else
    MsgBox "PathIsContentType >> " & strPath1 & " is " & MT_IMAGE_BMP, vbInformation, "Content Type Demo"
  End If

  '//////////////////////
  'strPath1 = "c:\"
  strPath1 = "c:\system32"

  'check whether a specified dir exists
  If PathIsDirectory(strPath1) Then
    MsgBox "PathIsDirectory >> " & strPath1 & " is a valid directory", vbInformation, "Check Directory Demo"
  Else
    MsgBox "PathIsDirectory >> " & strPath1 & " is not a valid directory", vbInformation, "Check Directory Demo"
  End If

  '//////////////////////
  'strPath1 = "c:\"
  strPath1 = "c:\testdir1"
  MkDir strPath1

  'check whether a dir contains files
  If PathIsDirectoryEmpty(strPath1) Then
    MsgBox "PathIsDirectoryEmpty >> " & strPath1 & " is an empty directory", vbInformation, "Check Empty Directory Demo"
  Else
    MsgBox "PathIsDirectoryEmpty >> " & strPath1 & " is not an empty directory", vbInformation, "Check Empty Directory Demo"
  End If
  RmDir strPath1

  '//////////////////////
  'if : or \ is not found in path then its considered as file spec path
  'strPath1 = "testdir\test.txt"
  strPath1 = "testfile.txt"

  'Searches a path for any path delimiting characters (for example, ':' or '\' ).
  If PathIsFileSpec(strPath1) Then
    MsgBox "PathIsFileSpec >> " & strPath1 & " is File Spec Path", vbInformation, "Check File Spec Path Demo"
  Else
    MsgBox "PathIsFileSpec >> " & strPath1 & " is not a File Spec Path", vbInformation, "Check File Spec Path Demo"
  End If

  '//////////////////////
  'TRUE if path exceeds the number of characters allowed by the 8.3 format, or FALSE otherwise.

  'strPath1 = "somefile.txt"
  'strPath1 = "c:\"
  strPath1 = "c:\Program Files\Microsoft SQL Server\MSSQL\"

  'check whether a file is in long format
  If PathIsLFNFileSpec(strPath1) Then
    MsgBox "PathIsLFNFileSpec >> " & strPath1 & " is Long Path", vbInformation, "Check Long Path Demo"
  Else
    MsgBox "PathIsLFNFileSpec >> " & strPath1 & " is not Long Path", vbInformation, "Check Long Path Demo"
  End If
  '//////////////////////
  'TRUE if path is network path else false

  'strPath1 = "\\dhana\api"
  strPath1 = "c:\api"

  'check whether a specified path is a network resource
  If PathIsNetworkPath(strPath1) Then
    MsgBox "PathIsNetworkPath >> " & strPath1 & " is a Network Path", vbInformation, "Check Network Path Demo"
  Else
    MsgBox "PathIsNetworkPath >> " & strPath1 & " is not a Network Path", vbInformation, "Check Network Path Demo"
  End If

  '//////////////////////
  'strPath1 = "\\dhana\api"
  strPath1 = "c:\api"

  'check whether a path contains a specific prefix
  If PathIsPrefix("c:\", strPath1) Then
    MsgBox "PathIsPrefix >> " & strPath1 & " is matching with prefix", vbInformation, "Check Prefix Demo"
  Else
    MsgBox "PathIsPrefix >> " & strPath1 & " is not matching with prefix", vbInformation, "Check Prefix Demo"
  End If

  '//////////////////////
  'strPath1 = "\\dhana\api": strPath2 = "\\dhana\printers" '//false
  strPath1 = "c:\InetPub": strPath2 = "C:\windows"  '//true
  'strPath1 = "c:\InetPub": strPath2 = "d:\windows" '//false

  'check whether two files are located on the same drive
  If PathIsSameRoot(strPath1, strPath2) Then
    MsgBox "PathIsSameRoot >> " & strPath1 & " and " & strPath2 & " both have same root", vbInformation, "Check Relative Demo"
  Else
    MsgBox "PathIsSameRoot >> " & strPath1 & " and " & strPath2 & " both have not same root", vbInformation, "Check Relative Demo"
  End If

  '//////////////////////
  strPath1 = "../Images/user.gif"
  'strPath1 = "c:\InetPub\Websites\Site1\Images\User.gif"

  'check whether a path is relative or absolute
  If PathIsRelative(strPath1) Then
    MsgBox "PathIsRelative >> " & strPath1 & " is relative path", vbInformation, "Check Relative Demo"
  Else
    MsgBox "PathIsRelative >> " & strPath1 & " is absolute path", vbInformation, "Check Relative Demo"
  End If

  '//////////////////////
  'strPath1 = "c:\"
  strPath1 = "C:\InetPub"

  'check whether a path is a root path
  If PathIsRoot(strPath1) Then
    MsgBox "PathIsRoot >> " & strPath1 & " is root", vbInformation, "Check Root Demo"
  Else
    MsgBox "PathIsRoot >> " & strPath1 & " is not root", vbInformation, "Check Root Demo"
  End If

  '//////////////////////
  'strPath1 = "c:\WINNT"
  'strPath1 = "c:\windows"
  strPath1 = "C:\InetPub"

  'give the windows directory the proper attributes to become a system folder
  'PathMakeSystemFolder strPath1 '//first make path as system folder just for demo

  'check whether it succeeded
  If PathIsSystemFolder(strPath1, 0) Then
    MsgBox "PathIsSystemFolder >> " & strPath1 & " is system folder", vbInformation, "Check System folder Demo"
  Else
    MsgBox "PathIsSystemFolder >> " & strPath1 & " is not system folder", vbInformation, "Check System folder Demo"
  End If

  '//////////////////////
  strPath1 = "\\dhana\api"
  'strPath1 = "http://dhana:8484/api.aspx"
  'strPath1 = "C:\InetPub"

  'determines if the string is a valid UNC for a server and share path
  If PathIsUNC(strPath1) Then
    MsgBox "PathIsUNC >> " & strPath1 & " is UNC", vbInformation, "Check UNC path Demo"
  Else
    MsgBox "PathIsUNC >> " & strPath1 & " is not UNC", vbInformation, "Check UNC path Demo"
  End If

  '//////////////////////
  'strPath1 = "\\dhana" 'true
  strPath1 = "\\."  'true
  strPath1 = "\\dhana\api"  'false
  'strPath1 = "C:\InetPub" 'false

  'determines if the string is a valid UNC for a server path only
  If PathIsUNCServer(strPath1) Then
    MsgBox "PathIsUNCServer >> " & strPath1 & " is UNC Server", vbInformation, "Check UNC Server path Demo"
  Else
    MsgBox "PathIsUNCServer >> " & strPath1 & " is not UNC Server", vbInformation, "Check UNC Server path Demo"
  End If

  '//////////////////////
  'strPath1 = "\\dhana" 'false
  'strPath1 = "\\." 'false
  strPath1 = "\\dhana\api"  'true
  'strPath1 = "C:\InetPub" 'false

  'determines if the string is a valid UNC for a share path only
  If PathIsUNCServerShare(strPath1) Then
    MsgBox "PathIsUNCServerShare >> " & strPath1 & " is UNC Server share", vbInformation, "Check UNC Server share Demo"
  Else
    MsgBox "PathIsUNCServerShare >> " & strPath1 & " is not UNC Server", vbInformation, "Check UNC Server share Demo"
  End If

  '//////////////////////
  strPath1 = "http://binaryworld.net:6576/main"  'true
  'strPath1 = "ftp://dhana" 'true
  'strPath1 = "\\dhana\api" 'false
  'strPath1 = "C:\InetPub" 'false

  'determines if the string is a valid URL
  If PathIsURL(strPath1) Then
    MsgBox "PathIsURL >> " & strPath1 & " is URL", vbInformation, "Check URL Demo"
  Else
    MsgBox "PathIsURL >> " & strPath1 & " is not URL", vbInformation, "Check URL Demo"
  End If

  '//////////////////////
  strPath1 = "c:\boot.ini"

  'determine whether the file exists
  If PathFileExists(strPath1) Then
    MsgBox "PathFileExists >> " & strPath1 & " exists", vbInformation, "Check File existance"
  Else
    MsgBox "PathFileExists >> " & strPath1 & " doesn't exist", vbInformation, "Check File existance"
  End If

  '//////////////////////
  strPath1 = "regsvr32.exe"
  'strPath1 = "calc.exe"
  'strPath1 = "moonabc.exe"

  'is the file 'autoexec.bat located in one of the directories specified in the PATH environment variable?
  If PathFindOnPath(strPath1, vbNullString) Then
    MsgBox "PathFindOnPath >> " & strPath1 & " the file is located a PATH-directory", vbInformation, "Check File existance in path variable"
  Else
    MsgBox "PathFindOnPath >> " & strPath1 & " the file is not located a PATH-directory", vbInformation, "Check File existance in path variable"
  End If

  '//////////////////////
  'check whether a specified character can be used in a path
  Dim ch As String, strOutBuffer As String
  ch = ">"
  'ch = "*"
  Select Case PathGetCharType(Asc(ch))
    Case GCT_INVALID
      strOutBuffer = "invalid"
    Case GCT_LFNCHAR
      strOutBuffer = "valid in a long file name"
    Case GCT_SHORTCHAR
      strOutBuffer = "valid in a short (8.3) file name"
    Case GCT_WILD
      strOutBuffer = "a wildcard character"
    Case GCT_SEPARATOR
      strOutBuffer = "a path separator"
  End Select
  MsgBox "PathGetCharType >> The character '" & ch & "' is " + strOutBuffer

  '//////////////////////
  strPath1 = "d:\test.txt"
  'get the corresponding drive number of a file
  MsgBox "PathGetDriveNumber >> The file is located on drive " + Chr$(65 + PathGetDriveNumber(strPath1))

  '//////////////////////
  Dim ext As String
  strPath1 = "test.bmp"
  'ext = "*.b?p" '//extension pattern
  ext = "*.b??"  '//extension pattern
  ext = "*.ec?"  '//extension pattern

  'does the extensions match?
  If PathMatchSpec(strPath1, ext) Then
    MsgBox "PathMatchSpec >> " & strPath1 & " extension matches with " & ext, vbInformation, "Match Spec"
  Else
    MsgBox "PathMatchSpec >> " & strPath1 & " extension does not match with " & ext, vbInformation, "Match Spec"
  End If
End Sub

Private Sub Form_Load()
  Demo3
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.