Next Generation Emulation banner
21 - 38 of 38 Posts

· You're already dead...
Joined
·
10,293 Posts
registering and creating windows and managing their events is what i hate about c++ too.
in fact, that's pretty much my only complaint about it.

also about readability:
on parts of code i don't want people to touch, then i don't care if the other people can read it or not, because they shouldn't be touching it.
on other stuff, i try to code nicely and comment anything too-complex.
 

· Emu Author
Joined
·
616 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.
 

· You're already dead...
Joined
·
10,293 Posts
how much do you know of win32 window creation?
well i'm far from an expert i'll tell you that xD

i basically learned the basics, implemented some code and just reuse it on future projects.
then i read up on msdn or some tutorials whenever i need to use new controls.
a wise coder once told me something like:
"There's no need to memorize everything, that's what docs are for."

i used to think i was supposed to memorize crap like window registration code, and DX initialization code. but then i found out people just copy the code when needed, and just memorize "how to use it", instead of memorizing the specifics.


so far, most my projects in c++ have not needed anything too-fancy in terms of window creation, and i've been able to do everything i needed.

i implement new dialog boxes using the MSVC dialog builder.
you can see an easy example on how to open new dialog boxes created with the dialog-builder when you create a new win32 application with Visual Studio and use the Win32 Application Setup Wizard.
they generate an about-box dialog for you, and you can just do some copy-pasting/modding to create new windows xD

that's basically how i get by.
if i had an application that's very window-heavy and not performance critical, then i'd use VB instead since its a lot simpler.
i plan to learn c# in the future, so i might start using that as my 'rapid app development' language.


also one thing i learned:
even very experienced c++ veterans hate implementing fancy c++ gui stuff. its just a pain to code, and the gui is something you just want to get done so you can code the 'real stuff'. xD
 

· 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..
 

· Emu Author
Joined
·
616 Posts
a long time ago i actually created my own dx controls, over 2500 lines of code but i never learned how to effectively use it so i just sat it to the side when i just started giving up on win32 stuff. i even converted it into a compiled dll file but i never could make it work right or maybe i did, i can not remember as it has been so long ago now. maybe i can go back and try to figure all that out again soon.
 

· Registered
Joined
·
598 Posts
Discussion Starter · #27 · (Edited)
Hey I have a question.I made that when the button is clicked,that the list box control displays ALL the current active processes.My code works,but there is one problem for which I need help.The list box DOES NOT display the extension for ANY of the processes like ".exe",".bat",".dll" and so on...
How can I display the extension for the processes?
Here is the code I used:

privatevoid button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
Process[] procs=Process.GetProcesses(Environment.MachineName);
foreach(Process process in procs)
{
listBox1.Items.Add(process.ProcessName.ToString());
}
}

:rolleyes:
 

· 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);
        }
 

· Registered
Joined
·
598 Posts
Discussion Starter · #29 ·
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);
        }
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??????:(
 

· 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 { }
            }
        }
 

· Registered
Joined
·
598 Posts
Discussion Starter · #33 · (Edited)
Rauntec......................do you have any idea how much you just helped me?????????????????Let me tell you...
A LOT DUDE!!!!!!!!!!!!!!!!:emb:
Thanks man I will never forget you this.Never!
Anyway the code you gave me for showing the currently active processes is working perfect.And then I wanted to make the end process button too,(and I will admit that I am not a very good programmer),and after 1 hour of thinking like hell,I managed to get my code working.The button1 click event is for showing the currently active processes and the button2 click event is ending any of the selected process.Here is the code:

privatevoid button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
Process[] procs=Process.GetProcesses(Environment.MachineName.ToString());
foreach(Process process in procs)
{
if(process.MainWindowTitle.Length>0)
{
listBox1.Items.Add(process.MainModule.ModuleName);
}
}
}
privatevoid button2_Click(object sender, System.EventArgs e)
{
foreach(Process procs in Process.GetProcesses(Environment.MachineName.ToString()))
{
if(procs.MainModule.ModuleName.Equals(listBox1.SelectedItem.ToString()))
{
procs.Kill();
break;
}
}
}


Ohh and Rauntec...man you know programming!:thumb:

Just one curious question...by this--->if(process.MainWindowTitle.Length>0)<---do you mean on the number of letters in the main title of the window????lol.....
And here--->Process[] procs=Process.GetProcesses(".");<---why here you use (".") instead of the Environment.MachineName.ToString() ???
 

· 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
 

· Registered
Joined
·
598 Posts
Discussion Starter · #35 ·
Here is the code I used to display all the currently active processes plus ALL the background processes,(the code for ending the selected process is the same like the one from above example):

private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
Process[] procs=Process.GetProcesses(Environment.MachineName.ToString());
foreach(Process process in procs)
{
try
{
listBox1.Items.Add(process.MainModule.ModuleName);
}
catch(Exception)
{
//No function to execute...
}
}
}


I would like to hear your opinion about this example...
This code perfectly displays all the currently active processes and all the background processes.However I need to catch the exception otherwise the application crashes because of an unhandled exception of type "System.NullReferenceException" which occurrs in my application.Any idea why??
 

· 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.
 

· Registered
Joined
·
598 Posts
Discussion Starter · #37 ·
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.
Lol you didn't lie when you said that the other ways are too complicated for me xD
Anyway...just 3 days ago I wrote the sample code which was supposed to end the process called "notepad.exe".When I would execute the button click event,the process would not end.However if instead of the "notepad.exe" I write "notepad" then the process ends.Here is the code:

string processName="notepad";
Process[] procs=Process.GetProcessesByName(processName);
foreach(Process process in procs)
{
process.Kill();
}

And just now few minutes ago I was crashing my head by thinking why the hell "notepad.exe" is not found by it's name.And then I remember the modules you mentioned.And finally after 30 minutes of trying,I wrote the code which successfuly ends "notepad.exe" or any other process.:wub:In the bellow example,I wrote the code so that the user can write his own process name which he/she wants to end.Take a look at the image and tell me if this is ok:

 

· 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
 
21 - 38 of 38 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