<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Logischer Fehler in Iteratorklasse]]></title><description><![CDATA[<p>Hallo <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>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.<br />
Die Klasse wrappt zwar einen Teil der WinAPI, das Problem ist aber nicht die WinAPI und es ist auch ohne Kenntniss jener verständlich.</p>
<p>Wenn ich meine Iteratorklasse wie folgt nutze:</p>
<pre><code class="language-cpp">for(ModuleIterator it(proc.getCurrentProcess()); it != ModuleIterator(); ++it) 
{
	wcout &lt;&lt; L&quot;Name: &quot; &lt;&lt; it-&gt;szModule &lt;&lt; L&quot;\n&quot;;
	wcout &lt;&lt; L&quot;Full path: &quot; &lt;&lt; it-&gt;szExePath &lt;&lt; L&quot;\n&quot;;
	wcout &lt;&lt; L&quot;Allocation base: 0x&quot; &lt;&lt; hex &lt;&lt; it-&gt;modBaseAddr &lt;&lt; L&quot;\n&quot;;
	wcout &lt;&lt; L&quot;Size: 0x&quot; &lt;&lt; it-&gt;modBaseSize &lt;&lt; L&quot;\n&quot; &lt;&lt; L&quot;\n&quot;;
}
</code></pre>
<p>werden nur Teile der Modulliste ausgegeben..löse ich das Ganze mit der rohen WinAPI passt es...</p>
<pre><code class="language-cpp">MODULEENTRY32W modEntry = { sizeof(modEntry) };
for(	BOOL moreModEntries = Module32FirstW(modSnapshot, &amp;modEntry);
	     moreModEntries;
	     moreModEntries = Module32NextW(modSnapshot, &amp;modEntry))
{
  wcout &lt;&lt; ...
}
</code></pre>
<p>Da muss irgendwo der Wurm in meiner Klasse sein.<br />
Hier ist sie, verzeiht meine Englischfails etc <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<pre><code class="language-cpp">/*
    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 &lt;http://www.gnu.org/licenses/&gt;.
*/

#if (defined _MSC_VER) &amp;&amp; (_MSC_VER &gt;= 1200)
	#pragma once
#endif

#ifndef NAVIGATOR_TLHELPITERATOR_HPP
#define NAVIGATOR_TLHELPITERATOR_HPP

#include &lt;stdexcept&gt;
#include &lt;iterator&gt;

#include &lt;Windows.h&gt;
#include &lt;TlHelp32.h&gt;

#include &quot;Types.hpp&quot;
#include &quot;../WinException.hpp&quot;

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&lt;	class entry_t,
					BOOL (__stdcall *func_getfirst)(HANDLE, entry_t*),
					BOOL (__stdcall *func_getnext)(HANDLE, entry_t*),
					unsigned long flag_spec&gt;
	class TlhelpIterator : public std::iterator&lt;	std::input_iterator_tag,
																entry_t&gt;
	{

	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(	&quot;TlhelpIterator::TlhelpIterator()&quot;,
											&quot;CreateToolhelp32Snapshot()&quot;,
											error);
			}

			//Get first entry
			state_ = func_getfirst(snapshot_, &amp;entry_);
			if(!state_)
			{
				DWORD error = GetLastError();
				throw WinException(	&quot;TlhelpIterator::TlhelpIterator()&quot;,
											&quot;func_getfirst()&quot;,
											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&amp; 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&amp; it) const
		{
			return !(*this == it);
		}

		/**
		* Pseudo-dereferencing operator returning the current entry.
		* @return Current entry.
		*/
		const entry_t&amp; operator*() const
		{
			using namespace std;

			if(!isInValidState())
			{
				throw runtime_error(	&quot;TlhelpIterator::operator*() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			return entry_;
		}

		/**
		* Pseudo-pointer operator returning a pointer to the current entry.
		* @return Pointer to current entry.
		*/
		const entry_t* operator-&gt;() const
		{
			using namespace std;

			if(!isInValidState())
			{
				throw runtime_error(	&quot;TlhelpIterator::operator*() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			return &amp;entry_;
		}

		/**
		* Preincrement operator jumping to next entry and returning
		* incremented iterator.
		* @return Incremented iterator.
		*/
		TlhelpIterator&amp; operator++() 
		{
			using namespace std;

			if(!isInValidState())
			{
				throw runtime_error(	&quot;TlhelpIterator::operator++() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			state_ = func_getnext(snapshot_, &amp;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(	&quot;TlhelpIterator::operator++() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			TlhelpIterator result = *this;
			++(*this);
			return result;
		}

	protected:
		HANDLE snapshot_;
		entry_t entry_;
		BOOL state_;
	};

	typedef TlhelpIterator&lt;	PROCESSENTRY32W,
									Process32FirstW,
									Process32NextW,
									TH32CS_SNAPPROCESS&gt;	ProcessIterator;

	typedef TlhelpIterator&lt; THREADENTRY32,
									Thread32First,
									Thread32Next,
									TH32CS_SNAPTHREAD&gt;	ThreadIterator;

	typedef TlhelpIterator&lt;	MODULEENTRY32W,
									Module32FirstW,
									Module32NextW,
									TH32CS_SNAPMODULE&gt;	ModuleIterator;

	typedef TlhelpIterator&lt;	HEAPLIST32,
									Heap32ListFirst,
									Heap32ListNext,
									TH32CS_SNAPHEAPLIST&gt;	HeapListIterator;

}

#endif //NAVIGATOR_TLHELPITERATOR_HPP
</code></pre>
<p>Falls sich das jemand antut und helfen möchte bedanke ich mich schon einmal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Grüße,<br />
Flo</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/276452/logischer-fehler-in-iteratorklasse</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 10:18:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/276452.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Nov 2010 14:46:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Logischer Fehler in Iteratorklasse on Tue, 02 Nov 2010 14:46:08 GMT]]></title><description><![CDATA[<p>Hallo <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>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.<br />
Die Klasse wrappt zwar einen Teil der WinAPI, das Problem ist aber nicht die WinAPI und es ist auch ohne Kenntniss jener verständlich.</p>
<p>Wenn ich meine Iteratorklasse wie folgt nutze:</p>
<pre><code class="language-cpp">for(ModuleIterator it(proc.getCurrentProcess()); it != ModuleIterator(); ++it) 
{
	wcout &lt;&lt; L&quot;Name: &quot; &lt;&lt; it-&gt;szModule &lt;&lt; L&quot;\n&quot;;
	wcout &lt;&lt; L&quot;Full path: &quot; &lt;&lt; it-&gt;szExePath &lt;&lt; L&quot;\n&quot;;
	wcout &lt;&lt; L&quot;Allocation base: 0x&quot; &lt;&lt; hex &lt;&lt; it-&gt;modBaseAddr &lt;&lt; L&quot;\n&quot;;
	wcout &lt;&lt; L&quot;Size: 0x&quot; &lt;&lt; it-&gt;modBaseSize &lt;&lt; L&quot;\n&quot; &lt;&lt; L&quot;\n&quot;;
}
</code></pre>
<p>werden nur Teile der Modulliste ausgegeben..löse ich das Ganze mit der rohen WinAPI passt es...</p>
<pre><code class="language-cpp">MODULEENTRY32W modEntry = { sizeof(modEntry) };
for(	BOOL moreModEntries = Module32FirstW(modSnapshot, &amp;modEntry);
	     moreModEntries;
	     moreModEntries = Module32NextW(modSnapshot, &amp;modEntry))
{
  wcout &lt;&lt; ...
}
</code></pre>
<p>Da muss irgendwo der Wurm in meiner Klasse sein.<br />
Hier ist sie, verzeiht meine Englischfails etc <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<pre><code class="language-cpp">/*
    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 &lt;http://www.gnu.org/licenses/&gt;.
*/

#if (defined _MSC_VER) &amp;&amp; (_MSC_VER &gt;= 1200)
	#pragma once
#endif

#ifndef NAVIGATOR_TLHELPITERATOR_HPP
#define NAVIGATOR_TLHELPITERATOR_HPP

#include &lt;stdexcept&gt;
#include &lt;iterator&gt;

#include &lt;Windows.h&gt;
#include &lt;TlHelp32.h&gt;

#include &quot;Types.hpp&quot;
#include &quot;../WinException.hpp&quot;

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&lt;	class entry_t,
					BOOL (__stdcall *func_getfirst)(HANDLE, entry_t*),
					BOOL (__stdcall *func_getnext)(HANDLE, entry_t*),
					unsigned long flag_spec&gt;
	class TlhelpIterator : public std::iterator&lt;	std::input_iterator_tag,
																entry_t&gt;
	{

	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(	&quot;TlhelpIterator::TlhelpIterator()&quot;,
											&quot;CreateToolhelp32Snapshot()&quot;,
											error);
			}

			//Get first entry
			state_ = func_getfirst(snapshot_, &amp;entry_);
			if(!state_)
			{
				DWORD error = GetLastError();
				throw WinException(	&quot;TlhelpIterator::TlhelpIterator()&quot;,
											&quot;func_getfirst()&quot;,
											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&amp; 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&amp; it) const
		{
			return !(*this == it);
		}

		/**
		* Pseudo-dereferencing operator returning the current entry.
		* @return Current entry.
		*/
		const entry_t&amp; operator*() const
		{
			using namespace std;

			if(!isInValidState())
			{
				throw runtime_error(	&quot;TlhelpIterator::operator*() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			return entry_;
		}

		/**
		* Pseudo-pointer operator returning a pointer to the current entry.
		* @return Pointer to current entry.
		*/
		const entry_t* operator-&gt;() const
		{
			using namespace std;

			if(!isInValidState())
			{
				throw runtime_error(	&quot;TlhelpIterator::operator*() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			return &amp;entry_;
		}

		/**
		* Preincrement operator jumping to next entry and returning
		* incremented iterator.
		* @return Incremented iterator.
		*/
		TlhelpIterator&amp; operator++() 
		{
			using namespace std;

			if(!isInValidState())
			{
				throw runtime_error(	&quot;TlhelpIterator::operator++() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			state_ = func_getnext(snapshot_, &amp;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(	&quot;TlhelpIterator::operator++() Error : &quot; \
											&quot;Object is in invalid state&quot;);

			}

			TlhelpIterator result = *this;
			++(*this);
			return result;
		}

	protected:
		HANDLE snapshot_;
		entry_t entry_;
		BOOL state_;
	};

	typedef TlhelpIterator&lt;	PROCESSENTRY32W,
									Process32FirstW,
									Process32NextW,
									TH32CS_SNAPPROCESS&gt;	ProcessIterator;

	typedef TlhelpIterator&lt; THREADENTRY32,
									Thread32First,
									Thread32Next,
									TH32CS_SNAPTHREAD&gt;	ThreadIterator;

	typedef TlhelpIterator&lt;	MODULEENTRY32W,
									Module32FirstW,
									Module32NextW,
									TH32CS_SNAPMODULE&gt;	ModuleIterator;

	typedef TlhelpIterator&lt;	HEAPLIST32,
									Heap32ListFirst,
									Heap32ListNext,
									TH32CS_SNAPHEAPLIST&gt;	HeapListIterator;

}

#endif //NAVIGATOR_TLHELPITERATOR_HPP
</code></pre>
<p>Falls sich das jemand antut und helfen möchte bedanke ich mich schon einmal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Grüße,<br />
Flo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1974164</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1974164</guid><dc:creator><![CDATA[Icematix]]></dc:creator><pubDate>Tue, 02 Nov 2010 14:46:08 GMT</pubDate></item></channel></rss>