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 (Combining and Constructing Path).

Total Hit ( 3221)

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

Combining and Constructing Path

To start off with, we're going to look at some functions for combining and constructing new paths.

PathAddBackslash (ordinal 32): Adds a backslash to the end of a string to create the correct syntax for a path. If the source path already has a trailing backslash, no backslash will be added.

PathAddExtension : Adds a file extension to a path string.

PathAppend (ordinal 36): Appends one path to the end of another.

PathBuildRoot (ordinal 30): Creates a root path from a given drive number.

PathCanonicalize : Canonicalizes a path. (e.g. c:\dir1\..\dir2 => c:\dir2)

PathCombine (ordinal 37): Similar to PathAppend, but it's considerably more powerful. You get to provide a separate destination buffer, lpszDestPath, so you don't have to overwrite your source data. Also, the function doesn't necessarily append lpszSrcPath2 onto the end of lpszSrcPath1. If lpszSrcPath2 starts with a backslash it is appended to the root of lpszSrcPath1. If lpszSrcPath2 is a full path (with drive specification or UNC) it overwrites lpszSrcPath1 altogether. The function returns NULL if there is not enough space. If the operation is successful it returns a pointer to lpszDestPath.

PathCommonPrefix : Compares two paths to determine if they share a common prefix. A prefix is one of these types: "C:\\", ".", "..", "..\\".

PathCompactPath : Truncates a file path to fit within a given pixel width by replacing path. components with ellipses. You can define path width in pixels for a specified DC.
PathCompactPathEx : Truncates a path to fit within a certain number of characters by replacing path components with ellipses. This function similar to the PathCompactPath but the only difference is instead of pixel yoou can define path width in characters.

PathCreateFromUrl : Takes a file URL and converts it to a DOS path.
PathMakePretty : Converts a path to all lowercase characters to give the path a consistent appearance.

PathQuoteSpaces : Searches a path for spaces. If spaces are found, the entire path is enclosed in quotation marks.

PathUnquoteSpaces : Removes quotes from the beginning and end of a path.

Click here to copy the following block
'////////////////////////////////////////////////
'//Combine and Construction path
'////////////////////////////////////////////////
Private Declare Function PathAddBackslash Lib "shlwapi.dll" Alias "PathAddBackslashA" ( _
    ByVal pszPath As String) As Long
Private Declare Function PathAddExtension Lib "shlwapi.dll" Alias "PathAddExtensionA" ( _
    ByVal pszPath As String, _
    ByVal pszExt As String) As Long
Private Declare Function PathAppend Lib "shlwapi.dll" Alias "PathAppendA" ( _
    ByVal pszPath As String, _
    ByVal pMore As String) As Long
Private Declare Function PathBuildRoot Lib "shlwapi.dll" Alias "PathBuildRootA" ( _
    ByVal szRoot As String, _
    ByVal iDrive As Long) As Long
Private Declare Function PathCanonicalize Lib "shlwapi.dll" Alias "PathCanonicalizeA" ( _
    ByVal pszBuf As String, _
    ByVal pszPath As String) As Long
Private Declare Function PathCombine Lib "shlwapi.dll" Alias "PathCombineA" ( _
    ByVal szDest As String, _
    ByVal lpszDir As String, _
    ByVal lpszFile As String) As Long
Private Declare Function PathCommonPrefix Lib "shlwapi.dll" Alias "PathCommonPrefixA" ( _
    ByVal pszFile1 As String, _
    ByVal pszFile2 As String, _
    ByVal achPath As String) As Long
Private Declare Function PathCompactPath Lib "shlwapi.dll" Alias "PathCompactPathA" ( _
    ByVal hDC As Long, _
    ByVal pszPath As String, _
    ByVal dx As Long) As Long
Private Declare Function PathCompactPathEx Lib "shlwapi.dll" Alias "PathCompactPathExA" ( _
    ByVal pszOut As String, _
    ByVal pszSrc As String, _
    ByVal cchMax As Long, _
    ByVal dwFlags As Long) As Long
Private Declare Sub PathCreateFromUrl Lib "shlwapi.dll" Alias "PathCreateFromUrlA" ( _
    ByVal pszUrl As String, _
    ByVal pszPath As String, _
    ByRef pcchPath As Long, _
    ByVal dwFlags As Long)
Private Declare Function PathMakePretty Lib "shlwapi.dll" Alias "PathMakePrettyA" ( _
    ByVal pszPath As String) As Long
Private Declare Sub PathQuoteSpaces Lib "shlwapi.dll" Alias "PathQuoteSpacesA" ( _
    ByVal lpsz As String)
Private Declare Sub PathUnquoteSpaces Lib "shlwapi.dll" Alias "PathUnquoteSpacesA" ( _
    ByVal lpsz As String)


Private Sub Demo1()
  Dim strOutBuffer As String

  '///////////////////////////////////////////////////
  'Path + extra buffer
  strOutBuffer = "c:\Windows" + String(100, 0)
  'if it doesn't have a trailing backslash, add one
  PathAddBackslash strOutBuffer
  MsgBox "PathAddBackslash >>" & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  'filename (without extension) and buffer
  strOutBuffer = "C:\Windows\testfile" + String(100, 0)
  'if the filename doesn't have an extension, add one
  PathAddExtension strOutBuffer, ".txt"
  MsgBox "PathAddExtension >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  'Path + extra buffer
  strOutBuffer = "C:\Windows" + String(100, 0)
  'append the directory string with the filename
  PathAppend strOutBuffer, "test.txt"
  MsgBox "PathAppend >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = String(100, 0)
  'build a root path from a given drive number
  PathBuildRoot strOutBuffer, 0
  MsgBox "PathBuildRoot >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = String(255, 0)
  'canonicalize the give path
  PathCanonicalize strOutBuffer, "c:\dir1\..\dir2\.\dir3"
  MsgBox "PathCanonicalize >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = String(255, 0)
  'canonicalize the give path
  PathCombine strOutBuffer, "c:", "windows\fonts\tahoma.ttf"
  MsgBox "PathCombine >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = String(255, 0)
  'compare two paths to determine if they share a common prefix
  PathCommonPrefix "C:\InetPub\Mail\test.htm", "C:\Test\t3.txt", strOutBuffer
  MsgBox "PathCommonPrefix >> " & CleanString(strOutBuffer) & " is common prefix in both paths"

  '///////////////////////////////////////////////////
  strOutBuffer = "c:\Program Files\Microsoft MSDN\NETDocs\v20\1033\dv_aspnet.hxi"
  'truncate the file path to fit within a width of 100 pixels by replacing path components with ellipses
  PathCompactPath Me.hDC, strOutBuffer, 100
  MsgBox "PathCompactPath (fit path in 100 pix) >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = String(255, 0)
  'truncate a path to fit within 25 characters by replacing path components with ellipses.
  PathCompactPathEx strOutBuffer, "c:\Program Files\Microsoft MSDN\NETDocs\v20\1033\dv_aspnet.hxi", 25, 0
  MsgBox "PathCompactPathEx (fit path in 25 characters) >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = String(255, 0)
  'convert a file URL to a DOS path
  PathCreateFromUrl "file:///C:/testdir/tabctl.htm", strOutBuffer, Len(strOutBuffer), 0
  MsgBox "PathCompactPathEx >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = "C:/TestDIR/TabCtl.Htm"
  'convert a path to all lowercase characters
  PathMakePretty strOutBuffer
  MsgBox "PathMakePretty >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = "C:\TESTDIR" + String(100, 0)
  'convert a path to all lowercase characters if all chaarcters are in Uppercase
  PathMakePretty strOutBuffer
  MsgBox "PathMakePretty >> " & CleanString(strOutBuffer)

  '///////////////////////////////////////////////////
  strOutBuffer = "C:\Program Files\readme.txt" + String(100, 0)
  'Quote the path if it contains space
  PathQuoteSpaces strOutBuffer
  MsgBox "PathQuoteSpaces >> " & CleanString(strOutBuffer)
  'UnQuote the path if its quoted
  PathUnquoteSpaces strOutBuffer
  MsgBox "PathUnquoteSpaces >> " & CleanString(strOutBuffer)

End Sub

'Remove all trailing Null characters
Function CleanString(sInput As String) As String
  Dim ZeroPos As Long
  ZeroPos = InStr(1, sInput, Chr$(0))
  If ZeroPos > 0 Then
    CleanString = Left$(sInput, ZeroPos - 1)
  Else
    CleanString = sInput
  End If
End Function

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