View Single Post
Old June 10th, 2009   #38 (permalink)
@ruantec
Emu author
 
@ruantec's Avatar
 
Join Date: Nov 2002
Location: Austria (originally from Dominican Republic)
Posts: 2,380
mmmm it doesnīt look that bad but to be honest i donīt like the continuous loops.. it may work fine for a small app but when you are working on big apps that required performance such ways are big performance killers... so iīll show you how is done a bit faster using API..

check this:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace process
{
    public partial class Form1 : Form
    {
        /*API Declarations*/
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_CLOSE = 0xF060;



        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listProcs();   
        }

        private void listProcs()
        {
            Process[] procs = Process.GetProcesses(Environment.MachineName);
            listBox1.Items.Clear();
            foreach (Process process in procs)
            {
                try
                {
                    if (process.MainWindowTitle.Length > 0)
                    {
                        listBox1.Items.Add(process.MainModule.ModuleName + " - " + process.MainWindowHandle);
                        //foreach (ProcessModule modul in process.Modules)
                        //{
                        //    listBox1.Items.Add(modul.ModuleName);

                        //    //Here some more in case you need them
                        //    //modul.FileName + Environment.NewLine;
                        //    //modul.ModuleMemorySize.ToString();
                        //    //modul.FileVersionInfo.FileVersion;
                        //}
                    }
                }
                catch { }
            }
        }

        private void listBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && listBox1.SelectedItem != null && listBox1.SelectedItem.ToString().Length > 0)
            {
                int ihandle = Convert.ToInt32(listBox1.SelectedItem.ToString().Split('-')[1].Trim());
                SendMessage(ihandle, WM_SYSCOMMAND, SC_CLOSE, 0);
                listProcs();
            }
        }
    }
}
as you can see iīve added the FindWindow API.. this api requires the ProcessName and the MainWindowTitle as values and it returns a integer back which is the mainWindowHandle that can be > 0 when the window has been found and 0 when nothing was found... to use the FindWindow API do something like this:

Code:
            int iHandle = FindWindow("wmplayer", "Windows Media Player");
            if (iHandle > 0)
            {
                // close the window using API        
                SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
            }

Regards
@ruantec
__________________

Current development tools:

Visual C++.net, Visual C#.net
Visual VB.net, Visual Webdeveloper.net
Bloodshed Dev C++, Borland C++
Visual Basic 6

Last edited by @ruantec; June 10th, 2009 at 22:41..
@ruantec is offline   Reply With Quote