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)); }