private void PrepareCapture(Process[] processes, Softwall.Rule[] softwallRules) { progress.ProgressUpdate("Preparing capture", 100); startReqEvent = WinApi.CreateEvent (IntPtr.Zero, true, false, "oSpyAgentStartRequest"); stopReqEvent = WinApi.CreateEvent (IntPtr.Zero, true, false, "oSpyAgentStopRequest"); stopRespEvent = WinApi.CreateEvent (IntPtr.Zero, false, false, "oSpyAgentStopResponse"); fileMapping = WinApi.CreateFileMapping (0xFFFFFFFFu, IntPtr.Zero, WinApi.enumProtect.PAGE_READWRITE, 0, (uint)Marshal.SizeOf(typeof(Capture)), "Global\\oSpyCapture"); if (Marshal.GetLastWin32Error () == WinApi.ERROR_ALREADY_EXISTS) throw new Error("Is another instance of oSpy or one or more processes previously monitored still alive?"); cfgPtr = WinApi.MapViewOfFile (fileMapping, WinApi.enumFileMap.FILE_MAP_WRITE, 0, 0, (uint)Marshal.SizeOf (typeof (Capture))); // Create a temporary directory for the capture do { capturePath = String.Format("{0}{1}", Path.GetTempPath(), Path.GetRandomFileName()); } while (Directory.Exists(capturePath)); Directory.CreateDirectory(capturePath); // We need to know how many agents are still active when waiting for the uninjection to complete activeAgentCountPtr = (IntPtr) (cfgPtr.ToInt64 () + Marshal.OffsetOf (typeof (Capture), "ActiveAgentCount").ToInt64 ()); // Write the temporary directory to shared memory char[] tmpDirChars = capturePath.ToCharArray(); IntPtr ptr = (IntPtr)(cfgPtr.ToInt64() + Marshal.OffsetOf(typeof(Capture), "LogPath").ToInt64()); Marshal.Copy(tmpDirChars, 0, ptr, tmpDirChars.Length); // And make it NUL-terminated Marshal.WriteInt16(ptr, tmpDirChars.Length * Marshal.SizeOf(typeof(UInt16)), 0); // Initialize LogIndex and LogSize logIndexUserspacePtr = (IntPtr)(cfgPtr.ToInt64 () + Marshal.OffsetOf (typeof (Capture), "LogIndexUserspace").ToInt64 ()); logCountPtr = (IntPtr)(cfgPtr.ToInt64 () + Marshal.OffsetOf (typeof (Capture), "LogCount").ToInt64 ()); logSizePtr = (IntPtr)(cfgPtr.ToInt64 () + Marshal.OffsetOf (typeof (Capture), "LogSize").ToInt64 ()); Marshal.WriteInt32 (logIndexUserspacePtr, 0); Marshal.WriteInt32 (logCountPtr, 0); Marshal.WriteInt32 (logSizePtr, 0); // Initialize softwall rules Marshal.WriteInt32(cfgPtr, Marshal.OffsetOf(typeof(Capture), "NumSoftwallRules").ToInt32(), softwallRules.Length); ptr = (IntPtr)(cfgPtr.ToInt64() + Marshal.OffsetOf(typeof(Capture), "SoftwallRules").ToInt64()); foreach (Softwall.Rule rule in softwallRules) { Marshal.StructureToPtr(rule, ptr, false); ptr = (IntPtr)(ptr.ToInt64() + Marshal.SizeOf(typeof(Softwall.Rule))); } // Copy configuration XML string configPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\config.xml"; File.Copy(configPath, String.Format("{0}\\config.xml", capturePath)); }
public void StartCapture(Process[] processes, Softwall.Rule[] softwallRules, Device[] devices, IProgressFeedback progress) { this.processes = processes; this.softwallRules = softwallRules; this.devices = devices; this.progress = progress; Thread th = new Thread(StartCaptureThread); th.Start(); }