private static int FindPortByWinMsg()
        {
            //Start message queue
            Win32Wrapper.PeekMessage(out Win32Wrapper.NativeMessage msg, 0, (uint)0x600, (uint)0x600, Win32Wrapper.PM_REMOVE);

            Trace.WriteLine("Prepare message for Stealth", "Stealth.Main");
            var procstring = Process.GetCurrentProcess().Id.ToString("X8") + Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost", "") + '\0';

            Trace.WriteLine(string.Format("Procstring: {0}", procstring), "Stealth.Main");
            var aCopyData = new Win32Wrapper.CopyDataStruct
            {
                dwData = (uint)Win32Wrapper.GetCurrentThreadId(),
                cbData = Process.GetCurrentProcess().MainModule.FileName.Replace(".vshost", "").Length + 8 + 1,
                lpData = Marshal.StringToHGlobalAnsi(procstring)
            };

            IntPtr copyDataPtr = aCopyData.MarshalToPtr();

            try
            {
                Trace.WriteLine("Find Stealth window", "Stealth.Main");
                IntPtr tWndPtr = Win32Wrapper.FindWindowEx(IntPtr.Zero, IntPtr.Zero, "TStealthForm", null);
                if (tWndPtr != IntPtr.Zero)
                {
                    Trace.WriteLine("Stealth window found. Send message.", "Stealth.Main");
                    IntPtr ret = Win32Wrapper.SendMessage(tWndPtr, Win32Wrapper.WM_COPYDATA, IntPtr.Zero, copyDataPtr);
                    Trace.WriteLine("Message sended. Wait message from Stealth.", "Stealth.Main");
                    while (!Win32Wrapper.PeekMessage(out msg, 0, (uint)0x600, (uint)0x600, Win32Wrapper.PM_REMOVE))
                    {
                        Thread.Sleep(10);
                    }
                    Trace.WriteLine(string.Format("Message recieved. Port: {0}", (int)(msg.wParam)), "Stealth.Main");
                }
                else
                {
                    throw new InvalidOperationException("Could not attach to Stealth. Exiting.");
                }
            }
            catch (InvalidOperationException)
            {
                return(-1);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex, "Stealth.Main");
                return(-1);
            }
            finally
            {
                Marshal.FreeHGlobal(copyDataPtr);
            }

            return((int)(msg.wParam));
        }
예제 #2
0
        public static void SetDesktopAsParent(IntPtr handle)
        {
            PrintVisibleWindowHandles(2);
            // The output will look something like this.
            // .....
            // 0x00010190 "" WorkerW
            //   ...
            //   0x000100EE "" SHELLDLL_DefView
            //     0x000100F0 "FolderView" SysListVieWin32Wrapper
            // 0x000100EC "Program Manager" Progman



            // Fetch the Progman window
            IntPtr progman = Win32Wrapper.FindWindow("Progman", null);

            IntPtr result = IntPtr.Zero;

            // Send 0x052C to Progman. This message directs Progman to spawn a
            // WorkerW behind the desktop icons. If it is already there, nothing
            // happens.
            Win32Wrapper.SendMessageTimeout(progman,
                                            0x052C,
                                            new IntPtr(0),
                                            IntPtr.Zero,
                                            Win32Wrapper.SendMessageTimeoutFlags.SMTO_NORMAL,
                                            1000,
                                            out result);


            PrintVisibleWindowHandles(2);
            // The output will look something like this
            // .....
            // 0x00010190 "" WorkerW
            //   ...
            //   0x000100EE "" SHELLDLL_DefView
            //     0x000100F0 "FolderView" SysListVieWin32Wrapper
            // 0x00100B8A "" WorkerW                                   <--- This is the WorkerW instance we are after!
            // 0x000100EC "Program Manager" Progman

            // We enumerate all Windows, until we find one, that has the SHELLDLL_DefView
            // as a child.
            // If we found that window, we take its next sibling and assign it to workerw.
            Win32Wrapper.EnumWindows(new Win32Wrapper.EnumWindowsProc((tophandle, topparamhandle) =>
            {
                IntPtr p = Win32Wrapper.FindWindowEx(tophandle,
                                                     IntPtr.Zero,
                                                     "SHELLDLL_DefView",
                                                     null);

                if (p != IntPtr.Zero)
                {
                    // Gets the WorkerW Window after the current one.
                    _workerw = Win32Wrapper.FindWindowEx(IntPtr.Zero,
                                                         tophandle,
                                                         "WorkerW",
                                                         null);
                    return(false);
                }

                return(true);
            }), IntPtr.Zero);

            Win32Wrapper.SetParent(handle, _workerw);
        }