Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C++

Windows 2000 Junction Points

Rate me:
Please Sign up or sign in to vote.
4.93/5 (42 votes)
5 Jan 2000 447K   3K   83  
Explains how reparse points are used to create filesystem links
// Copyright 2000 Mikael Nordell (tamlin@algonet.se)
// Distributed without warranties. Use as you see fit, except for
// commercial purposes.
#include "FSLinks.h"
#include "Reparse_Dir_HANDLE.h"
#include <cstdio>

namespace FSLinks {

BOOL CreateJunctionPoint(LPCTSTR szMountDir, LPCTSTR szDestDirArg)
{
	if (!szMountDir || !szDestDirArg || !szMountDir[0] || !szDestDirArg[0]) {
		return false;
	}

	TCHAR szDestDir[1024];
	if (szDestDirArg[0] == '\\' && szDestDirArg[1] == '?') {
		lstrcpy(szDestDir, szDestDirArg);
	} else {
		lstrcpy(szDestDir, TEXT("\\??\\"));
		TCHAR szFullDir[1024];
		LPTSTR pFilePart;
		if (!GetFullPathName(szDestDirArg, 1024, szFullDir, &pFilePart) ||
			GetFileAttributes(szFullDir) == -1)
		{
			return false;
		}
		lstrcat(szDestDir, szFullDir);
	}

	char szBuff[MAXIMUM_REPARSE_DATA_BUFFER_SIZE] = { 0 };
	TMN_REPARSE_DATA_BUFFER& rdb = *(TMN_REPARSE_DATA_BUFFER*)szBuff;

	return	rdb.Init(szDestDir) && 
			Reparse_Dir_HANDLE(szMountDir, true).SetReparsePoint(rdb);
}

BOOL DeleteJunctionPoint(LPCTSTR szDir)
{
	return Reparse_Dir_HANDLE(szDir, true).DeleteReparsePoint();
}

DWORD GetJunctionPointInfo(	LPCTSTR szMountDir,
							LPTSTR  szDestBuff,
							DWORD   dwBuffSize)
{
	const DWORD dwFA = GetFileAttributes(szMountDir);
	if (dwFA == 0xffffffff || !(dwFA & FILE_ATTRIBUTE_REPARSE_POINT)) {
		SetLastError(ERROR_PATH_NOT_FOUND);
		return 0;
	}

	Reparse_Dir_HANDLE hDir(szMountDir);

	if (!hDir.IsValid()) {
		SetLastError(ERROR_PATH_NOT_FOUND);
		return 0;
	}

	char szBuff[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
	TMN_REPARSE_DATA_BUFFER& rdb = *(TMN_REPARSE_DATA_BUFFER*)szBuff;

	DWORD dwBytesReturned;
	if (!DeviceIoControl(hDir,
						FSCTL_GET_REPARSE_POINT,
						NULL,
						0,
						(LPVOID)&rdb,
						MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
						&dwBytesReturned,
						0) ||
		!IsReparseTagValid(rdb.ReparseTag))
	{
		return 0;
	}

	if (dwBuffSize < rdb.SubstituteNameLength / sizeof(TCHAR) + sizeof(TCHAR))
	{
		return rdb.SubstituteNameLength / sizeof(TCHAR) + sizeof(TCHAR);
	}

#ifdef UNICODE
	lstrcpy(szDestBuff, rdb.PathBuffer);
#else
	if (!WideCharToMultiByte(CP_THREAD_ACP,
							0,
							rdb.PathBuffer,
							rdb.SubstituteNameLength / sizeof(WCHAR) + 1,
							szDestBuff,
							dwBuffSize,
							"",
							FALSE))
	{
		printf("WideCharToMultiByte failed (%d)\n", GetLastError());
		return 0;
	}
#endif
	return rdb.SubstituteNameLength / sizeof(TCHAR);
}

}	// namespace FSLinks

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions