public static bool LoadModules(IntPtr hProcess, ListModules ModuleType) { //Initialize parameters for EPM uint cbNeeded = 0; Psapi.EnumProcessModulesEx(hProcess, IntPtr.Zero, 0, out cbNeeded, ModuleType); long ArraySize = cbNeeded / IntPtr.Size; IntPtr[] hModules = new IntPtr[ArraySize]; GCHandle GCh = GCHandle.Alloc(hModules, GCHandleType.Pinned); // Don't forget to free this later IntPtr lphModules = GCh.AddrOfPinnedObject(); uint cb = cbNeeded; Psapi.EnumProcessModulesEx(hProcess, lphModules, cb, out cbNeeded, ModuleType); for (int i = 0; i < ArraySize; i++) { MODULE_INFO ModInfo = new MODULE_INFO(); System.Text.StringBuilder lpFileName = new System.Text.StringBuilder(256); System.Text.StringBuilder lpModuleBaseName = new System.Text.StringBuilder(32); Psapi.GetModuleFileNameExW(hProcess, hModules[i], lpFileName, (uint)(lpFileName.Capacity)); Psapi.GetModuleInformation(hProcess, hModules[i], out ModInfo, (uint)(Marshal.SizeOf(ModInfo))); Psapi.GetModuleBaseNameW(hProcess, hModules[i], lpModuleBaseName, (uint)(lpModuleBaseName.Capacity)); DbgHelp.SymLoadModuleEx(hProcess, IntPtr.Zero, lpFileName.ToString(), lpModuleBaseName.ToString(), ModInfo.lpBaseOfDll, (int)ModInfo.SizeOfImage, IntPtr.Zero, 0); } GCh.Free(); return false; }
public static bool LoadModules(IntPtr hProcess, ListModules ModuleType) { //Initialize parameters for EPM uint cbNeeded = 0; Psapi.EnumProcessModulesEx(hProcess, IntPtr.Zero, 0, out cbNeeded, ModuleType); long ArraySize = cbNeeded / IntPtr.Size; IntPtr[] hModules = new IntPtr[ArraySize]; GCHandle GCh = GCHandle.Alloc(hModules, GCHandleType.Pinned); // Don't forget to free this later IntPtr lphModules = GCh.AddrOfPinnedObject(); uint cb = cbNeeded; Psapi.EnumProcessModulesEx(hProcess, lphModules, cb, out cbNeeded, ModuleType); for (int i = 0; i < ArraySize; i++) { MODULE_INFO ModInfo = new MODULE_INFO(); System.Text.StringBuilder lpFileName = new System.Text.StringBuilder(256); System.Text.StringBuilder lpModuleBaseName = new System.Text.StringBuilder(32); Psapi.GetModuleFileNameExW(hProcess, hModules[i], lpFileName, (uint)(lpFileName.Capacity)); Psapi.GetModuleInformation(hProcess, hModules[i], out ModInfo, (uint)(Marshal.SizeOf(ModInfo))); Psapi.GetModuleBaseNameW(hProcess, hModules[i], lpModuleBaseName, (uint)(lpModuleBaseName.Capacity)); DbgHelp.SymLoadModuleEx(hProcess, IntPtr.Zero, lpFileName.ToString(), lpModuleBaseName.ToString(), ModInfo.lpBaseOfDll, (int)ModInfo.SizeOfImage, IntPtr.Zero, 0); } GCh.Free(); return(false); }
private void ExecuteLoadCommand() { //iterate through modules and execute force unload foreach (var moduleVM in ListModules.Where(m => m.Selected)) { AggregateDataProvider.ForceLoad(moduleVM.SerialNumber); } LoadData(ListID); }
private void ExecuteForceUnloadCommand() { //iterate through modules and execute force unload var snsOnTruck = AggregateDataProvider.SerialNumbersOnTruck.ToArray(); foreach (var moduleVM in ListModules.Where(m => m.Selected && snsOnTruck.Any(s => s == m.SerialNumber))) { AggregateDataProvider.ForceUnload(moduleVM.SerialNumber); } LoadData(ListID); }
public static extern bool EnumProcessModulesEx(IntPtr hProcess, IntPtr ModuleArray, int cb, ref int cbNeeded, ListModules FilterFlags);
public void PIK_ListModules() { CommandStart.Start(doc => ListModules.List()); }
private void MVm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { HasCheckedModules = ListModules.Any(x => x.Selected) && !MapVisible; }
public void LoadData(string listId) { try { //LOAD DATA BASED ON LIST ID using (var uow = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork()) { pickupList = uow.PickupListRepository.GetById(listId, "Field.Farm.Client", "AssignedModules"); syncedSettings = uow.SyncedSettingsRepo.GetAll().FirstOrDefault(); if (pickupList.Destination == PickupListDestination.GIN_YARD && pickupList.AssignedModules.Count(m => m.ModuleStatus == ModuleStatus.IN_FIELD) > 0) { var firstModule = pickupList.AssignedModules.First(); FieldLat = firstModule.Latitude; FieldLong = firstModule.Longitude; } else if (pickupList.Destination == PickupListDestination.GIN_FEEDER && pickupList.AssignedModules.Count(m => m.ModuleStatus == ModuleStatus.AT_GIN) > 0) { var firstModule = pickupList.AssignedModules.First(); FieldLat = firstModule.Latitude; FieldLong = firstModule.Longitude; } if (FieldLat == 0.000 || FieldLong == 0.000) { FieldLat = pickupList.Latitude; FieldLong = pickupList.Longitude; } ListName = pickupList.Name; Client = pickupList.ClientName; Farm = pickupList.FarmName; Field = pickupList.Field.Name; if (syncedSettings == null) { pickupList.ModulesPerLoad = 4; } else { pickupList.ModulesPerLoad = syncedSettings.ModulesPerLoad; } ModulesInField = pickupList.ModulesRemaining; ModulesLoaded = pickupList.AssignedModules.Count(c => AggregateDataProvider.SerialNumbersOnTruck.Contains(c.Name)); //adjust modules in field by modules on truck if (pickupList.Destination == PickupListDestination.GIN_YARD) { foreach (var module in pickupList.AssignedModules.Where(s => s.ModuleStatus == ModuleStatus.IN_FIELD && AggregateDataProvider.SerialNumbersOnTruck.Any(x => x == s.Name))) { ModulesInField--; } } else if (pickupList.Destination == PickupListDestination.GIN_FEEDER) { foreach (var module in pickupList.AssignedModules.Where(s => s.ModuleStatus == ModuleStatus.AT_GIN && AggregateDataProvider.SerialNumbersOnTruck.Any(x => x == s.Name))) { ModulesInField--; } } ModulesOnList = pickupList.TotalModules; LoadsCompleted = pickupList.LoadsCompleted; LoadsRemaining = pickupList.LoadsRemaining; DestinationName = pickupList.DestinationName; setLoadsRemaining(); HasCheckedModules = false; System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { foreach (var m in ListModules) { m.PropertyChanged -= MVm_PropertyChanged; } Modules.Clear(); ListModules.Clear(); foreach (var m in pickupList.AssignedModules.OrderBy(s => s.Name)) { //using not thread safe version of serial numbers on truck because this event gets fired //inside a lock on the _aggregateEvents in the provider //using the threadsafe property will cause a thread lock on the same property var mVm = new ModuleViewModel { Loaded = AggregateDataProvider.SerialNumbersOnTruckNotThreadSafe.Contains(m.Name), Latitude = m.Latitude, Longitude = m.Longitude, SerialNumber = m.Name }; mVm.ShowOnMap = (m.ModuleStatus == ModuleStatus.IN_FIELD && pickupList.Destination == PickupListDestination.GIN_YARD) || (m.ModuleStatus == ModuleStatus.AT_GIN && pickupList.Destination == PickupListDestination.GIN_FEEDER); Modules.Add(mVm); if (mVm.ShowOnMap || mVm.Loaded) { mVm.PropertyChanged += MVm_PropertyChanged; ListModules.Add(mVm); } } })); } } catch (Exception exc) { Logging.Logger.Log(exc); } }
public static extern bool EnumProcessModulesEx(IntPtr hProcess, IntPtr[] lphModule, int cb, out uint lpcbNeeded, [MarshalAs(UnmanagedType.I4)] ListModules dwFilterFlag);
public static extern bool EnumProcessModulesEx(IntPtr hProcess, [Out] IntPtr lphModuleArray, uint cb, out uint cbNeeded, ListModules FilterFlags);
public unsafe static IntPtr GetProcessBaseAddress(int pid, ListModules moduleTypes = ListModules.LIST_MODULES_ALL) { IntPtr processHandle = OpenProcess(ProcessAccessFlags.QueryLimitedInformation | ProcessAccessFlags.VirtualMemoryRead, false, pid); try { // Query process image name. char[] imageFileName = new char[2048]; int imageFileNameSize = imageFileName.Length; if (QueryFullProcessImageNameW(processHandle, 0, imageFileName, ref imageFileNameSize)) { // Query process handle's modules. IntPtr[] hModules = new IntPtr[1024]; int cb = 1024 * sizeof(long); int lpcbNeeded = 0; bool enumProcessModulesExReturn = false; fixed(IntPtr *lphModule = hModules) { enumProcessModulesExReturn = EnumProcessModulesEx(processHandle, lphModule, cb, out lpcbNeeded, moduleTypes); // If we failed, attempt to repeat the run a few times. if (!enumProcessModulesExReturn) { // https://referencesource.microsoft.com/#system/services/monitoring/system/diagnosticts/ProcessManager.cs,639 // Per Microsoft's Reference Source page: // "Also, EnumProcessModules is not a reliable method to get the modules for a process. // If OS loader is touching module information, this method might fail and copy part of the data. // This is no easy solution to this problem. The only reliable way to fix this is to // suspend all the threads in target process. Of course we don't want to do this in Process class. // So we just to try avoid the ---- by calling the same method 50 (an arbitary number) times." bool sourceProcessIsWow64 = GetProcessWoW64(GetCurrentProcessId()); bool targetProcessIsWow64 = GetProcessWoW64(pid); if (sourceProcessIsWow64 && !targetProcessIsWow64) { throw new Win32Exception((int)Win32Error.ERROR_PARTIAL_COPY, "299 (ERROR_PARTIAL_COPY) - One process is WOW64 and the other is not."); } for (int i = 0; i < 50; ++i) { enumProcessModulesExReturn = EnumProcessModulesEx(processHandle, lphModule, cb, out lpcbNeeded, moduleTypes); if (enumProcessModulesExReturn) { break; // If we succeeded, break out of the loop early. } // Sleep for 1ms then try again. Thread.Sleep(1); } } } // If we successfully retrieved an array of process modules, enumerate through them to find the main module. if (enumProcessModulesExReturn) { int moduleCount = lpcbNeeded / sizeof(long); for (int i = 0; i < moduleCount; i++) { char[] moduleFileName = new char[2048]; int moduleFileNameSize = GetModuleFileNameEx(processHandle, hModules[i], moduleFileName, moduleFileName.Length); // Compare the module name with the name of the process image. if (imageFileNameSize == moduleFileNameSize && string.Equals(new string(imageFileName, 0, imageFileNameSize), new string(moduleFileName, 0, moduleFileNameSize), StringComparison.InvariantCultureIgnoreCase)) { // We found the main module, return it's base address. return(hModules[i]); } } } else { throw new Win32Exception(); } } // If we reach this point, we didn't find the main module... return(IntPtr.Zero); } finally { CloseHandle(processHandle); } }
public static unsafe extern bool EnumProcessModulesEx(IntPtr hProcess, [Out] IntPtr *lphModule, int cb, out int lpcbNeeded, ListModules dwFilterFlag = ListModules.LIST_MODULES_ALL);