Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8098301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:02:23+00:00 2026-06-05T22:02:23+00:00

I am designing an input iterator type that enumerates all running processes in a

  • 0

I am designing an input iterator type that enumerates all running processes in a system.

This is similar to an iterator I designed to enumerate modules in a process. The module iterator takes a ‘process’ object in the constructor, and a default constructed iterator is considered to be the off-the-end iterator.

Example:

hadesmem::ModuleIterator beg(process);
hadesmem::ModuleIterator end;
assert(beg != end);

I do not know what to do about process enumeration though, because there is no ‘state’ or information that needs to be given to the iterator (everything is handled internally by the iterator using the Windows API).

Example:

// This is obviously a broken design, what is the best way to distinguish between the two?
hadesmem::ProcessIterator beg;
hadesmem::ProcessIterator end;

What is the idiomatic way to deal with this situation? i.e. Where you need to distinguish between the creation of a ‘new’ iterator and an off-the-end iterator when nothing needs to be given to the iterator constructor.

If it’s relevant, I am able to use C++11 in this library, as long as it’s supported by VC11, GCC 4.7, and ICC 12.1.

Thanks.

EDIT:

To clarify, I know that it’s not possible to distinguish between the two in the form I’ve posted above, so what I’m asking is more of a ‘design’ question than anything else… Maybe I’m just overlooking something obvious though (wouldn’t be the first time).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:02:24+00:00Added an answer on June 5, 2026 at 10:02 pm

    If you create a class that holds the parameters that go into the CreateToolhelp32Snapshot() representing the snapshot you’re iterating over, you’ll have a natural factory for the iterators. Something like this should work (I’m not on Windows, so not tested):

    class Process;
    class Processes {
        DWORD what, who;
    public:
        Processes(DWORD what, DWORD who) : what(what), who(who) {}
    
        class const_iterator {
            HANDLE snapshot;
            LPPROCESSENTRY32 it;
            explicit const_iterator(HANDLE snapshot, LPPROCESSENTRY32 it)
                : snapshot(snapshot), it(it) {}
        public:
            const_iterator() : snapshot(0), it(0) {}
    
            // the two basic functions, implement iterator requirements with these:
            const_iterator &advance() {
                assert(snapshot);
                if ( it && !Process32Next(snapshot, &it))
                    it = 0;
                return *this;
            }
            const Process dereference() const {
                assert(snapshot); assert(it);
                return Process(it);
            }
            bool equals(const const_iterator & other) const {
                return handle == other.handle && it == other.it;
            }
        };
    
        const_iterator begin() const {
            const HANDLE snapshot = CreateToolhelp32Snapshot(what, who);
            if (snapshot) {
                LPPROCESSENTRY32 it;
                if (Process32First(snapshot, &it))
                    return const_iterator(snapshot, it);
            }
            return end();
        }
        const_iterator end() const {
            return const_iterator(snapshot, 0);
        }
    };
    
    inline bool operator==(Processes::const_iterator lhs, Processes::const_iterator rhs) {
        return lhs.equals(rhs);
    }
    inline bool operator!=(Processes::const_iterator lhs, Processes::const_iterator rhs) {
        return !operator==(lhs, rhs);
    }
    

    Usage:

    int main() {
        const Processes processes( TH32CS_SNAPALL, 0 );
        for ( const Process & p : processes )
            // ...
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm designing a delegate method that is called when the remote server needs input
On a page that I'm designing I have a form with one input of
I'm defining an iterator type that does not store its current value explicitly. Instead,
I'm currently designing a login system for a make-believe company, right now all I
I am designing a web page with multi line Label name & input type
I'm designing a database that will need to be optimized for maximum speed. All
When designing a stock management database system for sales and purchases what would be
I'm designing a method for an input handler class. Here is some pseudo code...
I'm designing a template creation tool, which uses a jQuery Ajax request that posts
I am designing a form where I need to input two individuals, both fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.