Click here to Skip to main content
15,843,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i was trying to fix one method of anti-dumping in memory the method is to delete the header. And the code is:
public enum FreeConsts : uint
        {
            MEM_RELEASE = 0x8000
        }
    	[DllImport("kernel32.dll")]
		private static extern IntPtr ZeroMemory(IntPtr addr, IntPtr size);
		
		[DllImport("kernel32.dll")]
		private static extern IntPtr VirtualProtect(IntPtr lpAddress, IntPtr dwSize, IntPtr flNewProtect, ref IntPtr lpflOldProtect);
		
		[DllImport("kernel32")]
        private static extern bool VirtualFree(IntPtr lpAddress, UInt32 dwSize, FreeConsts dwFreeType);
		
		[DllImport("kernel32.dll")]
		public static extern IntPtr GetModuleHandle(string lpModuleName);
		
		public static void EraseHeader()
		{
		IntPtr address = GetModuleHandle(null);
		IntPtr dwOld = default(IntPtr);
		VirtualProtect(address, (IntPtr)4096, (IntPtr)0x40, ref dwOld);
		ZeroMemory(address, (IntPtr)4096);
		VirtualFree(address, 0, FreeConsts.MEM_RELEASE);
		}


However many times show me:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has
been thrown by the target of an invocation. ---> System.TypeInitializationExcept
ion: The type initializer for 'T???????????????????' threw an exception. ---> Sy
stem.TypeInitializationException: The type initializer for 'System.Management.Ma
nagementPath' threw an exception. ---> System.OutOfMemoryException: Exception of
 type 'System.OutOfMemoryException' was thrown.
   at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark
& stackMark)
   at System.Threading.Thread.Start(StackCrawlMark& stackMark)
   at System.Threading.Thread.Start()
   at System.Management.MTAHelper.InitWorkerThread()
   at System.Management.MTAHelper.CreateInMTA(Type type)
   at System.Management.ManagementPath.CreateWbemPath(String path)
   at System.Management.ManagementPath..cctor()
   --- End of inner exception stack trace ---
   at System.Management.ManagementScope._Clone(ManagementScope scope, Identifier
ChangedEventHandler handler)
   at System.Management.ManagementObjectSearcher..ctor(ManagementScope scope, Ob
jectQuery query, EnumerationOptions options)
   at System.Management.ManagementObjectSearcher..ctor(String queryString)
   at T???????????????????..cctor()
   --- End of inner exception stack trace ---
   at T???????????????????.get_Is64Bit()
   at ????????????????????.????????????????????()
   at ????????????????????.????????????????????()
   at ????????????????????.Main(String[] args)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
 Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
t[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at skip.Program.RunInternal(Byte[] encode, String pass)
   at skip.Program.Main()


The problem is when i load my assembly a lot of times crash if it is a small console works but if i am using a program or something crash in .net.

What I have tried:

I tried with small console program and works but when i use dll's or assemblies crash. showming me that
Posted
Updated 28-May-17 12:02pm
v3
Comments
Peter_in_2780 28-May-17 23:21pm    
Messing with system memory allocation like that can only ever end in tears.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900