public static int OpenApplication(ApplicationStartInfo application) { if (application == null) { throw new ArgumentNullException("application"); } if (string.IsNullOrEmpty(application.Application) || !System.IO.Directory.Exists(application.Application)) { throw new ArgumentException("Application is not valid"); } NSUrl appUrl = NSUrl.FromFilename(application.Application); // TODO: Once the above bug is fixed, we can replace the code below with //NSRunningApplication app = NSWorkspace.SharedWorkspace.LaunchApplication (appUrl, 0, new NSDictionary (), null); var config = new NSMutableDictionary(); if (application.Args != null && application.Args.Length > 0) { var args = new NSMutableArray(); foreach (string arg in application.Args) { args.Add(new NSString(arg)); } config.Add(new NSString("NSWorkspaceLaunchConfigurationArguments"), args); } if (application.Environment != null && application.Environment.Count > 0) { var envValueStrings = application.Environment.Values.Select(t => new NSString(t)).ToArray(); var envKeyStrings = application.Environment.Keys.Select(t => new NSString(t)).ToArray(); var envDict = new NSMutableDictionary(); for (int i = 0; i < envValueStrings.Length; i++) { envDict.Add(envKeyStrings[i], envValueStrings[i]); } config.Add(new NSString("NSWorkspaceLaunchConfigurationEnvironment"), envDict); } UInt32 options = 0; if (application.Async) { options |= (UInt32)LaunchOptions.NSWorkspaceLaunchAsync; } if (application.NewInstance) { options |= (UInt32)LaunchOptions.NSWorkspaceLaunchNewInstance; } IntPtr error; var appHandle = IntPtr_objc_msgSend_IntPtr_UInt32_IntPtr_IntPtr(NSWorkspace.SharedWorkspace.Handle, launchApplicationAtURLOptionsConfigurationErrorSelector, appUrl.Handle, options, config.Handle, out error); NSRunningApplication app = (NSRunningApplication)ObjCRuntime.Runtime.GetNSObject(appHandle); return(app.ProcessIdentifier); }
public static ProcessSerialNumber OpenApplication (ApplicationStartInfo application) { if (application == null) throw new ArgumentNullException ("application"); if (string.IsNullOrEmpty (application.Application) || !System.IO.Directory.Exists (application.Application)) throw new ArgumentException ("Application is not valid"); var appParams = new LSApplicationParameters (); if (application.NewInstance) appParams.flags |= LSLaunchFlags.NewInstance; if (application.Async) appParams.flags |= LSLaunchFlags.Async; NSArray argv = null; if (application.Args != null && application.Args.Length > 0) { var args = application.Args; NSObject[] arr = new NSObject[args.Length]; for (int i = 0; i < args.Length; i++) arr[i] = new NSString (args[i]); argv = NSArray.FromNSObjects (arr); appParams.argv = argv.Handle; } NSDictionary dict = null; if (application.Environment.Count > 0) { dict = new NSMutableDictionary (); foreach (var kvp in application.Environment) dict.SetValueForKey (new NSString (kvp.Value), new NSString (kvp.Key)); appParams.environment = dict.Handle; } var cfUrl = global::MonoMac.CoreFoundation.CFUrl.FromFile (application.Application); ProcessSerialNumber psn; try { appParams.application = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (FSRef))); if (!CoreFoundation.CFURLGetFSRef (cfUrl.Handle, appParams.application)) throw new Exception ("Could not create FSRef from CFUrl"); var status = LSOpenApplication (ref appParams, out psn); if (status != OSStatus.Ok) throw new Exception ("Failed to start process: " + ((int)status).ToString ()); } finally { if (appParams.application != IntPtr.Zero) Marshal.FreeHGlobal (appParams.application); appParams.application = IntPtr.Zero; if (dict != null) dict.Dispose (); //also ensures the NSDictionary is kept alive for the params if (argv != null) argv.Dispose (); //also ensures the NSArray is kept alive for the params } return psn; }
internal static NSRunningApplication OpenApplicationInternal(ApplicationStartInfo application) { if (application == null) { throw new ArgumentNullException(nameof(application)); } if (string.IsNullOrEmpty(application.Application) || !System.IO.Directory.Exists(application.Application)) { throw new ArgumentException("Application is not valid", nameof(application)); } NSUrl appUrl = NSUrl.FromFilename(application.Application); var config = new NSMutableDictionary(); if (application.Args != null && application.Args.Length > 0) { config.Add(NSWorkspace.LaunchConfigurationArguments, NSArray.FromStrings(application.Args)); } if (application.Environment != null && application.Environment.Count > 0) { var envValueStrings = application.Environment.Values.Select(t => new NSString(t)).ToArray(); var envKeyStrings = application.Environment.Keys.Select(t => new NSString(t)).ToArray(); config.Add(NSWorkspace.LaunchConfigurationEnvironment, NSDictionary.FromObjectsAndKeys(envValueStrings, envKeyStrings)); } NSWorkspaceLaunchOptions options = 0; if (application.Async) { options |= NSWorkspaceLaunchOptions.Async; } if (application.NewInstance) { options |= NSWorkspaceLaunchOptions.NewInstance; } if (application.HideFromRecentApps) { options |= NSWorkspaceLaunchOptions.WithoutAddingToRecents; } var app = NSWorkspace.SharedWorkspace.LaunchApplication(appUrl, options, config, out NSError error); if (app == null) { LoggingService.LogError(error.LocalizedDescription); } return(app); }
public static ProcessSerialNumber OpenApplication(ApplicationStartInfo application) { if (application == null) { throw new ArgumentNullException("application"); } if (string.IsNullOrEmpty(application.Application) || !System.IO.Directory.Exists(application.Application)) { throw new ArgumentException("Application is not valid"); } var appParams = new LSApplicationParameters(); if (application.NewInstance) { appParams.flags |= LSLaunchFlags.NewInstance; } if (application.Async) { appParams.flags |= LSLaunchFlags.Async; } NSArray argv = null; if (application.Args != null && application.Args.Length > 0) { var args = application.Args; NSObject[] arr = new NSObject[args.Length]; for (int i = 0; i < args.Length; i++) { arr[i] = new NSString(args[i]); } argv = NSArray.FromNSObjects(arr); appParams.argv = argv.Handle; } NSDictionary dict = null; if (application.Environment.Count > 0) { dict = new NSMutableDictionary(); foreach (var kvp in application.Environment) { dict.SetValueForKey(new NSString(kvp.Value), new NSString(kvp.Key)); } appParams.environment = dict.Handle; } var cfUrl = global::MonoMac.CoreFoundation.CFUrl.FromFile(application.Application); ProcessSerialNumber psn; try { appParams.application = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FSRef))); if (!CoreFoundation.CFURLGetFSRef(cfUrl.Handle, appParams.application)) { throw new Exception("Could not create FSRef from CFUrl"); } var status = LSOpenApplication(ref appParams, out psn); if (status != OSStatus.Ok) { throw new Exception("Failed to start process: " + ((int)status).ToString()); } } finally { if (appParams.application != IntPtr.Zero) { Marshal.FreeHGlobal(appParams.application); } appParams.application = IntPtr.Zero; if (dict != null) { dict.Dispose(); //also ensures the NSDictionary is kept alive for the params } if (argv != null) { argv.Dispose(); //also ensures the NSArray is kept alive for the params } } return(psn); }
public static int OpenApplication (ApplicationStartInfo application) { if (application == null) throw new ArgumentNullException ("application"); if (string.IsNullOrEmpty (application.Application) || !System.IO.Directory.Exists (application.Application)) throw new ArgumentException ("Application is not valid"); NSUrl appUrl = NSUrl.FromFilename (application.Application); // TODO: Once the above bug is fixed, we can replace the code below with //NSRunningApplication app = NSWorkspace.SharedWorkspace.LaunchApplication (appUrl, 0, new NSDictionary (), null); var config = new NSMutableDictionary (); if (application.Args != null && application.Args.Length > 0) { var args = new NSMutableArray (); foreach (string arg in application.Args) { args.Add (new NSString (arg)); } config.Add (new NSString ("NSWorkspaceLaunchConfigurationArguments"), args); } if (application.Environment != null && application.Environment.Count > 0) { var envValueStrings = application.Environment.Values.Select (t => new NSString (t)).ToArray (); var envKeyStrings = application.Environment.Keys.Select (t => new NSString (t)).ToArray (); var envDict = new NSMutableDictionary (); for (int i = 0; i < envValueStrings.Length; i++) { envDict.Add (envKeyStrings[i], envValueStrings[i]); } config.Add (new NSString ("NSWorkspaceLaunchConfigurationEnvironment"), envDict); } UInt32 options = 0; if (application.Async) options |= (UInt32) LaunchOptions.NSWorkspaceLaunchAsync; if (application.NewInstance) options |= (UInt32) LaunchOptions.NSWorkspaceLaunchNewInstance; IntPtr error; var appHandle = IntPtr_objc_msgSend_IntPtr_UInt32_IntPtr_IntPtr (NSWorkspace.SharedWorkspace.Handle, launchApplicationAtURLOptionsConfigurationErrorSelector, appUrl.Handle, options, config.Handle, out error); NSRunningApplication app = (NSRunningApplication)ObjCRuntime.Runtime.GetNSObject (appHandle); return app.ProcessIdentifier; }
public static int OpenApplication(ApplicationStartInfo application) => OpenApplicationInternal(application)?.ProcessIdentifier ?? -1;