Thread: Array of handles

  1. #1
    chreez
    Guest

    Array of handles

    I'm making a program that lists all running processes and gets the handle to the process using OpenProcess() and I had a few questions. I made a struct called Proc that looks like this:

    Code:
    class Proc
    {
    public:
    HANDLE hproc;
    PROCESSENTRY32 proc;
    Proc(){ hSnap = NULL; }
    };
    I know it's not the coolest thing in the world, but it helped me organize the crap alot. Then I declare an array of Procs. Because I need to CloseHandle() for each OpenProcess() that I have, would it work if I did something like this:

    Code:
    Proc allProcs[256];
    ...
    allProcs[i].hproc = OpenProcess(PROCESS_ALL_ACCESS, TRUE, allProcs[i].proc.th32ProcessID;
    ...
    CloseHandle(allProcs); // no matter how many processes I open, would this close all of them? This could minimize some code if it worked, but I could get around it.
    Thanks for your answers
    chreez

    oh, btw, can a HANDLE and a HWND sometimes be interchangeable?

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    [EDIT]

    ...I forgot I had an account here....

    Anyways, since I can't make an edit in the above section, here's a correction:

    Code:
    class Proc
    {
    public:
    HANDLE hproc;
    PROCESSENTRY32 proc;
    Proc(){ hproc = NULL; } //used to be hSnap, but should be this
    };
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Since CloseHandle() wants a HANDLE as it's parameter, I would doubt what you propose would compile.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    oh, yea, I forgot that, what I meant was

    Code:
    CloseHandle(allProcs.hproc);
    would that work, should've been my original example.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    That would also fail; allProcs by itself is a pointer to the first element in the array, so you'd have to use CloseHandle(allProcs->hproc); at the very least, but that would close the handle of only the first Proc struct.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, thanks, perfect answer, even though it means more code, doesn't matter 'cause it's already implemented.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM