Process not Outputted?

It appear that for some reason, my code won't output the process information.
On the error log, there is no error message. I had to put this here due to the fact that no one on the beginner's section could solve it. Could someone please tell me the problem?

#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
using namespace std;


char File[] = "BatteryLife.exe";
PROCESSENTRY32 pe32;
HANDLE hProcess;
HANDLE hProcess1;
LPVOID FindParent(DWORD process) {
if (Process32First(hProcess1, &pe32)) {
do{
}while(Process32Next(hProcess1, &pe32));
if (pe32.th32ProcessID==process) {
return pe32.szExeFile;
}
}}
int main() {

hProcess1 = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (Process32First(hProcess1, &pe32)) {
do{
}while(Process32Next(hProcess1, &pe32));
if (strcmp(pe32.szExeFile, File)) {
hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID);
cout << pe32.szExeFile << ": " << pe32.th32ProcessID << "\n" << endl << " Parent Process: " << FindParent(pe32.th32ParentProcessID);
}}
cin.get();
return 0;
}
closed account (z05DSL3A)
Looks like you need more error checking to tell you what is going on and it looks like you have a bit of code out of order.

Try something like this (it is in C but...)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#define WIN32_LEAN_AND_MEAN 
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

#include <tlhelp32.h>

void PrintError(TCHAR* msg);

int main() 
{
    HANDLE hProcessSnap;
    HANDLE hProcess;
    PROCESSENTRY32 pe32;
    DWORD dwPriorityClass;
    
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE)
    {
        PrintError(TEXT("CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS)"));
        return(1);
    }

    pe32.dwSize = sizeof(PROCESSENTRY32);

    if (!Process32First(hProcessSnap, &pe32))
    {
        PrintError(TEXT("Process32First"));
        CloseHandle(hProcessSnap);
        return(1);
    }

    do
    {
        if (_tcscmp(pe32.szExeFile, TEXT("devenv.exe")) == 0)
        {
            _tprintf(TEXT("\n\n======================================"));
            _tprintf(TEXT("\nPROCESS NAME: %s"), pe32.szExeFile);

            hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
            if (hProcess == NULL)
            {
                PrintError(TEXT("OpenProcess"));
            }
            else
            {
                dwPriorityClass = GetPriorityClass(hProcess);
                if (!dwPriorityClass)
                {
                    PrintError(TEXT("GetPriorityClass"));
                }
                CloseHandle(hProcess);
            }
            _tprintf(TEXT("\n  Process ID        = 0x%08X"), pe32.th32ProcessID);
            _tprintf(TEXT("\n  Parent process ID = 0x%08X"), pe32.th32ParentProcessID);
            _tprintf(TEXT("\n--------------------------------------\n"));
        }
    } while (Process32Next(hProcessSnap, &pe32));
    CloseHandle(hProcessSnap);
  
    return 0;
}

void PrintError(TCHAR* msg)
{
    DWORD errorNumber;
    TCHAR sysMsg[256];
    TCHAR* ptr;

    errorNumber = GetLastError();

    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, errorNumber,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        sysMsg, 256, NULL);

    ptr = sysMsg;
    while ((*ptr > 31) || (*ptr == 9))
        ++ptr;
    do
    {
        *ptr-- = 0;
    } while ((ptr >= sysMsg) && ((*ptr == '.' )|| (*ptr < 33)));

    _tprintf(TEXT("\n WARNING: %s failed with error %d (%s)"), msg, errorNumber, sysMsg);
}
C code
Not a bad idea, I'll try it.
Topic archived. No new replies allowed.