Windows 7 redirection (a.k.a. virtualization) application I use to demonstrate the effect:
C:> cl /W4 /EHsc Redirection.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
Redirection.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:Redirection.exe
Redirection.obj
Source:
#pragma comment(lib, "ole32")
#pragma comment(lib, "ShlWApi")
#pragma comment(lib, "Shell32")
#define _WIN32_WINNT 0x0600
#define UNICODE
#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <iostream>
using std::wcout;
using std::endl;
class auto_free_com_memory {
public:
auto_free_com_memory(void * p) : m_p(p) {}
~auto_free_com_memory() {if (m_p) ::CoTaskMemFree(m_p);}
private:
void * m_p;
};
int wmain() {
wchar_t * folderPath = NULL;
if FAILED(SHGetKnownFolderPath(FOLDERID_ProgramFiles, KF_FLAG_CREATE, NULL , &folderPath)) {
wcout << L"Cannot retrieve FOLDERID_ProgramFiles known folder location." << endl;
return -1;
}
auto_free_com_memory _fp(folderPath);
// System drive root redirection
wchar_t systemDriveRoot[] = {folderPath[0], folderPath[1], folderPath[2], L'\0'};
WCHAR filePath[MAX_PATH];
if (wcscpy_s(filePath, systemDriveRoot) || !PathAppend(filePath, L"UAC Redirection test.log")) {
wcout << L"Could not build the path to a file under the system drive root folder." << endl;
return -2;
}
wcout << L"Creating \"" << filePath << L"\"." << endl;
HANDLE file = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
wcout << L"Could not create a file under the system drive root folder. (error " << GetLastError() << L")." << endl;
else
CloseHandle(file);
// Program Files redirection
if (wcscpy_s(filePath, folderPath) || !PathAppend(filePath, L"MyUACRedirection")) {
wcout << L"Could not build the path to a file under the Program File known folder." << endl;
return -3;
}
// Does the file folder exist?
DWORD folderAttributes = GetFileAttributes(filePath);
if (folderAttributes == INVALID_FILE_ATTRIBUTES || ! (folderAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
wcout << L"The folder \"" << filePath << L"\" must exist and be accessible for this test to work." << endl;
return -4;
}
if (!PathAppend(filePath, L"MyUACRedirection.log")) {
wcout << L"Unexpected error adding the file name to \"" << filePath << "\"."<< endl;
}
wcout << L"Creating \"" << filePath << "\"." << endl;
file = CreateFile(filePath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE)
wcout << L"Could not create \"" << filePath << "\" (error " << GetLastError() << L")." << endl;
else
CloseHandle(file);
return 0;
}