Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Windows 2000 Junction Points

, 5 Jan 2000
Explains how reparse points are used to create filesystem links
junctionpoints_src.zip
JunctionPoints
FSLinks
FSLinks.dsp
MakeLink
MakeLink.dsp
MakeLink.dsw
MakeLink.exe
// Copyright 2000 Mikael Nordell (tamlin@algonet.se)
// Distributed without warranties. Use as you see fit, except for
// commercial purposes.
#include "../FSLinks/FSLinks.h"
#include <cstdio>
#include <cstdlib>	// definition of _MAX_PATH
#include <winioctl.h>
#include <tchar.h>


using namespace FSLinks;


void usage()
{
	const char szUsage[] =
		"MakeLink - Copyright 2000 Mikael Nordell\n"
		"\n"
		"Displays and/or modifies NTFS5 Junction Points\n"
		"\n"
		"MakeLink [junction where_to] [/L junction] [/D junction]\n"
		"\n"
		"  /D  Deletes Junction Point from specified junction directory.\n"
		"  /L  Lists where junction points to\n"
		"\n"
		"Examples:\n"
		"  MakeLink D:\\foo \"C:\\Program Files\"\n"
		"  MakeLink /L D:\\foo\n"
		"  MakeLink /D D:\\foo\n"
		"\n"
		"To mount a complete volume, use MOUNTVOL or Disk Administrator.\n";
	puts(szUsage);
}

void PrintJunctionInfo(LPCTSTR szSrc)
{
	const int cchDest = 512;
	TCHAR szDest[cchDest];
	
	const DWORD dwRet = GetJunctionPointInfo(szSrc, szDest, cchDest);
	if (dwRet == 0 || dwRet > cchDest) {
		printf("  Failed to get junction info for dir ");
		_tprintf(TEXT("\"%s\"\n"), szSrc);
		return;
	}

	printf("Junction points to ");
	_tprintf(TEXT("\"%s\"\n"), szDest);
}


extern "C" { void _setenvp() {} }

int _tmain(int argc, TCHAR *argv[])
{
	if (argc != 3) {
		usage();
		return 0;
	}

	if ((argv[1][0] == '/' || argv[1][0] == '-')) {
		switch (argv[1][1]) {
			case 'D':
			case 'd':
				PrintJunctionInfo(argv[2]);
				puts("");
				if (DeleteJunctionPoint(argv[2])) {
					puts("Deleted the above junction point.");
				} else {
					printf("Couldn't delete junction point ");
					_tprintf(TEXT("\"%s\"\n"), argv[2]);
				}
				break;

			case 'L':
			case 'l':
				PrintJunctionInfo(argv[2]);
				break;

			default:
				printf("Unknown option ");
				_tprintf(TEXT("%s\n"), argv[1]);
				usage();
		}
		return 0;
	}

	LPCTSTR szSrc = argv[1];

	TCHAR szVol[4] = TEXT("x:\\");
	if (szSrc[1] == ':') {
		szVol[0] = szSrc[0];
	} else {
        TCHAR curdirstr[_MAX_PATH];
        if (GetCurrentDirectory(sizeof(curdirstr), curdirstr)) {
			if (curdirstr[1] == ':') {
				szVol[0] = curdirstr[0];
			}
		}
	}

	DWORD dwCompLen;
	DWORD dwFsFlags;
	if (!GetVolumeInformation(szVol, 0, 0, 0, &dwCompLen, &dwFsFlags, 0, 0)) {
		const DWORD dwErr = GetLastError();
		printf("  GetVolumeInformation for ");
		_tprintf(TEXT("\"%s\""), szVol);
		printf(" failed (%d)!\n", dwErr);
		return -1;
	}

	szVol[2] = 0;

	if (!(dwFsFlags & FILE_SUPPORTS_REPARSE_POINTS)) {
		printf("  The filesystem on ");
		_tprintf(TEXT("\"%s\""), szVol);
		puts(" does not support Reparse Points");
		return -1;
	}

	const DWORD dwFA = GetFileAttributes(szSrc);
	if (dwFA == 0xffffffff) {
		printf("  Can't get info for ");
		_tprintf(TEXT("\"%s\""), szSrc);
		puts("\n  Does the directory really exist?");
		return -1;
	}

	int nRet = 0;
	if (!CreateJunctionPoint(argv[1], argv[2])) {
		printf("Couldn't create link ");
		nRet = -1;
	} else {
		printf("Created link ");
	}
	_tprintf(TEXT("\"%s\" -> \"%s\"\n"), argv[1], argv[2]);

	return nRet;
}

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

Share

About the Author

Mike Nordell
Architect
Sweden Sweden
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 6 Jan 2000
Article Copyright 2000 by Mike Nordell
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid