Exemplo n.º 1
0
        internal static IEnumerable <Native.ModuleEntry> GetProcessModules(int processId)
        {
            var processModules = new List <Native.ModuleEntry>();

            // Create a tool help snapshot

            var snapshotHandle = Native.CreateToolhelp32Snapshot(Native.SnapshotFlags.Module | Native.SnapshotFlags.Module32, (uint)processId);

            // Initialize a module entry struct

            var moduleEntrySize = Marshal.SizeOf(typeof(Native.ModuleEntry));

            var moduleEntry = new Native.ModuleEntry {
                Size = (uint)moduleEntrySize
            };

            // Store the module entry struct in a buffer

            var moduleEntryBuffer = StructureToPointer(moduleEntry);

            // Get the first module of the process and store it in the buffer

            if (!Native.Module32First(snapshotHandle, moduleEntryBuffer))
            {
                return(processModules);
            }

            // Get the first module entry structure from the buffer

            moduleEntry = PointerToStructure <Native.ModuleEntry>(moduleEntryBuffer);

            processModules.Add(moduleEntry);

            // Get the rest of the modules in the process

            while (Native.Module32Next(snapshotHandle, moduleEntryBuffer))
            {
                // Get the module entry structure from the buffer

                moduleEntry = PointerToStructure <Native.ModuleEntry>(moduleEntryBuffer);

                processModules.Add(moduleEntry);
            }

            return(processModules);
        }