Why does PROCESSENTRY32::szExeFile only contain a single character?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Why does PROCESSENTRY32::szExeFile only contain a single character?

  1. #1
    Join Date
    May 2017
    Posts
    7

    Question Why does PROCESSENTRY32::szExeFile only contain a single character?

    I'm having a problem where I'm attempting to go trough all open processes and see if any of them are equal to the name of a specified process.
    The way I'm trying to achieve this is by doing this :

    Code:
    DWORD GetProcessIdByName(char* processName)
    {
        PROCESSENTRY32 pe32;
        pe32.dwSize = sizeof(PROCESSENTRY32);
    
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
        if (hSnapshot == NULL)
        {
            error_string = "Failed to get list of processes";
            std::cout << "Failed to get list of processes." << std::endl;
            return 0;
        }
    
        if (!Process32First(hSnapshot, &pe32))
        {
            error_string = "Failed to get first element in process list";
            std::cout << "Failed to get first element in process list." << std::endl;
            CloseHandle(hSnapshot);
            return 0;
        }
    
        while (Process32Next(hSnapshot, &pe32) == TRUE)
        {
            std::cout << "Checking Process : " << (char*)pe32.szExeFile << " against : " << processName << std::endl;
    
            if (strcmp((char*)pe32.szExeFile, processName) == 0)
            {
                std::cout << "Comparsion Successfull!" << std::endl;
                CloseHandle(hSnapshot);
                return pe32.th32ProcessID;
            }
        }
    
        CloseHandle(hSnapshot);
        error_string = "No process with the name '";
        error_string += processName;
        error_string += "' could be found";
        std::cout << "No process with the name '" << processName << "' could be found." << std::endl;
        return 0;
    }
    However I ran into a problem where the strcmp line would never be able to find the process specified in the function argument (processName), even though I'm 100% certain that it should, so when checking the actual value of the
    Code:
    PROCESSENTRY32::szExeFile
    I noticed that it only held a single character, or at least when I tried to
    Code:
    std::cout
    it, only one single character was printed to console.

    And I can't seem to figure out why, I'm thinking I'm doing some sort of conversion error maybe? But according to other sources it seems like I'm doing everything correctly?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,325

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    You're not using Process32First() and Process32Next() correctly. You need to process the result of Process32First() before calling Process32Next(). See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx for an example.

    Are you compiling as ASCII or Unicode?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  3. #3
    Join Date
    May 2017
    Posts
    7

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    I assume by "You need to process the result of Process32First() before calling Process32Next()" you mean that I need to use OpenProcess() on it?
    I think I'm compiling as ASCII.

  4. #4
    Join Date
    May 2017
    Posts
    7

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    Actually, in the example you gave me they don't do that after calling Process32First(), so I don't think I understand what you mean :/

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,325

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    No. The while loop is turned into a do loop so that the result of Process32First() is processed then Process32Next() is called.
    Code:
       do {
            std::cout << "Checking Process : " << (char*)pe32.szExeFile << " against : " << processName << std::endl;
    
            if (strcmp((char*)pe32.szExeFile, processName) == 0)
            {
                std::cout << "Comparsion Successfull!" << std::endl;
                CloseHandle(hSnapshot);
                return pe32.th32ProcessID;
            }
        }  while (Process32Next(hSnapshot, &pe32) == TRUE)
    You shouldn't need to cast to char*
    Code:
    if (strcmp((char*)pe32.szExeFile, processName) == 0)
    as the type of pe32.szExeFile is an array of TCHAR which as ASCII becomes an array of char which depreciates to char*.

    For my system, this works
    Code:
    DWORD GetProcessIdByName(char* processName)
    {
    	PROCESSENTRY32 pe32;
    	pe32.dwSize = sizeof(PROCESSENTRY32);
    
    	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    	if (hSnapshot == NULL)
    	{
    		std::cout << "Failed to get list of processes." << std::endl;
    		return 0;
    	}
    
    	if (!Process32First(hSnapshot, &pe32))
    	{
    		std::cout << "Failed to get first element in process list." << std::endl;
    		CloseHandle(hSnapshot);
    		return 0;
    	}
    
    	do {
    		std::cout << "Checking Process : " << pe32.szExeFile << " against : " << processName << std::endl;
    
    
    		if (strcmp(pe32.szExeFile, processName) == 0)
    		{
    			std::cout << "Comparsion Successfull!" << std::endl;
    			CloseHandle(hSnapshot);
    			return pe32.th32ProcessID;
    		}
    	} while (Process32Next(hSnapshot, &pe32) == TRUE);
    
    
    	CloseHandle(hSnapshot);
    	std::cout << "No process with the name '" << processName << "' could be found." << std::endl;
    	return 0;
    }
    
    ...
    GetProcessIdByName("lsm.exe");
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  6. #6
    Join Date
    May 2017
    Posts
    7

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    It's still only printing out a single character on console for me, and using strcmp without casting szExeFile into a char* does not work for me, I assume that's because I'm compiling in Unicode?
    (I have no idea how to check that in my current IDE...)

    Here's my console output :

    Checking Process : [ against : hl2.exe
    Checking Process : S against : hl2.exe
    ...
    ... (It continues for about 100 more lines in between here, not gonna post it all :P)
    ...
    Checking Process : S against : chrome.exe
    Checking Process : S against : chrome.exe
    Checking Process : m against : chrome.exe
    Checking Process : v against : chrome.exe
    Checking Process : D against : chrome.exe
    No process with the name 'chrome.exe' could be found.
    Now that I take a second look though, it makes no sense for any process to have a '[' in it's name at all? At least, I don't know any processes that have that in the name...
    Last edited by The Shroom; May 17th, 2017 at 06:34 AM.

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,325

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    It looks like you are compiling as unicode. To check, Project/Properties/General - then what is shown for Character Set?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  8. #8
    Join Date
    May 2017
    Posts
    7

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    I think it's in Unicode, however I'm not sure. In my IDE there seems to be no place to check, but according to google the IDE I'm using should have Unicode set as default.
    If it is in Unicode, what should I be doing to make it work? Do I need to convert the szExeFile variable somehow?

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,325

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    For unicode, TCHAR is defined as WCHAR which is defined as wchar_t. This is based upon whether the symbol UNICODE is defined or not. If you don't need to use unicode, there should be an option to use ASCII (or multi-byte) character set. Failing that if you are using ASCII, then try
    Code:
    #undef UNICODE
    before the #include <windows.h>

    What IDE are you using?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  10. #10
    Join Date
    May 2017
    Posts
    7

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    When checking what TCHAR was defined as it lead me to this code in winnt.h :

    Code:
    //
    // Neutral ANSI/UNICODE types and macros
    //
    #ifdef  UNICODE                     // r_winnt
    
    #ifndef _TCHAR_DEFINED
    typedef WCHAR TCHAR, *PTCHAR;
    typedef WCHAR TBYTE , *PTBYTE ;
    #define _TCHAR_DEFINED
    #endif /* !_TCHAR_DEFINED */
    I'm using the IDE Qt Creator with the MSVC 2015 compiler.

    I tried undefining UNICODE before including the windows API, which solved the issue.
    After some more googling on how I can change the character set in Qt I figured it out, however I'm not sure which one I should select :
    https://i.gyazo.com/aa2567b3a602713e...03f2aaf98d.mp4
    Last edited by The Shroom; May 17th, 2017 at 03:22 PM.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,505

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    Quote Originally Posted by The Shroom View Post
    I think it's in Unicode, however I'm not sure. In my IDE there seems to be no place to check, but according to google the IDE I'm using should have Unicode set as default.
    If it is in Unicode, what should I be doing to make it work? Do I need to convert the szExeFile variable somehow?
    What IDE are you using? At any rate, the problem is your std:ut calls are ANSI, so if you compile for UNICODE, you'll see only on character. In your test program, use std::wcout for UNICODE. If you are using Visual Studio, put string literals inside L"" rather than just "".

  12. #12
    Join Date
    May 2017
    Posts
    7

    Re: Why does PROCESSENTRY32::szExeFile only contain a single character?

    Ah I see, thank very much. I think I'll have to research on this encoding thing some more later :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center