Logischer Fehler in Iteratorklasse



  • Hallo 🙂

    Ich komme mir richtig doof vor soetwas ins Forum zu setzen, aber ich bin gerade nicht imstande einen logischen Fehler in meiner Iteratorklasse zu finden.
    Die Klasse wrappt zwar einen Teil der WinAPI, das Problem ist aber nicht die WinAPI und es ist auch ohne Kenntniss jener verständlich.

    Wenn ich meine Iteratorklasse wie folgt nutze:

    for(ModuleIterator it(proc.getCurrentProcess()); it != ModuleIterator(); ++it) 
    {
    	wcout << L"Name: " << it->szModule << L"\n";
    	wcout << L"Full path: " << it->szExePath << L"\n";
    	wcout << L"Allocation base: 0x" << hex << it->modBaseAddr << L"\n";
    	wcout << L"Size: 0x" << it->modBaseSize << L"\n" << L"\n";
    }
    

    werden nur Teile der Modulliste ausgegeben..löse ich das Ganze mit der rohen WinAPI passt es...

    MODULEENTRY32W modEntry = { sizeof(modEntry) };
    for(	BOOL moreModEntries = Module32FirstW(modSnapshot, &modEntry);
    	     moreModEntries;
    	     moreModEntries = Module32NextW(modSnapshot, &modEntry))
    {
      wcout << ...
    }
    

    Da muss irgendwo der Wurm in meiner Klasse sein.
    Hier ist sie, verzeiht meine Englischfails etc 😉

    /*
        This file is part of Process++.
    
        Process++ is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.
    
        Process++ is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with Process++.  If not, see <http://www.gnu.org/licenses/>.
    */
    
    #if (defined _MSC_VER) && (_MSC_VER >= 1200)
    	#pragma once
    #endif
    
    #ifndef NAVIGATOR_TLHELPITERATOR_HPP
    #define NAVIGATOR_TLHELPITERATOR_HPP
    
    #include <stdexcept>
    #include <iterator>
    
    #include <Windows.h>
    #include <TlHelp32.h>
    
    #include "Types.hpp"
    #include "../WinException.hpp"
    
    namespace Navigator
    {
    	/**
    	* STL compliant input iterator
    	* Template class to wrap iterating the Tlhelp32 API.
    	* entry_t Type of entry returned by the First/Next functions
    	* func_getfirst Function pointer to function retrieving the first entry
    	* func_getnext Function pointer to function retrieving the next entry
    	* flag_spec Defines a flag for use in CreateToolhelp32Snapshot()
    	*/
    	template<	class entry_t,
    					BOOL (__stdcall *func_getfirst)(HANDLE, entry_t*),
    					BOOL (__stdcall *func_getnext)(HANDLE, entry_t*),
    					unsigned long flag_spec>
    	class TlhelpIterator : public std::iterator<	std::input_iterator_tag,
    																entry_t>
    	{
    
    	public:
    
    		/**
    		* Simple Constructor allocating data and validating the iterator.
    		* @param pid Specify a process id for which data should be iterated.
    		* Passing zero will iterate all specific resources on the system.
    		*/
    		TlhelpIterator(ProcessId pid)
    		{
    			//Get a snapshot
    			entry_.dwSize = sizeof(entry_t);
    			snapshot_ = CreateToolhelp32Snapshot(flag_spec, pid);
    			if(snapshot_ == INVALID_HANDLE_VALUE)
    			{
    				DWORD error = GetLastError();
    				throw WinException(	"TlhelpIterator::TlhelpIterator()",
    											"CreateToolhelp32Snapshot()",
    											error);
    			}
    
    			//Get first entry
    			state_ = func_getfirst(snapshot_, &entry_);
    			if(!state_)
    			{
    				DWORD error = GetLastError();
    				throw WinException(	"TlhelpIterator::TlhelpIterator()",
    											"func_getfirst()",
    											error);
    			}
    		}
    
    		/**
    		* Hackish constructor to create an invalid iterator.
    		* Constructing it will create an invalid iterator
    		* for use in loops etc.
    		* @param Pass true/false.
    		*/
    		TlhelpIterator() : state_(FALSE)
    		{ }
    
    		/**
    		* Simple destructor freeing resources.
    		*/
    		~TlhelpIterator()
    		{
    			CloseHandle(snapshot_);
    		}
    
    		/*
    		* Checks if the iterator is valid.
    		* @return true if valid, false otherwise.
    		*/
    		bool isInValidState() const
    		{
    			return state_ != FALSE;
    		}
    
    		/**
    		* Comparison, ONLY checks for validity!
    		* @return true if both objects are in the same state, false otherwise.
    		*/
    		bool operator==(const TlhelpIterator& it) const
    		{
    			return (isInValidState() == it.isInValidState());
    		}
    
    		/**
    		* Comparison, ONLY checks for validity!
    		* @return True if both objects are in different states, false otherwise.
    		*/
    		bool operator!=(const TlhelpIterator& it) const
    		{
    			return !(*this == it);
    		}
    
    		/**
    		* Pseudo-dereferencing operator returning the current entry.
    		* @return Current entry.
    		*/
    		const entry_t& operator*() const
    		{
    			using namespace std;
    
    			if(!isInValidState())
    			{
    				throw runtime_error(	"TlhelpIterator::operator*() Error : " \
    											"Object is in invalid state");
    
    			}
    
    			return entry_;
    		}
    
    		/**
    		* Pseudo-pointer operator returning a pointer to the current entry.
    		* @return Pointer to current entry.
    		*/
    		const entry_t* operator->() const
    		{
    			using namespace std;
    
    			if(!isInValidState())
    			{
    				throw runtime_error(	"TlhelpIterator::operator*() Error : " \
    											"Object is in invalid state");
    
    			}
    
    			return &entry_;
    		}
    
    		/**
    		* Preincrement operator jumping to next entry and returning
    		* incremented iterator.
    		* @return Incremented iterator.
    		*/
    		TlhelpIterator& operator++() 
    		{
    			using namespace std;
    
    			if(!isInValidState())
    			{
    				throw runtime_error(	"TlhelpIterator::operator++() Error : " \
    											"Object is in invalid state");
    
    			}
    
    			state_ = func_getnext(snapshot_, &entry_);
    
    			return *this;
    		}
    
    		/**
    		* Postincrement operator jumping to next entry and returning
    		* previous iterator.
    		* @return Previous iterator.
    		*/
    		TlhelpIterator operator++(int) 
    		{
    			using namespace std;
    
    			if(!isInValidState())
    			{
    				throw runtime_error(	"TlhelpIterator::operator++() Error : " \
    											"Object is in invalid state");
    
    			}
    
    			TlhelpIterator result = *this;
    			++(*this);
    			return result;
    		}
    
    	protected:
    		HANDLE snapshot_;
    		entry_t entry_;
    		BOOL state_;
    	};
    
    	typedef TlhelpIterator<	PROCESSENTRY32W,
    									Process32FirstW,
    									Process32NextW,
    									TH32CS_SNAPPROCESS>	ProcessIterator;
    
    	typedef TlhelpIterator< THREADENTRY32,
    									Thread32First,
    									Thread32Next,
    									TH32CS_SNAPTHREAD>	ThreadIterator;
    
    	typedef TlhelpIterator<	MODULEENTRY32W,
    									Module32FirstW,
    									Module32NextW,
    									TH32CS_SNAPMODULE>	ModuleIterator;
    
    	typedef TlhelpIterator<	HEAPLIST32,
    									Heap32ListFirst,
    									Heap32ListNext,
    									TH32CS_SNAPHEAPLIST>	HeapListIterator;
    
    }
    
    #endif //NAVIGATOR_TLHELPITERATOR_HPP
    

    Falls sich das jemand antut und helfen möchte bedanke ich mich schon einmal 🙂
    Grüße,
    Flo


Anmelden zum Antworten