Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateToolhelp32Snapshot fails under Win2000

Status
Not open for further replies.

UdoScheuvens

Programmer
Apr 12, 2001
42
DE
I have a problem with a function that works good under Windows 95/98. The documentation says that it also should work under Windows 2000, but when I try I get errors. First of all the code:

Code:
long lErr;
HANDLE lSnapshot1, lSnapshot2;
char sBuffer[200];
PROCESSENTRY32 ulpPE32;
BOOL bResult;

lSnapshot1 = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

ulpPE32.dwSize = sizeof(ulpPE32);
bResult = Process32First(lSnapshot1, &ulpPE32);
lErr = GetLastError();
if (lErr == ERROR_NO_MORE_FILES)
{
	MessageBox (NULL, "No processes!", NULL, MB_OK);
	return;
}


while (bResult == TRUE)
{
	lSnapshot2 = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ulpPE32.th32ProcessID);
	if (lSnapshot2 == (HANDLE)-1)
	{
		lErr = GetLastError();
		wsprintf (sBuffer, &quot;Failed for >%s< %ld:  %ld&quot;, ulpPE32.szExeFile, ulpPE32.th32ProcessID, lErr);
		MessageBox (NULL, sBuffer, NULL, MB_OK);
	}
	else
	{
		wsprintf (sBuffer, &quot;Worked for >%s< %ld&quot;, ulpPE32.szExeFile, ulpPE32.th32ProcessID);
		MessageBox (NULL, sBuffer, NULL, MB_OK);
		CloseHandle(lSnapshot2);
	}

    bResult = Process32Next(lSnapshot1, &ulpPE32);
    lErr = GetLastError();
    if (lErr == ERROR_NO_MORE_FILES)
	{
		MessageBox (NULL, &quot;That's it!&quot;, NULL, MB_OK);
		bResult = FALSE;
	}
}

Now the results:
Under Windows 95 this code works fine. All processes give the message &quot;Worked...&quot;. The ID is 6 or 7 digit negative number.

Under Windows 2000 the first call also works fine. But within the &quot;while&quot;-loop all calls fail (message &quot;Failed...)
The error code returned by GetLastError is 5 (ERROR_ACCESS_DENIED). The ID is a 3 digit positive number.

What has to be done to make this code work under Windows 2000?

Udo
 
Hi Udo

Sorry for the late reply. Getting process information from a Win9x machine and an NT/2000 machine is completely different, making it a real pain (especially if you're not sure what the target machine's running).

See &quot;HOWTO: Enumerate Applications in Win32&quot; in MSDN. There's an excellent article plus a complete code listing showing the differences between obtaining process information under 95/98 and NT/2000. What you might have trouble finding is the psapi.h header file (because I certainly did!). If you can't find it, email me and I'll send you a copy. I know the psapi.dll wasn't standard on NT (it shipped with one of the service packs) but I assume it comes with 2000 as standard, although I always install the latest version with my stuff just to be sure.

Phil
(phil.munro@shlgroup.com)
 
Try to log on win2000 as administrator. This will give u the right to access the process info.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top