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 (Extracting Component Parts).

Total Hit ( 4614)

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

Extracting Component Parts

The next couple of functions can be used to access the various component parts of a pathname.

PathFindFileName (ordinal 34): Returns a pointer to the start of the filename in the specified path. In the event that the path is a simple filename, it just returns a pointer to the path itself.

PathFindExtension (ordinal 31): Returns a pointer to the file extension of the specified filename. functions return a blank string if the filename has no extension

PathGetExtension (ordinal 158): Returns a pointer to the file extension of the specified filename. PathGetExtension doesn't include the dot as part of the extension. Function returns a blank string if the filename has no extension

PathGetArgs (ordinal 52): Searches for any command line arguments in the specified path.

PathGetDriveNumber (ordinal 57): returns the drive number of the specified path (0 for drive A, 1 for drive B, etc.). If there is no drive letter the function returns -1.

PathRemoveFileSpec (ordinal 35): removes the filename from the end of a path along with the trailing
backslash (unless it's a root path). It returns TRUE if some thing was removed, or FALSE if there was no filename.

PathStripPath (ordinal 38): Removes the path from the beginning of a filename

PathStripToRoot (ordinal 50): strips characters from the end of a path until only a root drive, directory, or UNC network share remains. If the path is relative (i.e. it has no root), the function
returns FALSE.

PathRemoveArgs (ordinal 251): removes any command line arguments from the end of the specified path.

PathRemoveExtension (ordinal 250): removes the file extension from the end of the specified filename.

PathRemoveFileSpec : Removes the trailing file name and backslash from a path, if it has them.

PathStripPath : Removes the path portion of a fully qualified path and file.

PathStripToRoot : Removes all parts of the path except for the root information.

PathFindNextComponent : Parses a path for the next path component

PathFindSuffixArray : Determines if a given file name has one of a list of suffixes.

Click here to copy the following block
Private Declare Function lstrcpyA Lib "kernel32" ( _
    ByVal RetVal As String, _
    ByVal Ptr As Long) As Long
Private Declare Function lstrlenA Lib "kernel32" ( _
    ByVal Ptr As Any) As Long

'////////////////////////////////////////////////
'//Extracting Component Parts
'////////////////////////////////////////////////
Private Declare Function PathFindExtension Lib "shlwapi.dll" Alias "PathFindExtensionA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathFindFileName Lib "shlwapi.dll" Alias "PathFindFileNameA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathGetArgs Lib "shlwapi.dll" Alias "PathGetArgsA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathRemoveArgs Lib "shlwapi.dll" Alias "PathRemoveArgsA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathRemoveBackslash Lib "shlwapi.dll" Alias "PathRemoveBackslashA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathRemoveBlanks Lib "shlwapi.dll" Alias "PathRemoveBlanksA" ( _
    ByVal pszPath As String) As Long
Private Declare Sub PathRemoveExtension Lib "shlwapi.dll" Alias "PathRemoveExtensionA" ( _
    ByVal pszPath As String)
Private Declare Function PathRemoveFileSpec Lib "shlwapi.dll" Alias "PathRemoveFileSpecA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathStripPath Lib "shlwapi.dll" Alias "PathStripPathA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathStripToRoot Lib "shlwapi.dll" Alias "PathStripToRootA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathFindNextComponent Lib "shlwapi" Alias "PathFindNextComponentA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathFindSuffixArray Lib "shlwapi" Alias "PathFindSuffixArrayA" ( _
    ByVal pszPath As String, _
    ByVal apszSuffix As String, _
    ByVal iArraySize As Long) As Long

Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
  'Given a pointer to a string, return the string
  GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
  Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function

Private Sub Demo2()
  Dim strOutBuffer As String
  
  '/////////////////////////////////////////
  MsgBox "PathFindExtension >> " & GetStrFromPtrA(PathFindExtension("c:\windows\test.txt")), , "Only extension demo"

  '/////////////////////////////////////////
  'Get Only filename
  MsgBox "PathFindFileName >> " & GetStrFromPtrA(PathFindFileName("c:\windows\test.txt")), , "Only filename demo"

  '/////////////////////////////////////////
  'Get Only arguments
  MsgBox "PathGetArgs >> " & GetStrFromPtrA(PathGetArgs("c:\windows\system\regsvr32.exe /u tabctl.ocx")), , "Only arguments demo"

  '/////////////////////////////////////////
  strOutBuffer = "c:\windows\system\regsvr32.exe /u tabctl.ocx"
  'Remove args from the path
  Call PathRemoveArgs(strOutBuffer)
  MsgBox "PathRemoveArgs >> " & strOutBuffer, , "Remove args demo"

  '/////////////////////////////////////////
  strOutBuffer = "c:\windows\system\"
  'Remove back slash
  Call PathRemoveBackslash(strOutBuffer)
  MsgBox "PathRemoveBackslash >> " & strOutBuffer, , "Remove back slash demo"

  '/////////////////////////////////////////
  strOutBuffer = "c:\testdir\temp.txt"
  'Remove extension demo
  Call PathRemoveExtension(strOutBuffer)
  MsgBox "PathRemoveExtension >> " & strOutBuffer, , "Remove extension demo"

  '/////////////////////////////////////////
  strOutBuffer = "     c:\testdir\temp.txt       "
  'trim path
  Call PathRemoveBlanks(strOutBuffer)
  MsgBox "PathRemoveBlanks >> " & strOutBuffer, , "trim path demo"

  '/////////////////////////////////////////
  strOutBuffer = "c:\testdir\temp.txt  "
  'Removes the trailing file name and backslash from a path, if it has them.
  Call PathRemoveFileSpec(strOutBuffer)
  MsgBox "PathRemoveFileSpec >> " & strOutBuffer, , "Remove spec demo"

  '/////////////////////////////////////////
  strOutBuffer = "c:\testdir\dir1\"
  'Removes the path portion of a fully qualified path and file.
  Call PathStripPath(strOutBuffer)
  MsgBox "PathStripPath >> " & strOutBuffer, , "Path strip demo"

  '/////////////////////////////////////////
  strOutBuffer = "c:\testdir\dir1\"
  'Removes all parts of the path except for the root information
  Call PathStripToRoot(strOutBuffer)
  MsgBox "PathStripToRoot >> " & strOutBuffer, , "Path strip to root demo"
  
  '/////////////////////////////////////////
  Dim strPath1 As String, strPath2 As String, s
  strPath1 = "c:\dir1\dir2\dir3"
  
  '//Extract part from path
  strPath2 = GetStrFromPtrA(PathFindNextComponent(strPath1)) '// returns dir1\dir2\dir3
  s = s & "PathFindNextComponent >> 1st extracted part from " & strPath1 & " is : " & strPath2 & vbCrLf
  strPath2 = GetStrFromPtrA(PathFindNextComponent(strPath2)) '// returns dir2\dir3
  s = s & "PathFindNextComponent >> 2nd extracted part from " & strPath1 & " is : " & strPath2 & vbCrLf
  strPath2 = GetStrFromPtrA(PathFindNextComponent(strPath2)) '// returns dir3
  s = s & "PathFindNextComponent >> 3rd extracted part from " & strPath1 & " is : " & strPath2 & vbCrLf
  strPath2 = GetStrFromPtrA(PathFindNextComponent(strPath2)) '// returns nothing
  s = s & "PathFindNextComponent >> 4th extracted part from " & strPath1 & " is : " & strPath2 & vbCrLf
  
  MsgBox s, vbInformation
End Sub

Private Sub Form_Load()
  Demo2
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.