7 Replies - 2551 Views - Last Post: 12 August 2012 - 12:01 PM

#1 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Get process information

Posted 20 January 2011 - 04:42 PM

Description: Compiled successfully under G++ and Visual C++ 2010.Retrieves information about a given process.
#include <cstdio>
#include <Windows.h>
#include <tlhelp32.h>

/* Returns a pointer to a PROCESSENTRY32 structure given the process name */
PROCESSENTRY32 *GetProcessInfo(char *szExeFile)
{
	PROCESSENTRY32  *pe32 = new PROCESSENTRY32 [sizeof(PROCESSENTRY32)];
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if(hSnapshot == INVALID_HANDLE_VALUE)
		return NULL;

    if(!Process32First(hSnapshot, pe32)) 
	{
        CloseHandle(hSnapshot);
        return NULL;
    }

    while(Process32Next( hSnapshot, pe32 ))
	{
		if( strcmp(szExeFile, pe32->szExeFile) == 0) 
		{
			CloseHandle(hSnapshot);
			return pe32;
		}
    } 

	return NULL;
}

int main()
{
	PROCESSENTRY32 *pe32 = GetProcessInfo("explorer.exe");
	
	/* Print some info about this process */
    printf("  =====================================================n");
    printf("  Process name: %sn", pe32->szExeFile);
    printf("  =====================================================n");

    printf("  Process ID        = %dn", pe32->th32ProcessID);
    printf("  Thread count      = %dn",   pe32->cntThreads);
    printf("  Parent process ID = %dn", pe32->th32ParentProcessID);
    printf("  Priority base     = %dn", pe32->pcPriClassBase);

	delete [] pe32;
	pe32 = NULL;
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Get process information

#2 neithan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 12-August 12

Re: Get process information

Posted 12 August 2012 - 10:24 AM

Tried to compile this in Visual Studio 2012 RC. Couldn't make it, in line 22 it says that WCHAR* and const char* are not compatible.
Was This Post Helpful? 0
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Get process information

Posted 12 August 2012 - 10:40 AM

Change your VC++ project's settings to use the Multi-Byte Character set.
Was This Post Helpful? 0
  • +
  • -

#4 neithan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 12-August 12

Re: Get process information

Posted 12 August 2012 - 10:41 AM

I tried to convert it to char* with this: while(Process32Next( hSnapshot, pe32 )) { wchar_t *orig = pe32->szExeFile; size_t origsize = wcslen(orig) + 1; const size_t newsize = 260; size_t convertedChars = 0; char nstring[newsize]; wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE); if( strcmp(szExeFile, nstring) == 0) { CloseHandle(hSnapshot); return pe32; } Now no syntax error but the code still crashes.
Was This Post Helpful? 0
  • +
  • -

#5 neithan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 12-August 12

Re: Get process information

Posted 12 August 2012 - 10:43 AM

Sorry I didn't see your comment. You're right, now it works! Just one thing more i didn't know, thanks :)
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Get process information

Posted 12 August 2012 - 10:43 AM

No need for a conversion really, you can just set the character set to Multi-Byte in your project properties.
Was This Post Helpful? 0
  • +
  • -

#7 neithan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 12-August 12

Re: Get process information

Posted 12 August 2012 - 10:51 AM

BTW, how was I supposed to know that? Not complaining about your code but about the debugger, which told me basically nothing.
Was This Post Helpful? 0
  • +
  • -

#8 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Get process information

Posted 12 August 2012 - 12:01 PM

I can see your point. It would be helpful if the compiler gave a more clear warning when incompatible character sets were being used.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1