Newsgroup: it.comp.lang.c++
Titolo: ToolHelp32 - 'this' ptr is changing on Module32First( )

Does anybody know what I'm doing wrong here?
After calling Module32First( ), the next function always changes my
*this* ptr?!?!  It can be any function, this code shows memset( ) but
*this* also changes on calls to GetLastError( ) & CloseHandle( ).  It
only happens after calling Module32First( ), if you skip over
Module32First( ) ... it doesn't happen.
Below is a snippet of code which isolates the problem.  The call to
Module32First( ) is near the bottom.
Thanks ... Any help is greatly appreciated.
//==mycode==mycode==mycode==mycode==mycode
//==mycode==mycode==mycode==mycode==mycode
//==mycode==mycode==mycode==mycode==mycode
/********************************************************************\
*  MyDbugr.cpp: Source code for MyDbugr                                *
*                                                                    *
*  Comments: MyDbugr Win32-based Application                         *
*                                                                    *
*  Functions:                                                        *
*     WinMain      - Application entry point                         *
*                                                                    *
*                                                                    *
\********************************************************************/
/*********************  Header Files  *********************/
#include <windows.h>
#include <crtdbg.h>
/*********************  ToolHelp32 structures & defines
***********************/
#define MAX_MODULE_NAME32 255
#define TH32CS_SNAPMODULE   0x00000008
typedef struct tagMODULEENTRY32
{
    DWORD   dwSize;
    DWORD   th32ModuleID;	// This module
    DWORD   th32ProcessID;	// owning process
    DWORD   GlblcntUsage;	// Global usage count on the module
    DWORD   ProccntUsage;	// Module usage count in
th32ProcessID's context
    BYTE  * modBaseAddr;	// Base address of module in
th32ProcessID's context
    DWORD   modBaseSize;	// Size in bytes of module starting at
modBaseAddr
    HMODULE hModule;		// The hModule of this module in
th32ProcessID's context
    char    szModule[MAX_MODULE_NAME32 + 1];
    char    szExePath[MAX_PATH];
} MODULEENTRY32;
typedef MODULEENTRY32 *  PMODULEENTRY32;
typedef MODULEENTRY32 *  LPMODULEENTRY32;
// function prototypes
typedef HANDLE (*PFNCREATETOOLHELP32SNAPSHOT)(	DWORD dwFlags, DWORD
th32ProcessID);
typedef BOOL (*PFNMODULE32FIRST)( HANDLE hSnapshot, LPMODULEENTRY32
lpme);
typedef BOOL (*PFNMODULE32NEXT)( HANDLE hSnapshot, LPMODULEENTRY32
lpme);
/*********************  Classes  ***********************/
class TestClass
{
	public:
		void LoopThroughDlls( DWORD dwProcess);
};
/********************************************************************\
*  Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int)    *
*                                                                    *
\********************************************************************/
int PASCAL WinMain( HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPSTR lpszCmdLine,
					int nCmdShow )
{
	STARTUPINFO			StartupInfo;
	PROCESS_INFORMATION	ProcessInfo;
	DEBUG_EVENT			DebugEvent;
	char szFileName[MAX_PATH];
	BOOL bFinished = FALSE;
	// initialize
	memset( &StartupInfo, NULL, sizeof(StartupInfo));
	StartupInfo.cb          = sizeof( STARTUPINFO );
	StartupInfo.wShowWindow = SW_SHOWDEFAULT;
	memset( &ProcessInfo, NULL, sizeof(ProcessInfo));
	memset( &DebugEvent, NULL, sizeof(DebugEvent));
	memset( szFileName, NULL, MAX_PATH);
	strcpy( szFileName, "d:\\release\\wfa40.exe");
	// start file in new thread
	if( !CreateProcess(	szFileName, NULL, NULL, NULL, FALSE,
						DEBUG_PROCESS |
DEBUG_ONLY_THIS_PROCESS,
						NULL, NULL,
&StartupInfo, &ProcessInfo))
		{
		_ASSERT(FALSE);
		MessageBox( NULL, "Error creating process", "Error",
MB_OK);
		return 0;
		}
	// msg loop
	while( TRUE)
	{
		if( !WaitForDebugEvent( &DebugEvent, INFINITE ) )
		{
			bFinished = TRUE;
			break;
		}
		switch( DebugEvent.dwDebugEventCode )
		{
			case LOAD_DLL_DEBUG_EVENT:
				{
				TestClass tc;
				tc.LoopThroughDlls(
DebugEvent.dwProcessId);
				}
				break;
		}
		//-- default action, just continue
		if( bFinished )
		{
			bFinished = FALSE;	 // reset the value
			break;
		}
		else
			ContinueDebugEvent( DebugEvent.dwProcessId,
DebugEvent.dwThreadId,
				DBG_CONTINUE );
	}
	return 1;
}
/********************************************************************\
*  Function: void TestClass::LoopThroughDlls( DWORD dwProcess)       *
*                                                                    *
\********************************************************************/
void TestClass::LoopThroughDlls( DWORD dwProcess)
{
	HANDLE hModule = GetModuleHandle( "Kernel32.dll");
	PFNCREATETOOLHELP32SNAPSHOT pfnCreateSnapshot = NULL;
	PFNMODULE32FIRST			pfnModule32First = NULL;
	PFNMODULE32NEXT				pfnModule32Next = NULL;
	int nModuleCnt = 0;
	MODULEENTRY32 ModuleInfo;
	if( NULL == hModule)
		{
		const DWORD dwErrNo = GetLastError();
		_ASSERT(FALSE);
		}
	else
		{
		pfnCreateSnapshot = (PFNCREATETOOLHELP32SNAPSHOT)
GetProcAddress( hModule, "CreateToolhelp32Snapshot");
		pfnModule32First = (PFNMODULE32FIRST) GetProcAddress(
hModule, "Module32First");
		pfnModule32Next = (PFNMODULE32NEXT) GetProcAddress(
hModule, "Module32Next");
		}
	// initialize
	ModuleInfo.dwSize = sizeof(MODULEENTRY32);
	// get the snapshot handle
	const HANDLE hSnapShot = pfnCreateSnapshot( TH32CS_SNAPMODULE,
dwProcess);
	if( !pfnModule32First( hSnapShot, &ModuleInfo))
		_ASSERT(FALSE); // should this ever happen?
	else
		{
		//*******
		//******* This is where the 'this' ptr changes.
		//******* It doesn't have to be memset, it's also failed
		//******* with CloseHandle() & GetLastError().
		//******* But it is the pfnModule32First() call which is
		//******* throwing it off, if you skip over
pfnModule32First()
		//******* everything is fine.
		//*******
		memset( &ModuleInfo, NULL, sizeof(ModuleInfo));
		//*******
		//*******
		//*******
		//	.
		//	.
		//	.
		}
	// close snapshot
	CloseHandle( hSnapShot);
}
Sent via Deja.com 
http://www.deja.com/
Before you buy.

Hi,
You may not be aware that you posted your message on the italian C++
newsgroup (the "it" in the group's name stands for "Italy").
You may have better luck with one of the following:
comp.lang.c++
comp.lang.c++.moderated
Also, this group is devoted to standard C++ and not platform specific
problems, so you don't have a big chance of getting an answer even if
you write in italian! :-)
Best regards,
Nicola Musatti