private static void Notify(bool created, string sourceVolumeName, string targetVolumeName) { using (MemoryAlloc data = new MemoryAlloc( MountMgrVolumeMountPoint.SizeOf + sourceVolumeName.Length * 2 + targetVolumeName.Length * 2 )) { MountMgrVolumeMountPoint mountPoint = new MountMgrVolumeMountPoint { SourceVolumeNameLength = (ushort)(sourceVolumeName.Length * 2), SourceVolumeNameOffset = (ushort)MountMgrVolumeMountPoint.SizeOf, TargetVolumeNameLength = (ushort)(targetVolumeName.Length * 2) }; mountPoint.TargetVolumeNameOffset = (ushort)(mountPoint.SourceVolumeNameOffset + mountPoint.SourceVolumeNameLength); data.WriteStruct(mountPoint); data.WriteUnicodeString(mountPoint.SourceVolumeNameOffset, sourceVolumeName); data.WriteUnicodeString(mountPoint.TargetVolumeNameOffset, targetVolumeName); using (FileHandle fhandle = OpenMountManager(FileAccess.GenericRead | FileAccess.GenericWrite)) { fhandle.IoControl( created ? IoCtlVolumeMountPointCreated : IoCtlVolumeMountPointDeleted, data.Memory, data.Size, IntPtr.Zero, 0 ); } } }
private MemoryAlloc AllocateString(string value) { MemoryAlloc alloc = new MemoryAlloc((value.Length + 1) * 2); alloc.WriteUnicodeString(0, value); alloc.WriteInt16(value.Length * 2, 0); return(alloc); }
public MemoryRegion GetAuthData() { MemoryAlloc data; int dataSize; int domainNameOffset; int userNameOffset; int passwordOffset; string lDomainName = _domainName != null ? _domainName : ""; string lUserName = _userName != null ? _userName : ""; string lPassword = _password != null ? _password : ""; // The structure plus the strings must be stored in the same buffer, // so we have to do some computation. domainNameOffset = Marshal.SizeOf(typeof(Msv1_0_InteractiveLogon)); userNameOffset = domainNameOffset + lDomainName.Length * 2; passwordOffset = userNameOffset + lUserName.Length * 2; dataSize = passwordOffset + lPassword.Length * 2; data = new MemoryAlloc(dataSize); Msv1_0_InteractiveLogon info = new Msv1_0_InteractiveLogon(); info.MessageType = Msv1_0_LogonSubmitType.Interactive; info.LogonDomainName.MaximumLength = info.LogonDomainName.Length = (ushort)(lDomainName.Length * 2); info.LogonDomainName.Buffer = data.Memory.Increment(domainNameOffset); data.WriteUnicodeString(domainNameOffset, lDomainName); info.UserName.MaximumLength = info.UserName.Length = (ushort)(lUserName.Length * 2); info.UserName.Buffer = data.Memory.Increment(userNameOffset); data.WriteUnicodeString(userNameOffset, lUserName); info.Password.MaximumLength = info.Password.Length = (ushort)(lPassword.Length * 2); info.Password.Buffer = data.Memory.Increment(passwordOffset); data.WriteUnicodeString(passwordOffset, lPassword); data.WriteStruct <Msv1_0_InteractiveLogon>(info); return(data); }
public MemoryRegion GetAuthData() { string lDomainName = !string.IsNullOrEmpty(_domainName) ? _domainName : string.Empty; string lUserName = !string.IsNullOrEmpty(_userName) ? _userName : string.Empty; string lPassword = !string.IsNullOrEmpty(_password) ? _password : string.Empty; // The structure plus the strings must be stored in the same buffer, // so we have to do some computation. int domainNameOffset = Msv1_0_InteractiveLogon.SizeOf; int userNameOffset = domainNameOffset + lDomainName.Length * 2; int passwordOffset = userNameOffset + lUserName.Length * 2; int dataSize = passwordOffset + lPassword.Length * 2; MemoryAlloc data = new MemoryAlloc(dataSize); Msv1_0_InteractiveLogon info = new Msv1_0_InteractiveLogon { MessageType = Msv1_0_LogonSubmitType.Interactive }; info.LogonDomainName.MaximumLength = info.LogonDomainName.Length = (ushort)(lDomainName.Length * 2); info.LogonDomainName.Buffer = data.Memory.Increment(domainNameOffset); data.WriteUnicodeString(domainNameOffset, lDomainName); info.UserName.MaximumLength = info.UserName.Length = (ushort)(lUserName.Length * 2); info.UserName.Buffer = data.Memory.Increment(userNameOffset); data.WriteUnicodeString(userNameOffset, lUserName); info.Password.MaximumLength = info.Password.Length = (ushort)(lPassword.Length * 2); info.Password.Buffer = data.Memory.Increment(passwordOffset); data.WriteUnicodeString(passwordOffset, lPassword); data.WriteStruct(info); return(data); }
private static void DeleteSymbolicLink(string path) { using (var data = new MemoryAlloc(MountMgrMountPoint.Size + path.Length * 2)) using (var outData = new MemoryAlloc(1600)) { MountMgrMountPoint mountPoint = new MountMgrMountPoint(); mountPoint.SymbolicLinkNameLength = (ushort)(path.Length * 2); mountPoint.SymbolicLinkNameOffset = MountMgrMountPoint.Size; data.WriteStruct <MountMgrMountPoint>(mountPoint); data.WriteUnicodeString(mountPoint.SymbolicLinkNameOffset, path); using (var fhandle = OpenMountManager(FileAccess.GenericRead | FileAccess.GenericWrite)) { fhandle.IoControl(IoCtlDeletePoints, data.Memory, data.Size, outData.Memory, outData.Size); } } }
public static bool Wait(string name, long timeout, bool relative) { using (var npfsHandle = new FileHandle( Win32.NamedPipePath + "\\", FileShareMode.ReadWrite, FileCreateOptions.SynchronousIoNonAlert, FileAccess.ReadAttributes | (FileAccess)StandardRights.Synchronize )) { using (var data = new MemoryAlloc(FilePipeWaitForBuffer.NameOffset + name.Length * 2)) { FilePipeWaitForBuffer info = new FilePipeWaitForBuffer(); info.Timeout = timeout; info.TimeoutSpecified = true; info.NameLength = name.Length * 2; data.WriteStruct <FilePipeWaitForBuffer>(info); data.WriteUnicodeString(FilePipeWaitForBuffer.NameOffset, name); NtStatus status; int returnLength; status = npfsHandle.FsControl(FsCtlWait, data, data.Size, IntPtr.Zero, 0, out returnLength); if (status == NtStatus.IoTimeout) { return(false); } if (status >= NtStatus.Error) { Win32.Throw(status); } return(true); } } }
public static bool PromptForCredentials( IWin32Window parent, string messageText, string captionText, string targetName, Win32Error errorCode, ref string userName, ref string password, ref bool save, CredUiFlags flags ) { const int maxBytes = 0x200; const int maxChars = (maxBytes - 2) / 2; Win32Error result; CredUiInfo info = new CredUiInfo(); if (userName.Length > maxChars || password.Length > maxChars) throw new ArgumentException("The user name or password string is too long."); info.Size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CredUiInfo)); info.Parent = parent != null ? parent.Handle : IntPtr.Zero; info.MessageText = messageText; info.CaptionText = captionText; using (var userNameAlloc = new MemoryAlloc(maxBytes)) using (var passwordAlloc = new MemoryAlloc(maxBytes)) { userNameAlloc.WriteUnicodeString(0, userName); userNameAlloc.WriteInt16(userName.Length * 2, 0); passwordAlloc.WriteUnicodeString(0, password); passwordAlloc.WriteInt16(password.Length * 2, 0); result = Win32.CredUIPromptForCredentials( ref info, targetName, IntPtr.Zero, errorCode, userNameAlloc, maxBytes / 2, passwordAlloc, maxBytes / 2, ref save, flags ); if (result == Win32Error.Cancelled) return false; if (result != Win32Error.Success) Win32.Throw(result); userName = userNameAlloc.ReadUnicodeString(0); password = passwordAlloc.ReadUnicodeString(0); return true; } }
public static string GetVolumeName(string deviceName) { using (MemoryAlloc data = new MemoryAlloc(MountMgrMountPoint.SizeOf + deviceName.Length * 2)) { MountMgrMountPoint mountPoint = new MountMgrMountPoint { DeviceNameLength = (ushort)(deviceName.Length * 2), DeviceNameOffset = MountMgrMountPoint.SizeOf }; data.WriteStruct(mountPoint); data.WriteUnicodeString(mountPoint.DeviceNameOffset, deviceName); using (var fhandle = OpenMountManager((FileAccess)StandardRights.Synchronize)) { NtStatus status; int retLength; using (MemoryAlloc outData = new MemoryAlloc(0x100)) { while (true) { status = fhandle.IoControl( IoCtlQueryPoints, data.Memory, data.Size, outData.Memory, outData.Size, out retLength ); if (status == NtStatus.BufferOverflow) { outData.ResizeNew(Marshal.ReadInt32(outData.Memory)); // read Size field continue; } else { break; } } status.ThrowIf(); MountMgrMountPoints mountPoints = outData.ReadStruct <MountMgrMountPoints>(); // Go through the mount points given and return the first symbolic link that seems // to be a volume name. for (int i = 0; i < mountPoints.NumberOfMountPoints; i++) { MountMgrMountPoint mp = outData.ReadStruct <MountMgrMountPoint>( MountMgrMountPoints.MountPointsOffset, MountMgrMountPoint.SizeOf, i ); string symLinkName = Marshal.PtrToStringUni( outData.Memory.Increment(mp.SymbolicLinkNameOffset), mp.SymbolicLinkNameLength / 2 ); if (IsVolumePath(symLinkName)) { return(symLinkName); } } return(null); } } } }
private static void Notify(bool created, string sourceVolumeName, string targetVolumeName) { using (MemoryAlloc data = new MemoryAlloc( MountMgrVolumeMountPoint.SizeOf + sourceVolumeName.Length * 2 + targetVolumeName.Length * 2 )) { MountMgrVolumeMountPoint mountPoint = new MountMgrVolumeMountPoint { SourceVolumeNameLength = (ushort)(sourceVolumeName.Length * 2), SourceVolumeNameOffset = (ushort)MountMgrVolumeMountPoint.SizeOf, TargetVolumeNameLength = (ushort)(targetVolumeName.Length * 2) }; mountPoint.TargetVolumeNameOffset = (ushort)(mountPoint.SourceVolumeNameOffset + mountPoint.SourceVolumeNameLength); data.WriteStruct(mountPoint); data.WriteUnicodeString(mountPoint.SourceVolumeNameOffset, sourceVolumeName); data.WriteUnicodeString(mountPoint.TargetVolumeNameOffset, targetVolumeName); using (FileHandle fhandle = OpenMountManager(FileAccess.GenericRead | FileAccess.GenericWrite)) { fhandle.IoControl( created ? IoCtlVolumeMountPointCreated : IoCtlVolumeMountPointDeleted, data.Memory, data.Size, IntPtr.Zero, 0 ); } } }
public static string GetVolumeName(string deviceName) { using (MemoryAlloc data = new MemoryAlloc(MountMgrMountPoint.SizeOf + deviceName.Length * 2)) { MountMgrMountPoint mountPoint = new MountMgrMountPoint { DeviceNameLength = (ushort)(deviceName.Length*2), DeviceNameOffset = MountMgrMountPoint.SizeOf }; data.WriteStruct(mountPoint); data.WriteUnicodeString(mountPoint.DeviceNameOffset, deviceName); using (var fhandle = OpenMountManager((FileAccess)StandardRights.Synchronize)) { NtStatus status; int retLength; using (MemoryAlloc outData = new MemoryAlloc(0x100)) { while (true) { status = fhandle.IoControl( IoCtlQueryPoints, data.Memory, data.Size, outData.Memory, outData.Size, out retLength ); if (status == NtStatus.BufferOverflow) { outData.ResizeNew(Marshal.ReadInt32(outData.Memory)); // read Size field continue; } else { break; } } status.ThrowIf(); MountMgrMountPoints mountPoints = outData.ReadStruct<MountMgrMountPoints>(); // Go through the mount points given and return the first symbolic link that seems // to be a volume name. for (int i = 0; i < mountPoints.NumberOfMountPoints; i++) { MountMgrMountPoint mp = outData.ReadStruct<MountMgrMountPoint>( MountMgrMountPoints.MountPointsOffset, MountMgrMountPoint.SizeOf, i ); string symLinkName = Marshal.PtrToStringUni( outData.Memory.Increment(mp.SymbolicLinkNameOffset), mp.SymbolicLinkNameLength / 2 ); if (IsVolumePath(symLinkName)) return symLinkName; } return null; } } } }
private static void DeleteSymbolicLink(string path) { using (MemoryAlloc data = new MemoryAlloc(MountMgrMountPoint.SizeOf + path.Length * 2)) using (MemoryAlloc outData = new MemoryAlloc(1600)) { MountMgrMountPoint mountPoint = new MountMgrMountPoint { SymbolicLinkNameLength = (ushort)(path.Length*2), SymbolicLinkNameOffset = MountMgrMountPoint.SizeOf }; data.WriteStruct(mountPoint); data.WriteUnicodeString(mountPoint.SymbolicLinkNameOffset, path); using (var fhandle = OpenMountManager(FileAccess.GenericRead | FileAccess.GenericWrite)) { fhandle.IoControl(IoCtlDeletePoints, data.Memory, data.Size, outData.Memory, outData.Size); } } }
public static bool Wait(string name, long timeout, bool relative) { using (var npfsHandle = new FileHandle( Win32.NamedPipePath + "\\", FileShareMode.ReadWrite, FileCreateOptions.SynchronousIoNonAlert, FileAccess.ReadAttributes | (FileAccess)StandardRights.Synchronize )) { using (var data = new MemoryAlloc(FilePipeWaitForBuffer.NameOffset + name.Length * 2)) { FilePipeWaitForBuffer info = new FilePipeWaitForBuffer(); info.Timeout = timeout; info.TimeoutSpecified = true; info.NameLength = name.Length * 2; data.WriteStruct<FilePipeWaitForBuffer>(info); data.WriteUnicodeString(FilePipeWaitForBuffer.NameOffset, name); NtStatus status; int returnLength; status = npfsHandle.FsControl(FsCtlWait, data, data.Size, IntPtr.Zero, 0, out returnLength); if (status == NtStatus.IoTimeout) return false; if (status >= NtStatus.Error) Win32.ThrowLastError(status); return true; } } }
public MemoryRegion GetAuthData() { string lDomainName = !string.IsNullOrEmpty(_domainName) ? _domainName : string.Empty; string lUserName = !string.IsNullOrEmpty(_userName) ? _userName : string.Empty; string lPassword = !string.IsNullOrEmpty(_password) ? _password : string.Empty; // The structure plus the strings must be stored in the same buffer, // so we have to do some computation. int domainNameOffset = Msv1_0_InteractiveLogon.SizeOf; int userNameOffset = domainNameOffset + lDomainName.Length * 2; int passwordOffset = userNameOffset + lUserName.Length * 2; int dataSize = passwordOffset + lPassword.Length * 2; MemoryAlloc data = new MemoryAlloc(dataSize); Msv1_0_InteractiveLogon info = new Msv1_0_InteractiveLogon { MessageType = Msv1_0_LogonSubmitType.Interactive }; info.LogonDomainName.MaximumLength = info.LogonDomainName.Length = (ushort)(lDomainName.Length * 2); info.LogonDomainName.Buffer = data.Memory.Increment(domainNameOffset); data.WriteUnicodeString(domainNameOffset, lDomainName); info.UserName.MaximumLength = info.UserName.Length = (ushort)(lUserName.Length * 2); info.UserName.Buffer = data.Memory.Increment(userNameOffset); data.WriteUnicodeString(userNameOffset, lUserName); info.Password.MaximumLength = info.Password.Length = (ushort)(lPassword.Length * 2); info.Password.Buffer = data.Memory.Increment(passwordOffset); data.WriteUnicodeString(passwordOffset, lPassword); data.WriteStruct(info); return data; }
public static bool PromptForCredentials( IWin32Window parent, string messageText, string captionText, string targetName, Win32Error errorCode, ref string userName, ref string password, ref bool save, CredUiFlags flags ) { const int maxBytes = 0x200; const int maxChars = (maxBytes - 2) / 2; Win32Error result; CredUiInfo info = new CredUiInfo(); if (userName.Length > maxChars || password.Length > maxChars) { throw new ArgumentException("The user name or password string is too long."); } info.Size = CredUiInfo.SizeOf; info.Parent = parent != null ? parent.Handle : IntPtr.Zero; info.MessageText = messageText; info.CaptionText = captionText; using (MemoryAlloc userNameAlloc = new MemoryAlloc(maxBytes)) using (MemoryAlloc passwordAlloc = new MemoryAlloc(maxBytes)) { userNameAlloc.WriteUnicodeString(0, userName); userNameAlloc.WriteInt16(userName.Length * 2, 0); passwordAlloc.WriteUnicodeString(0, password); passwordAlloc.WriteInt16(password.Length * 2, 0); result = Win32.CredUIPromptForCredentials( ref info, targetName, IntPtr.Zero, errorCode, userNameAlloc, maxBytes / 2, passwordAlloc, maxBytes / 2, ref save, flags ); if (result == Win32Error.Cancelled) { return(false); } if (result != Win32Error.Success) { Win32.Throw(result); } userName = userNameAlloc.ReadUnicodeString(0); password = passwordAlloc.ReadUnicodeString(0); return(true); } }