Ironruby trainer series: Write A Process Manager

Source: Internet
Author: User
Tags sharpdevelop

It is very convenient to use ironruby + sharpdevelop to develop GUI programs. The ironruby hands-on series program is to use Ruby to write a series of gadgets to familiarize themselves with ironruby.
Ironruby installed on my machine isIronruby 1.0 for. NET 2.0 SP1

Ironruby: http://ironruby.codeplex.com/releases

 

Download the sample code in this article
: Click to download this file
. After decompression, run. dat.

My homepage
: Www.w-yong.com

 

This article describes how to write a Process Manager, including Ruby, winform, and Win32 API. This article focuses on Ruby rather than process list.
However, the following methods can be used to obtain process information in the system:
1. Get it through the tool help API
2. Get it through psapi
3. Get it through Native API
4. Get through the eprocess traversal through the driver
5. For. net, you can use the process class in the system. Diagnostics namespace to obtain
6 ....
 
We use method 1 and tool help API to obtain process information. The program can list the processes running in the system and select a process. The program can display the modules loaded by the process and terminate the selected process. The program running result is as follows:

 

 

The main function of this program is actually causedProcessmanager
This class provides. For more information about the code implementation, see the process_manager.rb file. Processmanager usedCstruct
This class. Cstruct uses Ruby to simulate the structure of C language, similar to bitstruct. I wrote the cstruct class for a clear purpose. It does not need to be large but complete, has moderate functionality, and is easy to use. With cstruct, we can easily deal with win api. AboutCstruct
I will write another article to introduce it. Here, it is just a set of running dragons.

Let's take a look at the functions provided by the processmanager class:

Require 'process _ manager' <br/> # examples of procmgr use <br/> procmgr = processmanager. new <br/> # list all processes <br/> procmgr. procs {| proc | puts Proc. name} <br/> # Query Process Information by name. (all with the same name will be listed) <br/> procmgr. query ("svchost.exe") {| proc | puts "# {Proc. name} id = # {Proc. id}/N "} <br/> # terminate a process by name (all processes with the same name will be terminated) <br/> procmgr. kill ("notepad.exe") <br/> # terminate a process by ID <br/> procmgr. kill (3412) <br/> # Find out the process loaded with advapi32.dll in the system, <br/> # print out the location where advapi32.dll is loaded in the Process <br/> procmgr. procs do | proc | <br/> Proc. modules do | mod | <br/> If Mod. name. casecmp ('advapi32. dll ') = 0 <br/> printf "# {Proc. name} loaded 'advapi32. dll 'at % 08x/N ", Mod. image_base <br/> Break <br/> end

 

With the procs (list process) and modules (list module) methods provided by processmanager, we can write our GUI version of Process Manager. Most of the work is to use sharpdevelop to layout the interface.It should be noted that the variable names generated by sharpdevelop do not conform to Ruby naming conventions. If you are not comfortable with it, you can change it. This article has not been changed, but both are true.

The program has two main functions: show_processes and show_modules. The Code is as follows:

Show_processes:

Def show_processes <br/> # Clear the listview Control <br/> @ proclistview. items. clear <br/> @ proc_cache.clear </P> <p> # list system processes <br/> @ procmgr. procs do | proc | </P> <p> # cache proc object <br/> @ proc_cache <proc <br/> proc_mod = nil </P> <p> # retrieve the module corresponding to the Process <br/> Proc. modules do | mod | <br/> If file. basename (mod. path ). casecmp (Proc. name) = 0 <br/> proc_mod = mod <br/> Break <br/> end <br/> # If the module cannot be obtained, it creates a false one. <Br/> unless proc_mod <br/> proc_mod = struct. new (: path,: image_base,: image_size ). new (Proc. name,) <br/> end <br/> # retrieve the icon of the Process file <br/> @ small_proc_imagelist.images.add apputils. get_file_small_icon (proc_mod.path.gsub ("//?? // ",''), Apputils. is_system_process (proc_mod.path) <br/> # insert related information into the listview control. <Br/> proc_items = @ proclistview. items </P> <p> proc_items <system: string. new (System: Text: encoding. getencoding (0 ). getchars (proc_mod.path) <br/> proc_items [proc_items.Count-1]. imageindex = @ small_proc_imagelist.Images.size-1 <br/> proc_items [proc_items.Count-1]. subitems. add sprintf ("% 08x", Proc. ID) <br/> proc_items [proc_items.Count-1]. subitems. add sprintf ("% 08x", proc_mod.image_base) <br/> proc_items [proc_items.Count-1]. subitems. add sprintf ("% 08x", proc_mod.image_size) </P> <p> end <br/> # Set imagelist <br/> @ proclistview. smallimagelist = @ small_proc_imagelist <br/> end <br/>

 

Show_modules:

Def show_modules seleced_proc <br/> return unless seleced_proc <br/> # list the modules of the selected Process <br/> seleced_proc.modules do | mod | <br/> mod_items = @ modlistview. items </P> <p> # retrieve the module file icon <br/> @ small_mod_imagelist.images.add apputils. get_file_small_icon (mod. path. gsub ("//?? // ",''), Apputils. is_system_process (mod. Path) </P> <p> # insert related information into the listview control. <Br/> mod_items <system: string. new (System: Text: encoding. getencoding (0 ). getchars (mod. path) <br/> mod_items [mod_items.Count-1]. imageindex = @ small_mod_imagelist.Images.size-1 <br/> mod_items [mod_items.Count-1]. subitems. add sprintf ("% 08x", Mod. image_base) <br/> mod_items [mod_items.Count-1]. subitems. add sprintf ("% 08x", Mod. image_size) <br/> end <br/> # Set imagelist <br/> @ modlistview. smallimagelist = @ small_mod_imagelist <br/> end

 

For specific code, see the source code in the attachment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.