I have a project where I have to get the target of a junction. This is some code I came up with:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#define BUFSIZE MAX_PATH
using namespace std;
int main()
{
TCHAR Path[BUFSIZE];
DWORD dwRet;
HANDLE hFile;
hFile = CreateFile(L"C:\\Users\\Test\\Documents\\My Videos",
GENERIC_READ,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
0);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
return 0;
}
dwRet = GetFinalPathNameByHandle(hFile, Path, BUFSIZE, VOLUME_NAME_DOS);
if (dwRet < BUFSIZE)
{
_tprintf(TEXT("\nThe final path is: %s\n"), Path);
}
CloseHandle(hFile);
//wcout << Path;
return 0;
}
Now, the weird thing is that the code returns nicely the GetFinalPathNameByHandle
for every directory EXCEPT the junction / reparse point Documents\My Videos. For the junctions it throws an "error 5" with the GetLastError()
. Has anyone an idea what can cause this?