Next Generation Emulation banner
1 - 8 of 8 Posts

· Premium Member
Joined
·
19,572 Posts
how much do you know of win32 window creation? my main thing was i could almost never get my code to create addition windows right, i could make them but when i closed one it closed everything down, i fixed it once but i broke it and never got it working again :/. than again with everything i learned from c#, if i went back now and tried, i most likely could get it easily lol. learning c# helped me learn aspects of coding that i found rather difficult in c++ and was able to learn those a lot easier, i wonder if i could code much more efficiently in c++ now lol.
C/C++ has always been a pain when it comes to GUI but there are several ways to create them via API in a easy way. when you need your GUI to be fancy you have to deal with few things in windows... first is to create a form then by using the SetLayeredWindow api you can transform that form into a layer by passing the hwnd of the window. once you get your layer ready you can use GDI+ to draw everything you need. there are also DX ways to do that but they are too complicated.

the only problem(well not a problem for me as is what i love) you have with that technique is that you´re dealing with a per-pixel app that means you can´t draw a hwnd(control) over it as it won´t be visible. so your only choice is to show yourself and write your own controls and functionality which to me helps a lot as you can add/remove whatever you want and in most cases they consume less memory and do the job faster(Showed in the old @ES when loading 25,8GB of Audio information). by using a second form you can stick one to another and create several layers that allows you to hold normal controls or draw whatever you want there such as picturebox etc..
 

· Premium Member
Joined
·
19,572 Posts
if you want to get the process name just do that:

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            Process[] procs = Process.GetProcesses(Environment.MachineName);
            foreach (Process process in procs)
                listBox1.Items.Add(process.ProcessName);
        }
 

· Premium Member
Joined
·
19,572 Posts
Lol rauntec.I know what I need to do to get the current processes.The thing I don't know is WHAT I need to do to get the extension for EACH process.This code that I gave here shows the current processes like for example "notepad"....blablabla.What I want to do is how to show the EACH process extension,for example--->"notepad.exe"<---You can here see the EXE extension.

So how to display the extension for EACH process??????:(
lol that´s quite easy.. for that you are going to need the modules on each process... the processname.modules have a collection of modules of type "ProcessModule" you can use it like that:

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            Process[] procs = Process.GetProcesses(".");
            foreach (Process process in procs)
            {
                try
                {
                    foreach (ProcessModule modul in process.Modules)
                    {
                        listBox1.Items.Add(modul.ModuleName);

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

Tip from me....
a good way to find things out by yourself is by using a break poing and move your mouse over the object you want to explore... explore the required objects and you can browse every single part of it without problems just like this:
 

· Premium Member
Joined
·
19,572 Posts
That does shows the extensions,but it's not showing my currently active processes like WMP and so on...lol.
lol you have wishes :p :eyemove:

by accessing the main module you can get all the info you need... anyways here you go.

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            Process[] procs = Process.GetProcesses(".");
            foreach (Process process in procs)
            {
                try
                {
                    if (process.MainWindowTitle.Length > 0)
                    {
                        listBox1.Items.Add(process.MainModule.ModuleName);
                        //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 { }
            }
        }
 

· Premium Member
Joined
·
19,572 Posts
Glad i could be of help ;)

btw my nick is @ruantec and not Ruantec :p .... now about your questions..

Process[] procs=Process.GetProcesses(".") ensures to get all processes on every machine on your network as far as i know(i may be wrong maybe) while Environment.MachineName.ToString() just search a target machine... anyways if you move your mouse over the .MachineName you will realize that is already a string and therefore the conversion to .Tostring() is not needed so you can write Environment.MachineName.

by writing process.MainWindowTitle.Length>0 i check for the mainmodule title.. background, system and other processes doesn´t have a window at all and therefore they lack of windowtitle. opened processes do have a windowtitle and that´s why i check there length to see if is a open process. anyways theres another method to find opened applications other than the one i´ve showed you sofar but i think the one you use makes the job for now so no need to show the API way :p .

one last thing...when you list your process i recommend you to use a listview and create different columns for your data were in one of them you store the processID or the handle of the process.. in that way you don´t have to loop over and over to end a single process because when you click a selected item on the list you can easily get the ID back from the subitem column of your listview.. this will increase performance and at the same time make your app more responsible in case it stuck somewhere lol.

Regards
@ruantec
 

· Premium Member
Joined
·
19,572 Posts
Your code is fine and the reason for the exception is quite obvious too... if you check the pic on my post #30 i showed you how to explore your objects so you can debug your code and check your array of processes. if you check few of the array items you will notice that under the main module and in other areas the access is restricted and for that reason you´re getting that exception.

a simple:
Code:
    Try
    {
            //Your code
    }
    Catch{}
should do the trick for now.. there are other ways but probably too complicated for you atm.
 

· Premium Member
Joined
·
19,572 Posts
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
 
1 - 8 of 8 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top