<?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[CreateToolhelp32Snapshot()==INVALID_HANDLE_VALUE mit notepad.exe]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich möchte ein kleines Konsolenprogramm schreiben womit ich einzelne Module in einem Prozess dumpen kann. Mit dumpen meine ich das Modul als xy.bin auf der Festplatte speichern.</p>
<p>Als erstes erstelle ich einen Snapshot aller laufenden Prozesse und lasse alle mit ProzessID auflisten. Dann muss die gewünschte PID eingegeben werden. Jetzt wollte ich eine Liste der Module anzeigen lassen indem ich vom Prozess mit der angegeben ProzessID einen Snapshot mache. Nun wollte ich es mit &quot;notepad.exe&quot; testen und bekomme immer ein invalid handle. Warum?</p>
<p>Notepad habe ich natürlich geöffnet, die PID ist auch korrekt.</p>
<p>Der Fehler liegt in der Funktion moduls(). GetLastError() gibt Fehler 299 &quot;ERROR_PARTIAL_COPY&quot; zurück.<br />
MSDN meint dazu:<br />
&quot;Only part of a ReadProcessMemory or WriteProcessMemory request was completed.&quot;<br />
Damit kann ich leider nichts anfangen!?</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;iostream&gt;
#include &lt;Tlhelp32.h&gt;

LPDWORD		PID=0;
HANDLE		hproc=0;
HANDLE		hsnap=0;

bool SetDebugPrivileges()
{
	HANDLE hToken;
	TOKEN_PRIVILEGES tokenPriv;
	tokenPriv.PrivilegeCount = 1;

	if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &amp;hToken))
	{
		return 0;
	}

	if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &amp;tokenPriv.Privileges[0].Luid))
	{
		return 0;
	}
	tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

	if(!AdjustTokenPrivileges(hToken, false, &amp;tokenPriv, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
	{
		return 0;
	}
	return 1;
}

bool OpenProc(char* targetproc)
{
	hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);				// Create snapshot of all processes and store the handle
	if (hsnap==INVALID_HANDLE_VALUE)
		return false;
	PROCESSENTRY32 process;												// Create process struct
	process.dwSize = sizeof(PROCESSENTRY32);							// Set size
	long nprocess = Process32First(hsnap, &amp;process);					// Store infos for first process
	std::cout &lt;&lt; &quot;PID     Threads Flags   Size    Name&quot; &lt;&lt; std::endl;
	while (nprocess)													// Are there any more processes?
	{
		std::cout &lt;&lt; process.th32ProcessID
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.cntThreads
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.dwFlags
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.dwSize
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.szExeFile
				  &lt;&lt; std::endl;
		nprocess = Process32Next(hsnap, &amp;process);						// Store infos for next process
	}
	std::cout &lt;&lt; &quot;enter the processID you want to dump: &quot; &lt;&lt; std::endl;
	std::cin.clear();
	char strPID[81];
	std::cin.getline(strPID, 80);
	PID=(LPDWORD)atoi(strPID);

	if ( (hproc = OpenProcess(PROCESS_ALL_ACCESS, 0, DWORD (PID))) != 0)// Open process
	{
		CloseHandle(hsnap);
		hsnap=0;
		return true;													// success, return true
	}

	CloseHandle(hsnap);
	hsnap=0;
	return false;														// failed, return false
}

void moduls()
{
	MODULEENTRY32 modul;
	hsnap=CreateToolhelp32Snapshot( TH32CS_SNAPMODULE32, (DWORD)PID);
	if (hsnap==INVALID_HANDLE_VALUE)
	{
		int e=GetLastError(); // **** FEHLER **** 299 ERROR_PARTIAL_COPY 299 0x12B
		std::cout &lt;&lt; &quot;error:&quot; &lt;&lt; e &lt;&lt; std::endl;
		return;
	}
	modul.dwSize=sizeof(MODULEENTRY32);
	if (!Module32First( hsnap, &amp;modul))
	{
		return;
	}
	do
	{
		std::cout &lt;&lt; modul.szModule &lt;&lt; modul.modBaseAddr &lt;&lt; std::endl;

	}
	while ( Module32Next(hsnap, &amp;modul) );
	CloseHandle(hsnap);
}

int main(int argc, char *argv[])
{
	std::cout &lt;&lt; &quot;[PROCESSDUMP]&quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;~~~~~~~~~~~~~&quot; &lt;&lt; std::endl;

	std::cout &lt;&lt; &quot;SetDebugPrivileges() ... &quot;;
	SetDebugPrivileges();
	std::cout &lt;&lt; &quot;done&quot; &lt;&lt; std::endl;

	if (!OpenProc(argv[1]))
	{
		std::cout &lt;&lt; &quot;error: unable to open &quot; &lt;&lt; argv[1] &lt;&lt; std::endl;
		std::cin.get();
		return 0;
	}

	std::cout &lt;&lt; &quot;opened ProcessID: hex 0x&quot; &lt;&lt; PID &lt;&lt; &quot; - decimal &quot; &lt;&lt; (int)PID &lt;&lt; std::endl;

	moduls();

	std::cout &lt;&lt; &quot;Press [RETURN] to quit processpump&quot; &lt;&lt; std::endl;
	std::cin.get();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/225433/createtoolhelp32snapshot-invalid_handle_value-mit-notepad-exe</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 16:55:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/225433.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 23 Oct 2008 11:26:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to CreateToolhelp32Snapshot()==INVALID_HANDLE_VALUE mit notepad.exe on Thu, 23 Oct 2008 11:26:00 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich möchte ein kleines Konsolenprogramm schreiben womit ich einzelne Module in einem Prozess dumpen kann. Mit dumpen meine ich das Modul als xy.bin auf der Festplatte speichern.</p>
<p>Als erstes erstelle ich einen Snapshot aller laufenden Prozesse und lasse alle mit ProzessID auflisten. Dann muss die gewünschte PID eingegeben werden. Jetzt wollte ich eine Liste der Module anzeigen lassen indem ich vom Prozess mit der angegeben ProzessID einen Snapshot mache. Nun wollte ich es mit &quot;notepad.exe&quot; testen und bekomme immer ein invalid handle. Warum?</p>
<p>Notepad habe ich natürlich geöffnet, die PID ist auch korrekt.</p>
<p>Der Fehler liegt in der Funktion moduls(). GetLastError() gibt Fehler 299 &quot;ERROR_PARTIAL_COPY&quot; zurück.<br />
MSDN meint dazu:<br />
&quot;Only part of a ReadProcessMemory or WriteProcessMemory request was completed.&quot;<br />
Damit kann ich leider nichts anfangen!?</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;iostream&gt;
#include &lt;Tlhelp32.h&gt;

LPDWORD		PID=0;
HANDLE		hproc=0;
HANDLE		hsnap=0;

bool SetDebugPrivileges()
{
	HANDLE hToken;
	TOKEN_PRIVILEGES tokenPriv;
	tokenPriv.PrivilegeCount = 1;

	if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &amp;hToken))
	{
		return 0;
	}

	if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &amp;tokenPriv.Privileges[0].Luid))
	{
		return 0;
	}
	tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

	if(!AdjustTokenPrivileges(hToken, false, &amp;tokenPriv, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
	{
		return 0;
	}
	return 1;
}

bool OpenProc(char* targetproc)
{
	hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);				// Create snapshot of all processes and store the handle
	if (hsnap==INVALID_HANDLE_VALUE)
		return false;
	PROCESSENTRY32 process;												// Create process struct
	process.dwSize = sizeof(PROCESSENTRY32);							// Set size
	long nprocess = Process32First(hsnap, &amp;process);					// Store infos for first process
	std::cout &lt;&lt; &quot;PID     Threads Flags   Size    Name&quot; &lt;&lt; std::endl;
	while (nprocess)													// Are there any more processes?
	{
		std::cout &lt;&lt; process.th32ProcessID
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.cntThreads
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.dwFlags
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.dwSize
				  &lt;&lt; &quot;\t&quot; &lt;&lt; process.szExeFile
				  &lt;&lt; std::endl;
		nprocess = Process32Next(hsnap, &amp;process);						// Store infos for next process
	}
	std::cout &lt;&lt; &quot;enter the processID you want to dump: &quot; &lt;&lt; std::endl;
	std::cin.clear();
	char strPID[81];
	std::cin.getline(strPID, 80);
	PID=(LPDWORD)atoi(strPID);

	if ( (hproc = OpenProcess(PROCESS_ALL_ACCESS, 0, DWORD (PID))) != 0)// Open process
	{
		CloseHandle(hsnap);
		hsnap=0;
		return true;													// success, return true
	}

	CloseHandle(hsnap);
	hsnap=0;
	return false;														// failed, return false
}

void moduls()
{
	MODULEENTRY32 modul;
	hsnap=CreateToolhelp32Snapshot( TH32CS_SNAPMODULE32, (DWORD)PID);
	if (hsnap==INVALID_HANDLE_VALUE)
	{
		int e=GetLastError(); // **** FEHLER **** 299 ERROR_PARTIAL_COPY 299 0x12B
		std::cout &lt;&lt; &quot;error:&quot; &lt;&lt; e &lt;&lt; std::endl;
		return;
	}
	modul.dwSize=sizeof(MODULEENTRY32);
	if (!Module32First( hsnap, &amp;modul))
	{
		return;
	}
	do
	{
		std::cout &lt;&lt; modul.szModule &lt;&lt; modul.modBaseAddr &lt;&lt; std::endl;

	}
	while ( Module32Next(hsnap, &amp;modul) );
	CloseHandle(hsnap);
}

int main(int argc, char *argv[])
{
	std::cout &lt;&lt; &quot;[PROCESSDUMP]&quot; &lt;&lt; std::endl;
	std::cout &lt;&lt; &quot;~~~~~~~~~~~~~&quot; &lt;&lt; std::endl;

	std::cout &lt;&lt; &quot;SetDebugPrivileges() ... &quot;;
	SetDebugPrivileges();
	std::cout &lt;&lt; &quot;done&quot; &lt;&lt; std::endl;

	if (!OpenProc(argv[1]))
	{
		std::cout &lt;&lt; &quot;error: unable to open &quot; &lt;&lt; argv[1] &lt;&lt; std::endl;
		std::cin.get();
		return 0;
	}

	std::cout &lt;&lt; &quot;opened ProcessID: hex 0x&quot; &lt;&lt; PID &lt;&lt; &quot; - decimal &quot; &lt;&lt; (int)PID &lt;&lt; std::endl;

	moduls();

	std::cout &lt;&lt; &quot;Press [RETURN] to quit processpump&quot; &lt;&lt; std::endl;
	std::cin.get();
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1603340</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1603340</guid><dc:creator><![CDATA[Spekulatius]]></dc:creator><pubDate>Thu, 23 Oct 2008 11:26:00 GMT</pubDate></item><item><title><![CDATA[Reply to CreateToolhelp32Snapshot()==INVALID_HANDLE_VALUE mit notepad.exe on Thu, 23 Oct 2008 11:44:42 GMT]]></title><description><![CDATA[<p>Ich lese gerade im aktuelleren MSDN online:</p>
<blockquote>
<p>If the specified process is a 64-bit process and the caller is a 32-bit process, this function fails and the last error code is ERROR_PARTIAL_COPY (299).</p>
</blockquote>
<p>Heißt das jetzt es ist unmöglich von einem 32Bit Prozess ein Handle zu einem 64Bit Prozess zu bekommen?</p>
<p>Notepad ist bei mir ein 64Bit Prozess (WinXP 64)<br />
Mein Prozess, also der &quot;callerprocess&quot;, ist ein 32Bit Prozess.</p>
<p>Ich habe nun...<br />
TH32CS_SNAPALL<br />
TH32CS_SNAPMODULE<br />
TH32CS_SNAPMODULE32<br />
probiert und über keine Variante bekomme ich ein valid Handle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1603348</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1603348</guid><dc:creator><![CDATA[Spekulatius]]></dc:creator><pubDate>Thu, 23 Oct 2008 11:44:42 GMT</pubDate></item><item><title><![CDATA[Reply to CreateToolhelp32Snapshot()==INVALID_HANDLE_VALUE mit notepad.exe on Thu, 23 Oct 2008 12:52:16 GMT]]></title><description><![CDATA[<p>Ja!<br />
Genauso wenig wie es möglich ist 32Bit Code zu thunken, so wie das bei 16bit/32bit möglich war.<br />
<a href="http://blogs.msdn.com/oldnewthing/archive/2008/10/20/9006720.aspx" rel="nofollow">http://blogs.msdn.com/oldnewthing/archive/2008/10/20/9006720.aspx</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1603408</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1603408</guid><dc:creator><![CDATA[Martin Richter]]></dc:creator><pubDate>Thu, 23 Oct 2008 12:52:16 GMT</pubDate></item><item><title><![CDATA[Reply to CreateToolhelp32Snapshot()==INVALID_HANDLE_VALUE mit notepad.exe on Thu, 23 Oct 2008 13:20:34 GMT]]></title><description><![CDATA[<p>danke für die schnelle Hilfe <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>Um das zu lösen dachte ich mir jetzt &quot;dann compiliere ich einfach noch eine 64Bit-Version&quot; - aber nixda, VC++ 2008 Express hat nur einen 32Bit Compiler und erst ab der Standard Edition gibts wohl 64Bit. Außerdem frag ich mich ob man einfach so eine Konsolenanwendung überhaupt als 64Bit compilieren kann? ... keine Ahnung, damn it...</p>
<p>Allerdings denke ich schon das es möglich ist, weil wenn ich bei mir &quot;cmd&quot; aufrufe wird der Prozess als 64Bit angezeigt und läuft nicht mehr über den 'WOW64'-Emulator. Möchte ja auch sein bei einem 64Bit OS ^^</p>
<p>Gibt es ein freies VC++ von Microsoft wo ein 64Bit Compiler dabei ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1603427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1603427</guid><dc:creator><![CDATA[Spekulatius]]></dc:creator><pubDate>Thu, 23 Oct 2008 13:20:34 GMT</pubDate></item><item><title><![CDATA[Reply to CreateToolhelp32Snapshot()==INVALID_HANDLE_VALUE mit notepad.exe on Thu, 23 Oct 2008 14:38:07 GMT]]></title><description><![CDATA[<p>Ich weiß es nicht sicher, aber war im SDK nicht ein 64bit Compiler mit drin?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1603468</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1603468</guid><dc:creator><![CDATA[Martin Richter]]></dc:creator><pubDate>Thu, 23 Oct 2008 14:38:07 GMT</pubDate></item><item><title><![CDATA[Reply to CreateToolhelp32Snapshot()==INVALID_HANDLE_VALUE mit notepad.exe on Thu, 23 Oct 2008 17:12:31 GMT]]></title><description><![CDATA[<p>Auf jeden Fall war mal einer dabei... es war mal der Plan den x64 Compiler wieder zu entfernen, da es ja jetzt (ab VS2005) einen Compiler in der IDE gibt... zumindest ist älteren PSDKs in der 64-Bit Compiler dabei.<br />
Im PSDK, welches mit VS installiert wird ist auf jeden Fall keiner dabei...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1603559</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1603559</guid><dc:creator><![CDATA[Jochen Kalmbach]]></dc:creator><pubDate>Thu, 23 Oct 2008 17:12:31 GMT</pubDate></item></channel></rss>