Skip to content

Instantly share code, notes, and snippets.

@rbmm
Created July 25, 2025 07:55
Show Gist options
  • Select an option

  • Save rbmm/b2d053a5b0a226c123951bb43e9a13ff to your computer and use it in GitHub Desktop.

Select an option

Save rbmm/b2d053a5b0a226c123951bb43e9a13ff to your computer and use it in GitHub Desktop.
NTSTATUS CreateReparse(PCWSTR pszFileName, PCWSTR SubstituteName, PCWSTR PrintName)
{
NTSTATUS status;
PREPARSE_DATA_BUFFER prdb = 0;
int len = 0;
PWSTR PathBuffer = 0;
ULONG cb = 0;
while (0 < (len = _snwprintf(PathBuffer, len, L"%ws%c%ws", SubstituteName, 0, PrintName)))
{
if (PathBuffer)
{
prdb->ReparseTag = IO_REPARSE_TAG_CLOUD;
prdb->ReparseDataLength = (USHORT)(cb - offsetof(REPARSE_DATA_BUFFER, GenericReparseBuffer));
//prdb->SymbolicLinkReparseBuffer.Flags = 0;
prdb->MountPointReparseBuffer.SubstituteNameLength = (USHORT)wcslen(SubstituteName) * sizeof(WCHAR);
prdb->MountPointReparseBuffer.SubstituteNameOffset = 0;
prdb->MountPointReparseBuffer.PrintNameOffset = prdb->MountPointReparseBuffer.SubstituteNameLength + sizeof(WCHAR);
prdb->MountPointReparseBuffer.PrintNameLength = (USHORT)wcslen(PrintName) * sizeof(WCHAR);
HANDLE hFile;
IO_STATUS_BLOCK iosb;
UNICODE_STRING ObjectName;
OBJECT_ATTRIBUTES oa = { sizeof(oa), 0, &ObjectName, OBJ_CASE_INSENSITIVE };
RtlInitUnicodeString(&ObjectName, pszFileName);
if (0 <= (status = NtCreateFile(&hFile, FILE_ALL_ACCESS, &oa, &iosb, 0, 0, 0,
FILE_OVERWRITE_IF, FILE_OPEN_REPARSE_POINT|FILE_NON_DIRECTORY_FILE, 0, 0)))
{
NtFsControlFile(hFile, 0, 0, 0, &iosb, FSCTL_SET_REPARSE_POINT, prdb, cb, 0, 0);
NtClose(hFile);
}
return 0;
}
cb = FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer.PathBuffer[++len]);
prdb = (PREPARSE_DATA_BUFFER)alloca(cb);
PathBuffer = prdb->MountPointReparseBuffer.PathBuffer;
}
return STATUS_INTERNAL_ERROR;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment