Пример #1
0
		private void ResumeProcess()
		{
			if(!bSuspended)
				return;
			if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
			{
				profilerControlStausBar.Text =  "Profiling Already Started";
				return ;
			}
			try
			{	
				if(m_functionFlag!=0)
				{
					if(HandleMethodCall(true,BFC,"BFC",m_functionFlag,"Profiling Started","Unable to start!"))
						bIsCapturingStartedForOnce=true;
					else
						bIsCapturingStartedForOnce=false;
				}

				if(!bDefer)
				{
					IntPtr hProcess=System.Diagnostics.Process.GetProcessById(pid).Handle ;  
					if(hProcess==IntPtr.Zero)
						throw new Exception("Invalid Process Handle"); 

					int dwThreadId;	
					int pidParam=pid;
					string strParam=pidParam.ToString();
					ASCIIEncoding encCur = new ASCIIEncoding();
					byte[] encodedPassword = encCur.GetBytes(strParam);									
					for(int x=0;x<encodedPassword.Length;x++)
					{
						pidParam^=Convert.ToInt32(encodedPassword[x]); 
					}

					IntPtr hThread=CreateRemoteThread(hProcess,IntPtr.Zero,0,RCLR,(IntPtr)pidParam,0,out dwThreadId);					
					if( hThread==IntPtr.Zero)
						throw new Exception("Invalid Thread Handle"); 		
					int dwStatus;
					int success;
					while(true)
					{
						success=GetExitCodeThread(hThread,out dwStatus);
						if(success==0 || dwStatus !=0x00000103L)//0x00000103L :-  status_running
							break;
						System.Threading.Thread.Sleep(100);
					} 
					try{CloseHandle(hThread);}
					catch{}	
					try{CloseHandle(hProcess);}
					catch{}

					if(dwStatus!=0)
						Marshal.ThrowExceptionForHR(dwStatus);
					else
					{						
						profilerControlStausBar.Text =  "Profiling Already Started.Process Running Now.";
						Application.DoEvents(); 						

						try{this.Controls.Remove(Init);   }
						catch{} 
						
					}
					return;
				}
				else
				{
					try
					{
						if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
							throw new Exception("Invalid Configuration");  

						if(delegatorObj == null ||  !System.Runtime.InteropServices.Marshal.IsComObject(delegatorObj) )
							delegatorObj = new SharpDelegatorClass();
						try
						{
							delegatorObj.profilerName=Application.StartupPath+@"\SoftProdigy.Core.dll";
						}
						catch(COMException com_Error)
						{
							if(com_Error.ErrorCode==-2147023649)	//Already initialized
							{
								//Do nothing
							}
							else throw com_Error;//new COMException("Resumption failed with error :- "+com_Error.ErrorCode.ToString()  ,com_Error.ErrorCode);
						}

						int pidParam=pid;
						string strParam=pidParam.ToString();
						ASCIIEncoding encCur = new ASCIIEncoding();
						byte[] encodedPassword = encCur.GetBytes(strParam);									
						for(int x=0;x<encodedPassword.Length;x++)
						{
							pidParam^=Convert.ToInt32(encodedPassword[x]); 
						}

						delegatorObj.DeferMethodCall(pid,"RCLR", pidParam);						
						profilerControlStausBar.Text =  "Profiling Already Started.Process Running Now.";
						Application.DoEvents(); 
						
						//bSuspended=false;//This is another option						
						try{this.Controls.Remove(Init);   }
						catch{}
  
					}
					catch(COMException com_Error)
					{
						throw com_Error;
					}
					catch(Exception except)
					{				
						throw except;
					}

				}
				
			}
			catch(COMException com_Error)
			{
				MessageBox.Show(" Resumption Failed.\n"+com_Error.Message,"Exception!",MessageBoxButtons.OK ,MessageBoxIcon.Error) ;
				profilerControlStausBar.Text =  "Resumption failed :- "+com_Error.ErrorCode.ToString()  ;				
				foreach (Control control in this.Controls )
				{
					control.Enabled=false; 
				}
				Application.DoEvents();

				BFC=System.UIntPtr.Zero  ;
				CFC=System.UIntPtr.Zero;
				COC=System.UIntPtr.Zero;
				CO=System.UIntPtr.Zero;
				DFD=System.UIntPtr.Zero;
				DOD=System.UIntPtr.Zero;
				SFC=System.UIntPtr.Zero;				
				RCLR=System.UIntPtr.Zero; 				
			}
			catch(Exception except)
			{			
				MessageBox.Show(" Resumption Failed.\n"+except.Message,"Exception!",MessageBoxButtons.OK ,MessageBoxIcon.Error) ;
				try
				{
					profilerControlStausBar.Text =  " Resumption Failed :- "+Marshal.GetHRForException(except).ToString()  ;				
				}
				catch{}
				foreach (Control control in this.Controls )
				{
					control.Enabled=false; 
				}
				Application.DoEvents();

				BFC=System.UIntPtr.Zero  ;
				CFC=System.UIntPtr.Zero;
				COC=System.UIntPtr.Zero;
				CO=System.UIntPtr.Zero;
				DFD=System.UIntPtr.Zero;
				DOD=System.UIntPtr.Zero;
				SFC=System.UIntPtr.Zero;				
				RCLR=System.UIntPtr.Zero;
			}	
		}
Пример #2
0
		private void InitializeProfiler()
		{
			bDefer=true;
			try
			{
				System.Diagnostics.Process process=System.Diagnostics.Process.GetProcessById(pid)   ;
				IntPtr hProcess=process.Handle ;
				if(hProcess==IntPtr.Zero)
				{
					throw new Exception("Invalid Process Handle"); 
				}
				else
				{
					int dwThreadId;	
					if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
					{
						profilerControlStausBar.Text =  "Profiler Initialization Successful";
						return ;
					}		
					int pidParam=pid;
					string strParam=pidParam.ToString();
					ASCIIEncoding encCur = new ASCIIEncoding();
					byte[] encodedPassword = encCur.GetBytes(strParam);//					

					for(int x=0;x<encodedPassword.Length;x++)
					{
						pidParam^=Convert.ToInt32(encodedPassword[x]); 
					}

					IntPtr hThread=CreateRemoteThread(hProcess,IntPtr.Zero,0,IP,(IntPtr)pidParam,4,out dwThreadId);					//Suspended					
					if(System.Runtime.InteropServices.Marshal.GetLastWin32Error()==5)
					{
						bDefer=true;
						throw new System.Security.SecurityException("Access Denied");  
					}
			
					if( hThread==IntPtr.Zero)
					{
						throw new Exception("Invalid Thread Handle"); 
					}

					int result=ResumeThread(hThread);
					if(result==-1)
					{
						throw new Exception("Initialization failed with thread abortion"); 
					}
					if(result==1 || result==0)
					{
						//ok
					}
					else
					{
						throw new Exception("Initialization failed with thread count error"); 
					}

					int dwStatus;
					int success;
					while(true)
					{
						success=GetExitCodeThread(hThread,out dwStatus);
						if(success==0 || dwStatus !=0x00000103L)//0x00000103L :-  status_running
							break;
						System.Threading.Thread.Sleep(100);
					} 
					try{CloseHandle(hThread);}catch{}	
					try{CloseHandle(hProcess);}catch{}		

					if(dwStatus==0)
					{
						//MessageBox.Show("Profiler Initialization Successful"); 
						bDefer=false;
						profilerControlStausBar.Text =  "Profiler Initialization Successful";
						Application.DoEvents(); 
						if(Configurator.IsProcessOA(pid))
						{
							HandleMethodCall(false,CO,"CO" ,m_objectFlag," Profiler Initialization Successful","Initialization failed");
						} 						
						return;
					}
					else if(dwStatus==-2147023649)	//Already initialized
					{
						//MessageBox.Show("Profiler has already been initialized for this process."); 
						profilerControlStausBar.Text =  "Profiler already initialized.";
						Application.DoEvents(); 
						bDefer=false;
						return;
					}
					else throw new COMException("Initialization failed with error :- "+dwStatus.ToString() ,dwStatus);
				}				
			}
			catch{}	
				bDefer=true;
			try
			{
				if(!System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)     
				{				
					if(delegatorObj == null || !System.Runtime.InteropServices.Marshal.IsComObject(delegatorObj) )					
					{
						try
						{
							delegatorObj = new SharpDelegatorClass();					
							delegatorObj.profilerName=Application.StartupPath+@"\SoftProdigy.Core.dll";
						}
						catch(COMException com_Error)
						{
							if(com_Error.ErrorCode==-2147023649)	//Already initialized
							{
								profilerControlStausBar.Text =  "Profiler already initialized.";
								Application.DoEvents(); 						
								return;
							}
								//else throw new COMException("Initialization failed with error :- "+com_Error.ErrorCode.ToString()  ,com_Error.ErrorCode);
							else
							{
									throw com_Error;
							}
						}
					}

					int pidParam=pid;
					string strParam=pidParam.ToString();
					ASCIIEncoding encCur = new ASCIIEncoding();
					byte[] encodedPassword = encCur.GetBytes(strParam);//					

					for(int x=0;x<encodedPassword.Length;x++)
					{
						pidParam^=Convert.ToInt32(encodedPassword[x]); 
					}

					delegatorObj.DeferMethodCall(pid,"IP",pidParam);	
					profilerControlStausBar.Text =  "Profiler Initialization Successful";
					Application.DoEvents(); 
					if(Configurator.IsProcessOA(pid))
					{
						HandleMethodCall(false,CO,"CO" ,m_objectFlag," Profiler Initialization Successful","Initialization failed");
					}  				
				}
			}
			catch(COMException com_Error)
			{
				MessageBox.Show(" Profiler Initialization Failed.\n"+com_Error.Message,"Exception!",MessageBoxButtons.OK ,MessageBoxIcon.Error) ;
				try
				{
					profilerControlStausBar.Text =  "Initialization failed :- "+com_Error.ErrorCode.ToString()  ;				
				}
				catch{}
				foreach (Control control in this.Controls )
				{
					control.Enabled=false; 
				}
				Application.DoEvents();

				BFC=System.UIntPtr.Zero  ;
				CFC=System.UIntPtr.Zero;
				COC=System.UIntPtr.Zero;
				CO=System.UIntPtr.Zero;
				DFD=System.UIntPtr.Zero;
				DOD=System.UIntPtr.Zero;
				SFC=System.UIntPtr.Zero;				
				RCLR=System.UIntPtr.Zero; 
				throw com_Error;
			}
			catch(Exception except)
			{				
				try
				{
					profilerControlStausBar.Text =  " Profiler Initialization failed :- "+Marshal.GetHRForException(except).ToString()  ;				
				}
				catch{}
				foreach (Control control in this.Controls )
				{
					control.Enabled=false; 
				}
				Application.DoEvents();

				BFC=System.UIntPtr.Zero  ;
				CFC=System.UIntPtr.Zero;
				COC=System.UIntPtr.Zero;
				CO=System.UIntPtr.Zero;
				DFD=System.UIntPtr.Zero;
				DOD=System.UIntPtr.Zero;
				SFC=System.UIntPtr.Zero;				
				RCLR=System.UIntPtr.Zero;
				throw except;

			}			
		
		}		
Пример #3
0
				if(SharpClient.SharpClientForm.scInstance.psStatusBar.Panels[0].Text=="Initializing.Please wait.." )
				{
					SharpClient.SharpClientForm.scInstance.psStatusBar.Panels[0].Text="Ready to profile";				
				}
			}
		}


		public static void InstallProfilingEnvironment()
		{
			 pvtInstallProfilingEnvironment();
		}


		private static void pvtUninstallProfilingEnvironment(bool bShowMsg)
		{
			ProfileSharpServerLib.SharpDelegatorClass delegator=null; 
			object shellObject=null;
			object envProp=null;	
			try
			{				
				try
				{
					Configurator.RestoreDefaultConfiguration();  //V. Imp.
				}
				catch{}				
				SharpClient.SharpClientForm.scInstance.psStatusBar.Panels[0].Text ="Please wait.Shutting down profiling environment...";
				SharpClient.SharpClientForm.scInstance.Cursor =Cursors.WaitCursor;
				Application.DoEvents();  
				Configurator.ToggleProfiling(0);  
				if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
				{
					throw new COMException(null,-2147023838); 
				}	
				try
				{
					shellObject=Activator.CreateInstance(Type.GetTypeFromProgID("WScript.Shell",true));  
					envProp=shellObject.GetType().InvokeMember("Environment",System.Reflection.BindingFlags.GetProperty,null,shellObject,null);                    					
					envProp.GetType().InvokeMember("Remove",System.Reflection.BindingFlags.InvokeMethod,null,envProp,new object[]{"COR_ENABLE_PROFILING"});     
					envProp.GetType().InvokeMember("Remove",System.Reflection.BindingFlags.InvokeMethod,null,envProp,new object[]{"COR_PROFILER"});     					
				}
				catch{}
				delegator=new ProfileSharpServerLib.SharpDelegatorClass();
				delegator.InstallServiceEnvironment(Application.StartupPath+"\\EnvSetter.dll",null);  

			}		
			catch(Exception ex)
			{	
				SharpClient.SharpClientForm.scInstance.Cursor=Cursors.Arrow ; 	
				if(bShowMsg)
				{
					if(Marshal.GetHRForException(ex)==-2147023838 || Marshal.GetHRForException(ex)==-2147221164 || Marshal.GetHRForException(ex)==-2147024893 )//Service not available
					{
						SharpClient.SharpClientForm.scInstance.psStatusBar.Panels[0].Text="Interactive Profiling Only";
					}
					else
					{
						MessageBox.Show("The profiling environment could not be restored.\nSome .NET applications may not respond.Refer documentation for recovery!.","Critical Error!",MessageBoxButtons.OK ,MessageBoxIcon.Error);  
					}
				}
				Application.DoEvents(); 
			}
			finally
			{
				SharpClient.SharpClientForm.scInstance.Cursor=Cursors.Arrow ; 
				
				if(delegator!=null)
				{
					System.Runtime.InteropServices.Marshal.ReleaseComObject(delegator);
					delegator=null;
				}
Пример #4
0
		private bool HandleMethodCall(bool isFunctionCall,UIntPtr methodPtr,string methodName,int param,string successMessage,string failMessage)
		{

			if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
			{
				profilerControlStausBar.Text =  successMessage;
				return true;
			}
			try
			{
				if(isFunctionCall)
				{
					if(m_functionFlag!=0 && (configString=="PERFORMANCE_ANALYSIS"  || configString=="BOTH"))
					{
						goto A;
					}
					else
					{
						MessageBox.Show("This action is not valid for this process.", "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);										
						return false;
					}
				}
				else
				{
					if(m_objectFlag !=0 && (configString=="MEMORY_ANALYSIS"  || configString=="BOTH"))
					{
						goto A;
					}
					else
					{
						MessageBox.Show("This action is not valid for this process.", "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);										
						return false;
					}

				}
				
			A:

				if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
				{
					profilerControlStausBar.Text =  successMessage;
					return true;
				}

				if(!bDefer)									
				{				
					//if(System.DateTime.Now.Second%2==0)					
					if(true)					
					{
						IntPtr hProcess=System.Diagnostics.Process.GetProcessById(pid).Handle;   
						if(hProcess==IntPtr.Zero)
							throw new Exception("Invalid Process Handle"); 

						int dwThreadId=0;
						IntPtr hThread=IntPtr.Zero;
						if(param==0)
						{
							if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
							{
								hThread=IntPtr.Zero ;
							}
							else
							{
								hThread=CreateRemoteThread(hProcess,IntPtr.Zero,0,methodPtr,IntPtr.Zero,0,out dwThreadId);			
							}
						}
						else
						{
							if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
							{
								hThread=(IntPtr)200 ;
							}
							else
							{
								hThread=CreateRemoteThread(hProcess,IntPtr.Zero,0,methodPtr,(IntPtr)param,0,out dwThreadId);			
							}
						}

						if( hThread==IntPtr.Zero)						
							throw new Exception("Invalid Thread Handle:-"+ System.Runtime.InteropServices.Marshal.GetHRForLastWin32Error().ToString()); 				

						int dwStatus;
						int success;
						while(true)
						{
							success=GetExitCodeThread(hThread,out dwStatus);
							if(success==0 || dwStatus !=0x00000103L)//0x00000103L :-  status_running
								break;
							System.Threading.Thread.Sleep(100);
						} 
						try{CloseHandle(hThread);}
						catch{}	
						try{CloseHandle(hProcess);}
						catch{}

						if(dwStatus!=0)
						{
							Marshal.ThrowExceptionForHR  (dwStatus);
						}
						else
						{	
							profilerControlStausBar.Text =  successMessage;
							Application.DoEvents(); 
					  
						}
					}					
					return true;
				}
				else
				{
					try
					{
						if(System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
						{
							return true;
						}
						if(delegatorObj == null || !System.Runtime.InteropServices.Marshal.IsComObject(delegatorObj) )
							delegatorObj = new SharpDelegatorClass();
						try
						{
							delegatorObj.profilerName=Application.StartupPath+@"\SoftProdigy.Core.dll";
						}
						catch(COMException com_Error)
						{
							if(com_Error.ErrorCode==-2147023649)	//Already initialized
							{
								//Do nothing
							}
							else throw com_Error;
						}

						delegatorObj.DeferMethodCall(pid,methodName,param);					  				
						profilerControlStausBar.Text = successMessage;
						Application.DoEvents(); 					  
					}
					catch(COMException com_Error)
					{
						throw com_Error;
					}
					catch(Exception exc)
					{
						throw exc;
					}
					return true;					  

				}
			}
			catch(COMException _com_error)
			{
				profilerControlStausBar.Text = failMessage;
				Application.DoEvents(); 
				MessageBox.Show(failMessage+"\n"+_com_error.Message, "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
			catch(Exception ex)
			{
				profilerControlStausBar.Text = failMessage;
				Application.DoEvents(); 
				MessageBox.Show(failMessage+"\n"+ex.Message, "Error!",MessageBoxButtons.OK,MessageBoxIcon.Error);															 
			}

			return false;

		}
Пример #5
0
				keySuspend.SetValue("SuspendOnStart",0);  
				
			}
			catch(Exception ex)
			{
				MessageBox.Show("The profiler was unable to restore CLR settings for the system.\nAll .NET processes may be rendered in suspeneded state.\nRead the documentation to rescue.\n"+ex.Message,"Critical Error!!!!",MessageBoxButtons.OK,MessageBoxIcon.Error);  								
			}
			if(keySuspend!=null)
			{
				try	{keySuspend.Close();}
				catch{}
			}		
		}

		private static void pvtInstallProfilingEnvironment()
		{
			SharpClient.SharpClientForm.scInstance.psStatusBar.Panels[0].Text="Initializing.Please wait..";
			SharpClient.SharpClientForm.scInstance.Cursor =Cursors.WaitCursor ;
			Application.DoEvents(); 			
			ProfileSharpServerLib.SharpDelegatorClass delegator=null; 
			object shellObject=null;
			object envProp=null;
			try
			{
				Configurator.ToggleProfiling(1); 
				if(!System.Security.Principal.WindowsIdentity.GetCurrent().IsSystem)
				{
					shellObject=Activator.CreateInstance(Type.GetTypeFromProgID("WScript.Shell",true));  
					envProp=shellObject.GetType().InvokeMember("Environment",System.Reflection.BindingFlags.GetProperty,null,shellObject,null);                    					
					envProp.GetType().InvokeMember("Item",System.Reflection.BindingFlags.SetProperty,null,envProp,new object[]{"COR_ENABLE_PROFILING","0x1"});     
					envProp.GetType().InvokeMember("Item",System.Reflection.BindingFlags.SetProperty,null,envProp,new object[]{"COR_PROFILER","{383227C2-1C12-4C3B-90E7-EF5BCCBEBD7D}"});     
					delegator=new ProfileSharpServerLib.SharpDelegatorClass();
					delegator.InstallServiceEnvironment(Application.StartupPath+"\\EnvSetter.dll",null );  
				}
			}
			catch(Exception ex)
			{
				if(Marshal.GetHRForException(ex)==-2147023838 || Marshal.GetHRForException(ex)==-2147221164 ||Marshal.GetHRForException(ex)==-2147024893 )
				{
					SharpClient.SharpClientForm.scInstance.psStatusBar.Panels[0].Text="Ready to profile - Desktop applications only.";  
				}
				else
				{
					SharpClient.SharpClientForm.scInstance.psStatusBar.Panels[0].Text="The profiling environment could not be started.";
					Application.DoEvents();
					throw ex;
				}				
				//throw ex;
			}
			finally
			{
				if(delegator!=null)
				{
					System.Runtime.InteropServices.Marshal.ReleaseComObject(delegator);
					delegator =null;
				}
				if(shellObject!=null)
				{
					Marshal.ReleaseComObject(shellObject);