private static void Main() { // 捕获未处理异常 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; // 调试器 if (Global.LoadDebugger) { ServiceLocator.DebuggerContainer.Hook(); } ServiceLocator.GlobalLogger.Info("Application Start"); // 隐藏Console 窗口 Console.Title = Guid.NewGuid().ToString(); IntPtr intptr = NativeMethod.FindWindow("ConsoleWindowClass", Console.Title); if (intptr != IntPtr.Zero) { NativeMethod.ShowWindow(intptr, Global.ShowConsole); //隐藏这个窗口 } // 启动引擎 ServiceLocator.ClientEngine.Start(); //显示窗口 并运行 ServiceLocator.Window.Show(); //关闭窗口后停止运行 ServiceLocator.ClientEngine.Stop(); ServiceLocator.GlobalLogger.Info("Application Exit"); }
private void Application_Startup(object sender, StartupEventArgs e) { if (System.Diagnostics.Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count() > 1) { IntPtr hTargetWnd = NativeMethod.FindWindow(null, "window1"); if (hTargetWnd == IntPtr.Zero) { return; } MyStruct myStruct; myStruct.Message = "Show Up"; int myStructSize = Marshal.SizeOf(myStruct); IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize); try { Marshal.StructureToPtr(myStruct, pMyStruct, true); COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.cbData = myStructSize; cds.lpData = pMyStruct; NativeMethod.SendMessage(hTargetWnd, WM_COPYDATA, new IntPtr(), ref cds); int result = Marshal.GetLastWin32Error(); if (result != 0) { } } finally { Marshal.FreeHGlobal(pMyStruct); } Application.Current.Shutdown(0); } }
public static IntPtr GetBackMostHanndle() { return(NativeMethod.FindWindowEx( NativeMethod.FindWindowEx(NativeMethod.FindWindow("Progman", "Program Manager"), IntPtr.Zero, "SHELLDLL_DefView", ""), IntPtr.Zero, "SysListView32", "FolderView")); }
private void btnSendMessage_Click(object sender, EventArgs e) { // Find the target window handle. IntPtr hTargetWnd = NativeMethod.FindWindow(null, "CSReceiveWM_COPYDATA"); if (hTargetWnd == IntPtr.Zero) { MessageBox.Show("Unable to find the \"CSReceiveWM_COPYDATA\" window", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Prepare the COPYDATASTRUCT struct with the data to be sent. MyStruct myStruct; int nNumber; if (!int.TryParse(this.tbNumber.Text, out nNumber)) { MessageBox.Show("Invalid value of Number!"); return; } myStruct.Number = nNumber; myStruct.Message = this.tbMessage.Text; // Marshal the managed struct to a native block of memory. int myStructSize = Marshal.SizeOf(myStruct); IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize); try { Marshal.StructureToPtr(myStruct, pMyStruct, true); COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.cbData = myStructSize; cds.lpData = pMyStruct; // Send the COPYDATASTRUCT struct through the WM_COPYDATA message to // the receiving window. (The application must use SendMessage, // instead of PostMessage to send WM_COPYDATA because the receiving // application must accept while it is guaranteed to be valid.) NativeMethod.SendMessage(hTargetWnd, WM_COPYDATA, this.Handle, ref cds); int result = Marshal.GetLastWin32Error(); if (result != 0) { MessageBox.Show(String.Format( "SendMessage(WM_COPYDATA) failed w/err 0x{0:X}", result)); } } finally { Marshal.FreeHGlobal(pMyStruct); } }
private void btnSendMessage_Click(object sender, EventArgs e) { // 找到目标窗口句柄 IntPtr hTargetWnd = NativeMethod.FindWindow(null, "CSReceiveWM_COPYDATA"); if (hTargetWnd == IntPtr.Zero) { MessageBox.Show("不能发现 \"CSReceiveWM_COPYDATA\" 窗口", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // 准备好用来发送的带数据的COPYDATASTRUCT(COPYDATASTRUCT) MyStruct myStruct; int nNumber; if (!int.TryParse(this.tbNumber.Text, out nNumber)) { MessageBox.Show("无效的数值!"); return; } myStruct.Number = nNumber; myStruct.Message = this.tbMessage.Text; // 封装托管结构到本地内存块 int myStructSize = Marshal.SizeOf(myStruct); IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize); try { Marshal.StructureToPtr(myStruct, pMyStruct, true); COPYDATASTRUCT cds = new COPYDATASTRUCT(); cds.cbData = myStructSize; cds.lpData = pMyStruct; // 通过WM_COPYDATA消息发送COPYDATASTRUCT结构到接受窗口 // (应用程序必须用SendMessage代替PostMessage 发送WM_COPYDATA // 因为接受程序必须接受,而这是有保证的。) NativeMethod.SendMessage(hTargetWnd, WM_COPYDATA, this.Handle, ref cds); int result = Marshal.GetLastWin32Error(); if (result != 0) { MessageBox.Show(String.Format( "发送消息(WM_COPYDATA) 失败 w/err 0x{0:X}", result)); } } finally { Marshal.FreeHGlobal(pMyStruct); } }
/// <summary>クラス名からウィンドウを検索します。検索対象はトップウィンドウのみ</summary> public static HWND FindByClassName(string className) { return(new HWND(NativeMethod.FindWindow(className, null))); }
/// <summary> /// ウインドウのタイトルからハンドルを取得します /// </summary> /// <param name="className"></param> /// <param name="windowName"></param> /// <returns></returns> public static IntPtr FindWindowByName(String className, String windowName) { return(NativeMethod.FindWindow(className, windowName)); }