protected override void DoExecute() { string tempDirName = Guid.NewGuid().ToString("N"); string tempDirPath = Path.Combine(Path.GetTempPath(), tempDirName); _directoryAdapter.CreateDirectory(tempDirPath); string currentPath = Path.GetDirectoryName( ReflectionUtils.GetCodeBaseFilePath(Assembly.GetExecutingAssembly())); if (string.IsNullOrEmpty(currentPath)) { throw new InternalException("Couldn't get current path."); } string consoleAppSrcPath = Path.Combine(currentPath, "UberDeployer.ConsoleApp"); string consoleAppDstPath = tempDirPath; _directoryAdapter.CopyAll(consoleAppSrcPath, consoleAppDstPath); string consoleAppExePath = Path.Combine(consoleAppDstPath, _ConsoleAppExeName); string consoleAppArgs = string.Format( "deploy \"{0}\" \"{1}\" \"{2}\" \"{3}\" \"{4}\"", _deploymentInfo.ProjectName, _deploymentInfo.ProjectConfigurationName, _deploymentInfo.ProjectConfigurationBuildId, _deploymentInfo.TargetEnvironmentName, _deploymentInfo.IsSimulation ? "simulate" : ""); var processSecurityAttributes = new Win32Api.SECURITY_ATTRIBUTES(); var threadSecurityAttributes = new Win32Api.SECURITY_ATTRIBUTES(); var startupInfo = new Win32Api.STARTUPINFO(); Win32Api.PROCESS_INFORMATION processInformation; processSecurityAttributes.nLength = Marshal.SizeOf(processSecurityAttributes); threadSecurityAttributes.nLength = Marshal.SizeOf(threadSecurityAttributes); Win32Api.CreateProcess( consoleAppExePath, " " + consoleAppArgs, ref processSecurityAttributes, ref threadSecurityAttributes, false, Win32Api.NORMAL_PRIORITY_CLASS, IntPtr.Zero, consoleAppDstPath, ref startupInfo, out processInformation); PostDiagnosticMessage("UberDeployer.Agent has been requested to deploy itself - it could take a few seconds.", DiagnosticMessageType.Warn); }
//-------------------------------------------------------------------------------------------------- /* * Creates the two mutexes checked for by the installer/uninstaller to see if * the program is still running. * One of the mutexes is created in the global name space (which makes it * possible to access the mutex across user sessions in Windows XP); the other * is created in the session name space(because versions of Windows NT prior * to 4.0 TSE don't have a global name space and don't support the 'Global\' * prefix). */ void CreateInstanceMutexes() { const string mutexName = "MacadInstanceRunning"; /* By default on Windows NT, created mutexes are accessible only by the user * running the process. We need our mutexes to be accessible to all users, so * that the mutex detection can work across user sessions in Windows XP. To * do this we use a security descriptor with a null DACL. */ IntPtr ptrSecurityDescriptor = IntPtr.Zero; try { var securityDescriptor = new Win32Api.SECURITY_DESCRIPTOR(); Win32Api.InitializeSecurityDescriptor(out securityDescriptor, Win32Api.SECURITY_DESCRIPTOR_REVISION); Win32Api.SetSecurityDescriptorDacl(ref securityDescriptor, true, IntPtr.Zero, false); ptrSecurityDescriptor = Marshal.AllocCoTaskMem(Marshal.SizeOf(securityDescriptor)); Marshal.StructureToPtr(securityDescriptor, ptrSecurityDescriptor, false); var securityAttributes = new Win32Api.SECURITY_ATTRIBUTES(); securityAttributes.nLength = Marshal.SizeOf(securityAttributes); securityAttributes.lpSecurityDescriptor = ptrSecurityDescriptor; securityAttributes.bInheritHandle = false; if (Win32Api.CreateMutex(ref securityAttributes, false, mutexName) == IntPtr.Zero || Win32Api.CreateMutex(ref securityAttributes, false, @"Global\" + mutexName) == IntPtr.Zero) { var lastError = Win32Error.GetLastError(); // if we get the ERROR_ALREADY_EXISTS value, there is // already another instance of this application running. // That is ok and no error. if (lastError != Win32Error.ERROR_ALREADY_EXISTS) { Console.WriteLine($"InstanceMutex creation failed: {Marshal.GetLastWin32Error()}"); } } } catch (Exception e) { Console.WriteLine(e); } if (ptrSecurityDescriptor != IntPtr.Zero) { Marshal.FreeCoTaskMem(ptrSecurityDescriptor); } }
internal VirtualKey CreateKey(KeyIdentity identity, string lpClass, Win32Api.RegOption dwOptions, IntPtr lpSecurityAttributes) { // Security attributes and security descriptor are not the same IntPtr securityDescriptor = IntPtr.Zero; if (lpSecurityAttributes != IntPtr.Zero) { Win32Api.SECURITY_ATTRIBUTES securityAttributes = (Win32Api.SECURITY_ATTRIBUTES)Marshal.PtrToStructure( lpSecurityAttributes, typeof(Win32Api.SECURITY_ATTRIBUTES)); if (securityAttributes.nLength >= Marshal.SizeOf(typeof(UInt32)) + Marshal.SizeOf(typeof(IntPtr))) { securityDescriptor = securityAttributes.lpSecurityDescriptor; } } IKeyImpl registryKey = Create(identity, lpClass, dwOptions, securityDescriptor, IntPtr.Zero); return(new VirtualKey(this, identity, registryKey, alterer_)); }