Export (0) Print
Expand All

PathCommonPrefix function

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

Syntax


int PathCommonPrefix(
  _In_       LPCTSTR pszFile1,
  _In_       LPCTSTR pszFile2,
  _Out_opt_  LPTSTR pszPath
);

Parameters

pszFile1 [in]

Type: LPCTSTR

A pointer to a null-terminated string of length MAX_PATH that contains the first path name.

pszFile2 [in]

Type: LPCTSTR

A pointer to a null-terminated string of length MAX_PATH that contains the second path name.

pszPath [out, optional]

Type: LPTSTR

A pointer to a buffer that receives the common prefix. This buffer must be at least MAX_PATH characters in size. If there is no common prefix, it is set to NULL.

Return value

Type: int

Returns the count of common prefix characters in the path. If the output buffer pointer is not NULL, then these characters are copied to the output buffer.

Examples


#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main( void )
{
	// String path name 1.
	char buffer_1[ ] = "C:\\win\\desktop\\temp.txt";
	char *lpStr1;
	lpStr1 = buffer_1;

	// String path name 2.
	char buffer_2[ ] = "c:\\win\\tray\\sample.txt";
	char *lpStr2;
	lpStr2 = buffer_2;

	// String path out buffer.
	char buffer_3[ ] = "abcdef";
	char *lpStr3;
	lpStr3 = buffer_3;

	// Variable to get the return. 
	// from "PathCommonPrefix"
	int retval;
	retval = PathCommonPrefix(lpStr1,lpStr2,lpStr3);
	cout << "The contents of String 1:            " << lpStr1 << endl;
	cout << "The contents of String 2:            " << lpStr2 << endl;
	cout << "The length of the output buffer is : " << retval << endl;
	cout << "The contents of Output buffer:       " << lpStr3 << endl;
}

OUTPUT:
-----------
The contents of String 1:            C:\win\desktop\temp.txt
The contents of String 2:            c:\win\tray\sample.txt
The length of the output buffer is : 6
The contents of Output buffer:       C:\win

Requirements

Minimum supported client

Windows 2000 Professional, Windows XP [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Shlwapi.h

Library

Shlwapi.lib

DLL

Shlwapi.dll (version 4.71 or later)

Unicode and ANSI names

PathCommonPrefixW (Unicode) and PathCommonPrefixA (ANSI)

 

 

Community Additions

ADD
Show:
© 2014 Microsoft