public bool TryMoveNext(Toolhelp32.Snapshot snap, out Toolhelp32.IEntry entry)
    {
        var x = new WinModuleEntry {
            dwSize = Marshal.SizeOf(typeof(WinModuleEntry))
        };
        var b = Module32Next(snap, ref x);

        entry = x;
        return(b);
    }
Ejemplo n.º 2
0
        public static ModuleInfo[] WinGetModuleInfos(uint processId)
        {
            IntPtr    handle       = (IntPtr)(-1);
            GCHandle  bufferHandle = new GCHandle();
            ArrayList moduleInfos  = new ArrayList();

            try {
                handle = CreateToolhelp32Snapshot(8, processId);
                if (handle == (IntPtr)(-1))
                {
                    return(null);
                }
                int   entrySize  = Marshal.SizeOf(typeof(WinModuleEntry));
                int   bufferSize = entrySize + WinModuleEntry.sizeofFileName + WinModuleEntry.sizeofModuleName;
                int[] buffer     = new int[bufferSize / 4];
                bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                IntPtr bufferPtr = bufferHandle.AddrOfPinnedObject();
                Marshal.WriteInt32(bufferPtr, bufferSize);

                HandleRef handleRef = new HandleRef(null, handle);

                if (Module32First(handleRef, bufferPtr))
                {
                    do
                    {
                        WinModuleEntry module = new WinModuleEntry();
                        Marshal.PtrToStructure(bufferPtr, module);
                        ModuleInfo moduleInfo = new ModuleInfo();
                        moduleInfo.baseName    = Marshal.PtrToStringAnsi((IntPtr)((long)bufferPtr + entrySize));
                        moduleInfo.fileName    = Marshal.PtrToStringAnsi((IntPtr)((long)bufferPtr + entrySize + WinModuleEntry.sizeofModuleName));
                        moduleInfo.baseOfDll   = module.modBaseAddr;
                        moduleInfo.sizeOfImage = module.modBaseSize;
                        moduleInfo.Id          = module.th32ModuleID;
                        moduleInfos.Add(moduleInfo);
                        Marshal.WriteInt32(bufferPtr, bufferSize);
                    }while (Module32Next(handleRef, bufferPtr));
                }
            }
            finally {
                if (bufferHandle.IsAllocated)
                {
                    bufferHandle.Free();
                }
                if (handle != (IntPtr)(-1))
                {
                    CloseHandle(new HandleRef(null, handle));
                }
            }

            ModuleInfo[] temp = new ModuleInfo[moduleInfos.Count];
            moduleInfos.CopyTo(temp, 0);
            return(temp);
        }
 public static extern bool Module32Next(Toolhelp32.Snapshot snap, ref WinModuleEntry entry);