Ejemplo n.º 1
0
 public static void SendMessageToWindow(IntPtr hwnd, int message, string parameter)
 {
     CopyDataStruct cds = new CopyDataStruct();
     try
     {
         cds.cbData = (parameter.Length + 1) * 2;
         cds.lpData = UnsafeNativeMethods.LocalAlloc(0x40, cds.cbData);
         Marshal.Copy(parameter.ToCharArray(), 0, cds.lpData, parameter.Length);
         cds.dwData = (IntPtr)1;
         UnsafeNativeMethods.SendMessage(hwnd, message, IntPtr.Zero, ref cds);
     }
     finally
     {
         cds.Dispose();
     }
 }
Ejemplo n.º 2
0
        public static void SendMessageToWindow(IntPtr hwnd, int message, string parameter)
        {
            CopyDataStruct cds = new CopyDataStruct();

            try
            {
                cds.cbData = (parameter.Length + 1) * 2;
                cds.lpData = UnsafeNativeMethods.LocalAlloc(0x40, cds.cbData);
                Marshal.Copy(parameter.ToCharArray(), 0, cds.lpData, parameter.Length);
                cds.dwData = (IntPtr)1;
                UnsafeNativeMethods.SendMessage(hwnd, message, IntPtr.Zero, ref cds);
            }
            finally
            {
                cds.Dispose();
            }
        }
Ejemplo n.º 3
0
        public static void Send(string msg, int receive_ID)
        {
            IntPtr         targetHWnd = (IntPtr)receive_ID;
            CopyDataStruct cds        = new CopyDataStruct();

            try
            {
                cds.cbData = (msg.Length + 1) * 2;
                cds.lpData = LocalAlloc(0x40, cds.cbData);
                Marshal.Copy(msg.ToCharArray(), 0, cds.lpData, msg.Length);
                cds.dwData = (IntPtr)1;
                SendMessage(targetHWnd, WM_COPYDATA, IntPtr.Zero, ref cds);
            }
            finally
            {
                cds.Dispose();
            }
        }
Ejemplo n.º 4
0
		/// <summary>
		/// Sends string arguments to running instance of World Wind.
		/// </summary>
		/// <param name="args"></param>
		/// <returns></returns>
		public static bool SendArgs(IntPtr targetHWnd, string args)
		{
			if (targetHWnd == IntPtr.Zero)
				return false;

			CopyDataStruct cds = new CopyDataStruct();
			try
			{
				cds.cbData = (args.Length + 1) * 2;
				cds.lpData = NativeMethods.LocalAlloc(0x40, cds.cbData);
				Marshal.Copy(args.ToCharArray(), 0, cds.lpData, args.Length);
				cds.dwData =  (IntPtr) 1;

				return SendMessage( targetHWnd, WM_COPYDATA, /*Handle*/System.IntPtr.Zero, ref cds );
			}
			finally
			{
				cds.Dispose();
			}
		}
Ejemplo n.º 5
0
        /// <summary>
        /// Sends string arguments to running instance of World Wind.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static bool SendArgs(IntPtr targetHWnd, string args)
        {
            if (targetHWnd == IntPtr.Zero)
            {
                return(false);
            }

            CopyDataStruct cds = new CopyDataStruct();

            try
            {
                cds.cbData = (args.Length + 1) * 2;
                cds.lpData = NativeMethods.LocalAlloc(0x40, cds.cbData);
                Marshal.Copy(args.ToCharArray(), 0, cds.lpData, args.Length);
                cds.dwData = (IntPtr)1;

                return(SendMessage(targetHWnd, WM_COPYDATA, /*Handle*/ System.IntPtr.Zero, ref cds));
            }
            finally
            {
                cds.Dispose();
            }
        }
Ejemplo n.º 6
0
        private bool SendCmd(string args)
        {
            ResponseBuffer = "";

            byte[]         bytes;
            CopyDataStruct cds = new CopyDataStruct();

            bytes = System.Text.Encoding.ASCII.GetBytes(args + "\x00");

            try
            {
                cds.cbData = bytes.Length;
                cds.lpData = LocalAlloc(0x40, cds.cbData);
                Marshal.Copy(bytes, 0, cds.lpData, bytes.Length);
                cds.dwData = (IntPtr)3;
                SendMessage((IntPtr)IDA_HWND, WM_COPYDATA, IntPtr.Zero, ref cds);
            }
            finally
            {
                cds.Dispose();
            }

            return(true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Sends string to window by WM_COPYDATA
        /// </summary>
        /// <param name="targetHWnd"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static bool SendArgs(IntPtr targetHWnd, string args)
        {
            CopyDataStruct cds = new CopyDataStruct();
            try
            {
                cds.cbData = (args.Length + 1) * 2;
                cds.lpData = LocalAlloc(0x40, cds.cbData);
                Marshal.Copy(args.ToCharArray(), 0, cds.lpData, args.Length);
                cds.dwData = (IntPtr)1;
                SendMessage(targetHWnd, WM_COPYDATA, IntPtr.Zero, ref cds);
            }
            finally
            {
                cds.Dispose();
            }

            return true;
        }
Ejemplo n.º 8
0
        private static void Main(string[] args)
        {
            var filepaths = new List <string>();

            try
            {
                var showHelp = false;

                // First order of business is to parse commandline args
                // Do this before loading configurations because the data directory
                // may be passed as an argument.
                var options = new OptionSet()
                {
                    { "a|app=", "The application's source directory.", v => _appDir = v },
                    { "d|data=", "The application's data storage directory.", v => _dataDir = v },
                    { "vs", "Whether this instance is run from Visual Studio.", v => _runningInVs = v != null },
                    { "h|help", v => showHelp = v != null }
                };
                var extra = options.Parse(args);

                if (showHelp)
                {
                    NativeMethods.AttachConsole(NativeMethods.ATTACH_PARENT_PROCESS);
                    Console.Write(Strings.CmdLineHelpMessage);
                    NativeMethods.FreeConsole();
                    return;
                }

                if (extra.Count > 0)
                {
                    filepaths = extra.Where(arg => File.Exists(arg)).ToList();
                }
            }
            catch (Exception e)
            {
                NativeMethods.AttachConsole(NativeMethods.ATTACH_PARENT_PROCESS);
                Console.Write(Strings.CmdLineParseError, e.Message);
                NativeMethods.FreeConsole();
                return;
            }

            // Initialize
            Init();

            // Allow only a single process in this section at a time.
            if (_mutex.WaitOne(-1, true))
            {
                var instances = GetApplicationInstances();

                if (instances.Any())
                {
                    var firstInstance = instances.First();

                    // This will bring the top most form into the foreground. Top most being the one most recently activated/focused.
                    _ = NativeMethods.SetForegroundWindow(firstInstance.MainWindowHandle);

                    // If the window is minimized then restore it
                    var windowPlacement = NativeMethods.GetPlacement(firstInstance.MainWindowHandle);
                    if (windowPlacement.showCmd == (int)CmdShow.SW_SHOWMINIMIZED)
                    {
                        NativeMethods.ShowWindow(firstInstance.MainWindowHandle, CmdShow.SW_SHOWNORMAL);
                    }

                    if (!SingleInstance)
                    {
                        // Since cef doesn't not allow multiple processes to load the same Data/Cache directory, we must use
                        // the same existing process to open a new window. We do this by sending a message to the existing process.

                        // The top most form will receive and process the message.
                        // Note: The dev console is a separate form and if that is the top most then this will not work.
                        var cds = new CopyDataStruct();
                        try
                        {
                            var data = "Cmd:New Window";
                            cds.cbData = (UIntPtr)((data.Length + 1) * 2);                                     // Number of bytes
                            cds.lpData = NativeMethods.LocalAlloc(LocalMemoryFlags.LMEM_ZEROINIT, cds.cbData); // Known local-pointer in RAM.

                            if (cds.lpData == IntPtr.Zero)
                            {
                                throw new OutOfMemoryException();
                            }

                            Marshal.Copy(data.ToCharArray(), 0, cds.lpData, data.Length); // Copy data to preserved local-pointer
                            cds.dwData = (IntPtr)1;
                            NativeMethods.SendMessage(firstInstance.MainWindowHandle, NativeMethods.WM_COPYDATA, IntPtr.Zero, ref cds);
                        }
                        finally
                        {
                            cds.Dispose();
                        }
                    }

                    if (filepaths.Count > 0)
                    {
                        // Send messages to open the files
                        foreach (var filepath in filepaths)
                        {
                            var cds = new CopyDataStruct();
                            try
                            {
                                var data = "Cmd:Open File:" + filepath;
                                cds.cbData = (UIntPtr)((data.Length + 1) * 2);                                     // Number of bytes
                                cds.lpData = NativeMethods.LocalAlloc(LocalMemoryFlags.LMEM_ZEROINIT, cds.cbData); // Known local-pointer in RAM.

                                if (cds.lpData == IntPtr.Zero)
                                {
                                    throw new OutOfMemoryException();
                                }

                                Marshal.Copy(data.ToCharArray(), 0, cds.lpData, data.Length); // Copy data to preserved local-pointer
                                cds.dwData = (IntPtr)1;
                                NativeMethods.SendMessage(firstInstance.MainWindowHandle, NativeMethods.WM_COPYDATA, IntPtr.Zero, ref cds);
                            }
                            finally
                            {
                                cds.Dispose();
                            }
                        }
                    }

                    _mutex.ReleaseMutex();
                    return;
                }

                // For Windows 7 and above, best to include relevant app.manifest entries as well
                Cef.EnableHighDPISupport();

                using (var settings = new CefSettings()
                {
                    // By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
                    CachePath = CacheDirectory
                })
                {
                    settings.RegisterScheme(new CefCustomScheme()
                    {
                        SchemeName           = FileSystemSchemeHandlerFactory.SchemeName,
                        SchemeHandlerFactory = new FileSystemSchemeHandlerFactory()
                    });

                    // Perform dependency check to make sure all relevant resources are in our output directory.
                    Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
                }

                // Setup default json serialization settings.
                JsonConvert.DefaultSettings = () =>
                                              new JsonSerializerSettings
                {
                    DateTimeZoneHandling           = DateTimeZoneHandling.Utc,
                    Formatting                     = Formatting.Indented,
                    NullValueHandling              = NullValueHandling.Ignore,
                    TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
                };

                // Load SqlServer types
                SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

                using (var appContext = new AppContext(filepaths))
                {
                    // Release mutex because WinForms.Application.Run will block
                    _mutex.ReleaseMutex();
                    Application.Run(appContext);
                }
            }
        }
Ejemplo n.º 9
0
        private bool SendCmd(string args)
        {
            ResponseBuffer = "";
            byte[] bytes;
            CopyDataStruct cds = new CopyDataStruct();

            bytes = System.Text.Encoding.ASCII.GetBytes(args + "\x00");

            try
            {
                cds.cbData = bytes.Length;
                cds.lpData = LocalAlloc(0x40, cds.cbData);
                Marshal.Copy(bytes, 0, cds.lpData, bytes.Length);
                cds.dwData = (IntPtr)3;
                SendMessage((IntPtr)IDA_HWND, WM_COPYDATA, IntPtr.Zero, ref cds);
            }
            finally
            {
                cds.Dispose();
            }

            return true;
        }