public static ModuleInfo[] GetModuleInfos(int processId)
 {
     IntPtr ptr = (IntPtr) (-1);
     GCHandle handle = new GCHandle();
     ArrayList list = new ArrayList();
     try
     {
         ptr = Microsoft.Win32.NativeMethods.CreateToolhelp32Snapshot(8, processId);
         if (ptr == ((IntPtr) (-1)))
         {
             throw new Win32Exception();
         }
         int num = Marshal.SizeOf(typeof(Microsoft.Win32.NativeMethods.WinModuleEntry));
         int val = (num + 260) + 0x100;
         int[] numArray = new int[val / 4];
         handle = GCHandle.Alloc(numArray, GCHandleType.Pinned);
         IntPtr ptr2 = handle.AddrOfPinnedObject();
         Marshal.WriteInt32(ptr2, val);
         HandleRef ref2 = new HandleRef(null, ptr);
         if (Microsoft.Win32.NativeMethods.Module32First(ref2, ptr2))
         {
             do
             {
                 Microsoft.Win32.NativeMethods.WinModuleEntry structure = new Microsoft.Win32.NativeMethods.WinModuleEntry();
                 Marshal.PtrToStructure(ptr2, structure);
                 ModuleInfo info = new ModuleInfo {
                     baseName = Marshal.PtrToStringAnsi((IntPtr) (((long) ptr2) + num)),
                     fileName = Marshal.PtrToStringAnsi((IntPtr) ((((long) ptr2) + num) + 0x100L)),
                     baseOfDll = structure.modBaseAddr,
                     sizeOfImage = structure.modBaseSize,
                     Id = structure.th32ModuleID
                 };
                 list.Add(info);
                 Marshal.WriteInt32(ptr2, val);
             }
             while (Microsoft.Win32.NativeMethods.Module32Next(ref2, ptr2));
         }
     }
     finally
     {
         if (handle.IsAllocated)
         {
             handle.Free();
         }
         if (ptr != ((IntPtr) (-1)))
         {
             Microsoft.Win32.SafeNativeMethods.CloseHandle(new HandleRef(null, ptr));
         }
     }
     ModuleInfo[] array = new ModuleInfo[list.Count];
     list.CopyTo(array, 0);
     return array;
 }
        public static ModuleInfo[] GetModuleInfos(int processId)
        {
            IntPtr    ptr    = (IntPtr)(-1);
            GCHandle  handle = new GCHandle();
            ArrayList list   = new ArrayList();

            try
            {
                ptr = Microsoft.Win32.NativeMethods.CreateToolhelp32Snapshot(8, processId);
                if (ptr == ((IntPtr)(-1)))
                {
                    throw new Win32Exception();
                }
                int   num      = Marshal.SizeOf(typeof(Microsoft.Win32.NativeMethods.WinModuleEntry));
                int   val      = (num + 260) + 0x100;
                int[] numArray = new int[val / 4];
                handle = GCHandle.Alloc(numArray, GCHandleType.Pinned);
                IntPtr ptr2 = handle.AddrOfPinnedObject();
                Marshal.WriteInt32(ptr2, val);
                HandleRef ref2 = new HandleRef(null, ptr);
                if (Microsoft.Win32.NativeMethods.Module32First(ref2, ptr2))
                {
                    do
                    {
                        Microsoft.Win32.NativeMethods.WinModuleEntry structure = new Microsoft.Win32.NativeMethods.WinModuleEntry();
                        Marshal.PtrToStructure(ptr2, structure);
                        ModuleInfo info = new ModuleInfo {
                            baseName    = Marshal.PtrToStringAnsi((IntPtr)(((long)ptr2) + num)),
                            fileName    = Marshal.PtrToStringAnsi((IntPtr)((((long)ptr2) + num) + 0x100L)),
                            baseOfDll   = structure.modBaseAddr,
                            sizeOfImage = structure.modBaseSize,
                            Id          = structure.th32ModuleID
                        };
                        list.Add(info);
                        Marshal.WriteInt32(ptr2, val);
                    }while (Microsoft.Win32.NativeMethods.Module32Next(ref2, ptr2));
                }
            }
            finally
            {
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
                if (ptr != ((IntPtr)(-1)))
                {
                    Microsoft.Win32.SafeNativeMethods.CloseHandle(new HandleRef(null, ptr));
                }
            }
            ModuleInfo[] array = new ModuleInfo[list.Count];
            list.CopyTo(array, 0);
            return(array);
        }