@@ -59,10 +59,8 @@ public class Section
5959 }
6060
6161 private readonly List < Module > modules = new List < Module > ( ) ;
62- public IEnumerable < Module > Modules => modules ;
6362
6463 private readonly List < Section > sections = new List < Section > ( ) ;
65- public IEnumerable < Section > Sections => sections ;
6664
6765 private readonly Symbols symbols = new Symbols ( ) ;
6866 public Symbols Symbols => symbols ;
@@ -337,7 +335,7 @@ private string ReadRemoteRuntimeTypeInformation64(IntPtr address)
337335 return null ;
338336 }
339337
340- #endregion
338+ #endregion
341339
342340 #region WriteMemory
343341
@@ -377,17 +375,33 @@ public bool WriteRemoteMemory<T>(IntPtr address, T value) where T : struct
377375
378376 public Section GetSectionToPointer ( IntPtr address )
379377 {
380- return sections
381- . Where ( s => s . Category != SectionCategory . Unknown )
382- . Where ( s => address . InRange ( s . Start , s . End ) )
383- . FirstOrDefault ( ) ;
378+ lock ( sections )
379+ {
380+ return sections
381+ . Where ( s => s . Category != SectionCategory . Unknown )
382+ . Where ( s => address . InRange ( s . Start , s . End ) )
383+ . FirstOrDefault ( ) ;
384+ }
384385 }
385386
386387 public Module GetModuleToPointer ( IntPtr address )
387388 {
388- return modules
389- . Where ( m => address . InRange ( m . Start , m . End ) )
390- . FirstOrDefault ( ) ;
389+ lock ( modules )
390+ {
391+ return modules
392+ . Where ( m => address . InRange ( m . Start , m . End ) )
393+ . FirstOrDefault ( ) ;
394+ }
395+ }
396+
397+ public Module GetModuleByName ( string name )
398+ {
399+ lock ( modules )
400+ {
401+ return modules
402+ . Where ( m => m . Name . Equals ( name , StringComparison . InvariantCultureIgnoreCase ) )
403+ . FirstOrDefault ( ) ;
404+ }
391405 }
392406
393407 /// <summary>Tries to map the given address to a section or a module of the process.</summary>
@@ -420,16 +434,22 @@ public Task UpdateProcessInformationsAsync()
420434 {
421435 if ( ! IsValid )
422436 {
423- modules . Clear ( ) ;
424- sections . Clear ( ) ;
437+ lock ( modules )
438+ {
439+ modules . Clear ( ) ;
440+ }
441+ lock ( sections )
442+ {
443+ sections . Clear ( ) ;
444+ }
425445
426446 return Task . CompletedTask ;
427447 }
428448
429449 return Task . Run ( ( ) =>
430450 {
431- var modules = new List < Module > ( ) ;
432- var sections = new List < Section > ( ) ;
451+ var newModules = new List < Module > ( ) ;
452+ var newSections = new List < Section > ( ) ;
433453
434454 nativeHelper . EnumerateRemoteSectionsAndModules (
435455 process . Handle ,
@@ -459,11 +479,11 @@ public Task UpdateProcessInformationsAsync()
459479 section . Category = SectionCategory . Data ;
460480 break ;
461481 }
462- sections . Add ( section ) ;
482+ newSections . Add ( section ) ;
463483 } ,
464484 delegate ( IntPtr baseAddress , IntPtr regionSize , string modulePath )
465485 {
466- modules . Add ( new Module
486+ newModules . Add ( new Module
467487 {
468488 Start = baseAddress ,
469489 End = baseAddress . Add ( regionSize ) ,
@@ -473,10 +493,16 @@ public Task UpdateProcessInformationsAsync()
473493 }
474494 ) ;
475495
476- this . modules . Clear ( ) ;
477- this . modules . AddRange ( modules ) ;
478- this . sections . Clear ( ) ;
479- this . sections . AddRange ( sections ) ;
496+ lock ( modules )
497+ {
498+ modules . Clear ( ) ;
499+ modules . AddRange ( newModules ) ;
500+ }
501+ lock ( sections )
502+ {
503+ sections . Clear ( ) ;
504+ sections . AddRange ( newSections ) ;
505+ }
480506 } ) ;
481507 }
482508
0 commit comments