/// <summary> /// 打开应用程序 /// </summary> /// <param name="appPath"></param> public static void AppStart(string appPath) { try { string appStartPath = appPath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) ApiDefinitions.CloseHandle(userTokenHandle); int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { LogHelpr.Error("打开程序时出错:" + ex.ToString()); } }
private void OpenApp4Service(string appName, string cmdLine) { try { //string appStartPath = @"C:\\Deepleo.exe"; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser(userTokenHandle, appName, cmdLine, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } //int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { _log.ErrorFormat("Start Application failed, its path is {0} ,exception: {1}", appName, ex.Message); } }
/// <summary> /// 切换进程2 /// </summary> /// <param name="killAppName"></param> /// <param name="openAppName"></param> /// <param name="openAppNamePath"></param> /// <param name="args"></param> /// <returns></returns> public static void ToggleProcess4UserSession(string killAppName, string openAppName, string openAppNamePath, string args) { //获得到进程,杀死进程 if (!string.IsNullOrEmpty(killAppName)) { Process[] processes = Process.GetProcessesByName(killAppName); if (processes.Length > 0) { _log.Info(string.Format("Kill进程:{0}", killAppName)); foreach (Process process in processes) { process.Kill(); } } } //判断要开启的进程是否已经开启,已经开启则无需开启 var startProcess = Process.GetProcessesByName(openAppName); if (startProcess.Length == 0) { if (!string.IsNullOrEmpty(openAppNamePath)) { //启动进程 if (File.Exists(openAppNamePath)) { try { IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser(userTokenHandle, openAppNamePath, args, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } //int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { _log.ErrorFormat("Start Application failed, its path is {0} ,exception: {1}", openAppNamePath, ex.Message); } } } } }
/// <summary> /// 服务启动程序 显示界面 /// </summary> private void HasUI() { string appName = "demo-winform"; string appPath = "G://demo-winform.exe"; Process[] localByName = Process.GetProcessesByName(appName); if (localByName.Length == 0) //如果得到的进程数是0, 那么说明程序未启动,需要启动程序 { try { IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { Console.WriteLine("启动程序失败!" + ex); } } else { //如果程序已经启动,则执行这一部分代码 } }
private int OpenForm(string filePath) { try { string appStartPath = filePath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)System.Runtime.InteropServices.Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } int processId = (int)procInfo.dwProcessId; return(processId); } catch (Exception ex) { Logs.Log("", "调用Cjwdev.WindowsApi时出错:" + ex.ToString()); } return(0); }
public void AppStart(string appPath) { try { string appStartPath = appPath; IntPtr userTokenHandle = IntPtr.Zero; ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle); ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION(); ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO(); startInfo.cb = (uint)Marshal.SizeOf(startInfo); ApiDefinitions.CreateProcessAsUser( userTokenHandle, appStartPath, "", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out procInfo); if (userTokenHandle != IntPtr.Zero) { ApiDefinitions.CloseHandle(userTokenHandle); } int _currentAquariusProcessId = (int)procInfo.dwProcessId; } catch (Exception ex) { logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); logger("AppStart failed! " + ex.ToString()); } }
/// <summary> /// Get the Api Definition based on the name given /// </summary> /// <param name="name">The name of the definition</param> /// <returns>The Api Definition</returns> public ApiDefinition Api(String name) => ApiDefinitions.Where(item => item.Name == name).FirstOrDefault();
/// <summary> /// Get the Api Definition based on the Id given /// </summary> /// <param name="id">The Id of the definition</param> /// <returns>The Api Definition</returns> public ApiDefinition Api(Guid id) => ApiDefinitions.Where(item => item.Id == id).FirstOrDefault();
/// <summary> /// Save a piece of information to the appropriate package element /// </summary> /// <typeparam name="T">The type of data to be saved</typeparam> /// <param name="dataToSave">The data to be saved</param> /// <returns>The saved data</returns> public T Save <T>(T dataToSave) where T : CommonObject { // Get the type of data to be saved String typeOfData = typeof(T).ToShortName(); // Based on the type of data, save it to the correct repository element switch (typeOfData) { case "apidefinition": // Get the actual value from the object wrapper ApiDefinition apiDefinition = (ApiDefinition)Convert.ChangeType(dataToSave, typeof(ApiDefinition)); // If the type is not null if (apiDefinition != null) { // Does this api definition already exist? ApiDefinition existingApiDefinition = (apiDefinition.Id == Guid.Empty) ? null : this.Api(apiDefinition.Id); // No API Definition found? if (existingApiDefinition == null) { // Doesn't exist currently so create a new Id // and assign the object as the "existing" api definition existingApiDefinition = apiDefinition; existingApiDefinition.Id = Guid.NewGuid(); // Add this new api definition to the repository ApiDefinitions.Add(existingApiDefinition); } else { // Assign the values from the item to save existingApiDefinition.Description = apiDefinition.Description; existingApiDefinition.Name = apiDefinition.Name; existingApiDefinition.LastUpdated = DateTime.Now; // Assign the foreign keys existingApiDefinition.DataConnection = apiDefinition.DataConnection; existingApiDefinition.DataDefinition = apiDefinition.DataDefinition; existingApiDefinition.CredentialsLinks = apiDefinition.CredentialsLinks; } // Convert the data back to the return data type (which is actually the same) dataToSave = (T)Convert.ChangeType(existingApiDefinition, typeof(T)); } break; case "dataitemdefinition": // Get the actual value from the object wrapper DataItemDefinition dataItemDefinition = (DataItemDefinition)Convert.ChangeType(dataToSave, typeof(DataItemDefinition)); // If the type is not null if (dataItemDefinition != null) { // Does this data definition already exist? DataItemDefinition existingDataItemDefinition = (dataItemDefinition.Id == Guid.Empty) ? null : this.DataDefinition(dataItemDefinition.Id); // No data definition found? if (existingDataItemDefinition == null) { // Doesn't exist currently so create a new Id // and assign the object as the "existing" data definition existingDataItemDefinition = dataItemDefinition; existingDataItemDefinition.Id = Guid.NewGuid(); // Add this new data definition to the repository DataDefinitions.Add(existingDataItemDefinition); } else { // Assign the values from the item to save existingDataItemDefinition.Description = dataItemDefinition.Description; existingDataItemDefinition.Name = dataItemDefinition.Name; existingDataItemDefinition.Culture = dataItemDefinition.Culture; existingDataItemDefinition.EncodingFormat = dataItemDefinition.EncodingFormat; existingDataItemDefinition.LastUpdated = DateTime.Now; // Assign the lists existingDataItemDefinition.ItemProperties = dataItemDefinition.ItemProperties; existingDataItemDefinition.PropertyBag = dataItemDefinition.PropertyBag; } // Convert the data back to the return data type (which is actually the same) dataToSave = (T)Convert.ChangeType(existingDataItemDefinition, typeof(T)); } break; case "dataconnection": // Get the actual value from the object wrapper DataConnection connection = (DataConnection)Convert.ChangeType(dataToSave, typeof(DataConnection)); // If the type is not null if (connection != null) { // Does this connection already exist? DataConnection existingConnection = (connection.Id == Guid.Empty) ? null : DataConnection(connection.Id); // No connection found? if (existingConnection == null) { // Doesn't exist currently so create a new Id // and assign the object as the "existing" connection existingConnection = connection; existingConnection.Id = Guid.NewGuid(); // Add this new connection to the repository DataConnections.Add(existingConnection); } else { // Assign the values from the item to save existingConnection.ConnectionString = connection.ConnectionString; existingConnection.Description = connection.Description; existingConnection.Name = connection.Name; existingConnection.ProviderType = connection.ProviderType; existingConnection.Credentials = connection.Credentials; existingConnection.LastUpdated = DateTime.Now; existingConnection.PropertyBag = connection.PropertyBag; existingConnection.ObjectName = connection.ObjectName; } // Convert the data back to the return data type (which is actually the same) dataToSave = (T)Convert.ChangeType(existingConnection, typeof(T)); } break; case "transformation": // Get the actual value from the object wrapper Transformation transformation = (Transformation)Convert.ChangeType(dataToSave, typeof(Transformation)); // If the type is not null if (transformation != null) { // Does this transformation already exist? Transformation existingTransformation = (transformation.Id == Guid.Empty) ? null : Transformation(transformation.Id); // No transformation found? if (existingTransformation == null) { // Doesn't exist currently so create a new Id // and assign the object as the "existing" transformation existingTransformation = transformation; existingTransformation.Id = Guid.NewGuid(); // Add this new transformation to the repository Transformations.Add(existingTransformation); } else { // Assign the values from the item to save existingTransformation.Description = transformation.Description; existingTransformation.Name = transformation.Name; existingTransformation.LastUpdated = DateTime.Now; } // Convert the data back to the return data type (which is actually the same) dataToSave = (T)Convert.ChangeType(existingTransformation, typeof(T)); } break; case "credentials": // Get the actual value from the object wrapper Credentials credentials = (Credentials)Convert.ChangeType(dataToSave, typeof(Credentials)); // If the type is not null if (credentials != null) { // Does this set of credentials already exist? Credentials existingCredentials = (credentials.Id == Guid.Empty) ? null : this.Credentials(credentials.Id); // Loop the properties and see if any have not been saved before. If not give them an Id credentials.Properties.ForEach(credential => { if (credential.Id == null) { credential.Id = Guid.NewGuid(); } }); // No credentials found? if (existingCredentials == null) { // Doesn't exist currently so create a new Id // and assign the object as the "existing" credentials existingCredentials = credentials; existingCredentials.Id = Guid.NewGuid(); // Add this new credentials to the repository CredentialsStore.Add(existingCredentials); } else { // Assign the values from the item to save existingCredentials.Description = credentials.Description; existingCredentials.Name = credentials.Name; existingCredentials.LastUpdated = DateTime.Now; // Assign the lists existingCredentials.Properties = credentials.Properties; } // Convert the data back to the return data type (which is actually the same) dataToSave = (T)Convert.ChangeType(existingCredentials, typeof(T)); } break; } // Make sure as the item is saved to this package that the // reference to it is inside the object dataToSave.ParentPackage = this; // Return the data that was saved return(dataToSave); }