Skip to content

Instantly share code, notes, and snippets.

/BoxSyncFolderRedirector.cpp Secret

Created May 31, 2016
Embed
What would you like to do?
#include "shlobj.h"
#include "ShObjIdl.h"
#include "Shlwapi.h"
int main(int, char **) {
// Initialize COM
CoInitialize(0);
// Get path to user's home directory
WCHAR path[MAX_PATH];
SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
// Contruct path to Box Sync folder
WCHAR boxSyncPath[MAX_PATH];
PathCombine(boxSyncPath, path, L"Box Sync");
// Contruct path to Box Sync\Documents folder
WCHAR boxSyncDocumentsPath[MAX_PATH];
PathCombine(boxSyncDocumentsPath, boxSyncPath, L"Documents");
// Set up a notification for when a change occurs in user's home directory
HANDLE change =
FindFirstChangeNotification(path, FALSE, FILE_NOTIFY_CHANGE_DIR_NAME);
while (true) {
// Wait for a change
WaitForSingleObject(change, INFINITE);
// Check if the Box Sync directory appeared
DWORD attributes = GetFileAttributes(boxSyncPath);
if (attributes != INVALID_FILE_ATTRIBUTES &&
(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
// Create the KnownFolderManager object
IKnownFolderManager *folderManager = 0;
CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_ALL,
IID_IKnownFolderManager, (void **)&folderManager);
// Perform the redirection
LPWSTR error;
folderManager->Redirect(FOLDERID_Documents, NULL,
KF_REDIRECT_COPY_CONTENTS |
KF_REDIRECT_DEL_SOURCE_CONTENTS |
KF_REDIRECT_UNPIN | KF_REDIRECT_PIN,
boxSyncDocumentsPath, 0, NULL, &error);
return 0;
} else {
// Continue monitoring
FindNextChangeNotification(change);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.