示例#1
0
        /// <summary>
        /// Starts the debug string capture
        /// <param name="filename">The file of the zipfile to capture the debug string output in</param>
        /// <param name="process">The process to filter the debug string output for</param>
        /// </summary>
        public static void StartCapture(string filename, Process process)
        {
            m_Filename = filename;

            if (process == null)
            {
                m_PID = 0;
            }
            else
            {
                m_PID = process.Id;
            }

            lock (m_SyncRoot)
            {
                if (m_Capturer != null)
                {
                    throw new ApplicationException("DebugString capture is already started");
                }

                // Check for multiple instances
                m_Mutex = new Mutex(false, typeof(DebugString).Namespace, out bool createdNew);
                if (!createdNew)
                {
                    throw new ApplicationException("There is already an instance running");
                }

                //NM.SECURITY_ATTRIBUTES sa = new NM.SECURITY_ATTRIBUTES();
                //sa.lpSecurityDescriptor = IntPtr.Zero;
                IntPtr sa = IntPtr.Zero;

                // Create the event for slot 'DBWIN_BUFFER_READY'
                m_AckEvent = NM.CreateEvent(sa, false, false, "DBWIN_BUFFER_READY");
                if (m_AckEvent == IntPtr.Zero)
                {
                    throw CreateApplicationException("Failed to create event 'DBWIN_BUFFER_READY'");
                }

                // Create the event for slot 'DBWIN_DATA_READY'
                m_ReadyEvent = NM.CreateEvent(sa, false, false, "DBWIN_DATA_READY");
                if (m_ReadyEvent == IntPtr.Zero)
                {
                    throw CreateApplicationException("Failed to create event 'DBWIN_DATA_READY'");
                }

                // Get a handle to the readable shared memory at slot 'DBWIN_BUFFER'.
                m_SharedFile = NM.CreateFileMapping(new IntPtr(-1), sa, NM.PageProtection.ReadWrite, 0, 4096, "DBWIN_BUFFER");
                if (m_SharedFile == IntPtr.Zero)
                {
                    throw CreateApplicationException("Failed to create a file mapping to slot 'DBWIN_BUFFER'");
                }

                // Create a view for this file mapping so we can access it
                m_SharedMem = NM.MapViewOfFile(m_SharedFile, NM.FileMapAccess.FileMapRead, 0, 0, new IntPtr(512));
                if (m_SharedMem == IntPtr.Zero)
                {
                    throw CreateApplicationException("Failed to create a mapping view for slot 'DBWIN_BUFFER'");
                }

                //create the file
                m_tw = new StreamWriter(Path.GetDirectoryName(m_Filename) + @"\" + Path.GetFileNameWithoutExtension(m_Filename) + @".txt");

                // Start a new thread where we can capture the output
                // of OutputDebugString calls so we don't block here.
                m_Capturer = new Thread(new ThreadStart(Capture));
                m_Capturer.Start();
            }
        }