public void Monitor() { if (!kernel32.SetConsoleCtrlHandler(new kernel32.HandlerRoutine(HandlerRoutine), true)) { Console.WriteLine("[-] {0} 0x{1:X}", "SetConsoleCtrlHandler Failed", Marshal.GetLastWin32Error()); Console.WriteLine("[-] {0}\n", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message); return; } lpfnWndProcDelegate lpfnWndProc = FnWndProc; Winuser.WNDCLASSEX lpwcx = Winuser.WNDCLASSEX.Build(); lpwcx.lpfnWndProc = lpfnWndProc; lpwcx.lpszClassName = szName + "_Window"; lpClassName = user32.RegisterClassEx(ref lpwcx); if (0 == lpClassName) { Console.WriteLine("[-] {0} 0x{1:X}", "RegisterClassEx Failed", Marshal.GetLastWin32Error()); Console.WriteLine("[-] {0}\n", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message); return; } Console.WriteLine("[+] Registered Class 0x{0}", lpClassName.ToString("X4")); hWndClipboard = user32.CreateWindowEx(Winuser.WindowStylesEx.WS_EX_RIGHTSCROLLBAR, new IntPtr(lpClassName), "Monitor", 0, 0, 0, 0, 0, Winuser.HWND_MESSAGE, IntPtr.Zero, hInstance, IntPtr.Zero); if (IntPtr.Zero == hWndClipboard || !user32.IsWindow(hWndClipboard)) { Console.WriteLine("[-] {0} 0x{1:X}", "CreateWindowEx Failed", Marshal.GetLastWin32Error()); Console.WriteLine("[-] {0}\n", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message); return; } Console.WriteLine("[+] Created Window 0x{0}", hWndClipboard.ToString("X4")); hWndViewer = user32.SetClipboardViewer(hWndClipboard); if (IntPtr.Zero == hWndViewer) { Console.WriteLine("[-] {0} 0x{1:X}", "SetClipboardViewer Failed", Marshal.GetLastWin32Error()); Console.WriteLine("[-] {0}\n", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message); return; } Console.WriteLine("[+] Created Clipboard Viewer 0x{0}", hWndViewer.ToString("X4")); Winuser.tagMSG lpMsg = new Winuser.tagMSG(); Boolean result; do { Console.WriteLine("Waiting"); result = user32.GetMessage(ref lpMsg, hWndViewer, Winuser.WM_QUIT, Winuser.WM_CHANGECBCHAIN); if (result) { user32.TranslateMessage(ref lpMsg); user32.DispatchMessage(ref lpMsg); } else { Console.WriteLine("[-] {0} 0x{1:X}", "GetMessage Failed", Marshal.GetLastWin32Error()); Console.WriteLine("[-] {0}\n", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message); Console.WriteLine("Press Any Key to Continue"); Console.ReadKey(); } }while (result); if (!user32.ChangeClipboardChain(hWndClipboard, hWndViewer)) { Console.WriteLine("[-] {0} 0x{1:X}", "ChangeClipboardChain Failed", Marshal.GetLastWin32Error()); Console.WriteLine("[-] {0}\n", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message); return; } kernel32.SetConsoleCtrlHandler(new kernel32.HandlerRoutine(HandlerRoutine), false); if (!user32.DestroyWindow(hWndClipboard)) { Console.WriteLine("[-] {0} 0x{1:X}", "DestroyWindow Failed", Marshal.GetLastWin32Error()); Console.WriteLine("[-] {0}\n", new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message); return; } }
public static extern UInt16 RegisterClassEx(ref Winuser.WNDCLASSEX lpwcx);
public static extern ushort RegisterClassEx(ref Winuser.WNDCLASSEX lpwcx);