Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]Get PID from process name or window name?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hyphen
Advanced Cheater
Reputation: 0

Joined: 12 Sep 2008
Posts: 84
Location: Not Having Fun

PostPosted: Thu Jun 18, 2009 9:40 pm    Post subject: [C++]Get PID from process name or window name? Reply with quote

Hello there I'm wondering if anybody could explain to me how to obtain a process's ProcessID (as in the four digit number you can see in task manager) using the process's name (ex: winmine.exe), the window name (ex:Minesweeper), or some other method that does not require the process's handle as input.
The reason I need the PID is so I can use OpenProcess with it to get hProcess for use in WriteProcessMemory.

Code:
HANDLE WINAPI hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 2999/*PID*/);


Keep in mind that I'm new to programming..
Any help/tips are appreciated~
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Thu Jun 18, 2009 10:21 pm    Post subject: Reply with quote

Code:
DWORD dwPID = NULL;
HANDLE hProc = INVALID_HANDLE_VALUE;
GetWindowThreadProcessId(FindWindow(NULL,TEXT("Minesweeper")), &dwPID);
hProc = OpenProcess(PROCESS_VM_OPERATION, false, dwPID);


Don't forget, once you're done:

Code:
CloseHandle(hProc);


Last edited by &Vage on Thu Jun 18, 2009 10:24 pm; edited 2 times in total
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Thu Jun 18, 2009 10:23 pm    Post subject: Reply with quote

http://forum.cheatengine.org/viewtopic.php?t=282653
_________________
Back to top
View user's profile Send private message
hyphen
Advanced Cheater
Reputation: 0

Joined: 12 Sep 2008
Posts: 84
Location: Not Having Fun

PostPosted: Fri Jun 19, 2009 3:22 pm    Post subject: Reply with quote

; wrote:
Code:
DWORD dwPID = NULL;
HANDLE hProc = INVALID_HANDLE_VALUE;
GetWindowThreadProcessId(FindWindow(NULL,TEXT("Minesweeper")), &dwPID);
hProc = OpenProcess(PROCESS_VM_OPERATION, false, dwPID);


Don't forget, once you're done:

Code:
CloseHandle(hProc);


It worked, thank you very much.
Code:

// Yay it works! Thanks a bunch!

#include "stdafx.h"
#include <windows.h>

typedef unsigned long ULONG;

DWORD ThreadID;
//ULONG *canClick   = (ULONG*)0x01005000;

//used variables/pointers
ULONG *timer      = (ULONG*)0x0100579C;
BYTE theArray[2]  = {0xAA,0x01};

//ULONG *smiley   = (ULONG*)0x01005160;
//ULONG *mines      = (ULONG*)0x0100564A;
//BYTE  *myArray    = (BYTE*)theArray[2];
//ULONG *theScript  =(ULONG*)0x01002FF5;

BOOL obtainedHandle = FALSE;
HANDLE hProcess     = INVALID_HANDLE_VALUE;
DWORD PID           = NULL;


DWORD WINAPI setTimer(LPVOID lParam) {
      while(1)
     {
if (obtainedHandle == FALSE)
{
        GetWindowThreadProcessId(FindWindow(NULL,TEXT("Minesweeper")), &PID);
        HANDLE WINAPI hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
        obtainedHandle = TRUE;
}

      if(GetAsyncKeyState(VK_NUMPAD9))
     {
       WriteProcessMemory(hProcess,(LPVOID*)(DWORD)0x0100579C,theArray,2,NULL);
     }
   
     }
      ExitThread(0);
}

BOOL APIENTRY DllMain(HINSTANCE hDll, DWORD callReason, LPVOID lpReserved) {
     if(callReason == DLL_PROCESS_ATTACH) {
                   MessageBox(0, "Argh this is annoying... ", "HinjectOR", MB_ICONEXCLAMATION | MB_OK);
               CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&setTimer, 0, 0, &ThreadID);
     }
   
     return 1;
}


Erm..... closing the handle. That's not important, is it? Shocked Is the handle occupying space on my heap or w/e??
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Jun 19, 2009 3:51 pm    Post subject: Reply with quote

yes closing handles is very important else especially if you have something in a function which is called a lot of times. getting a handle requests and allocates more memory. if you don't close it then that memory is not given back to the system and this is known as a memory leak.

at the end of the process the system does clean up unclosed handles but it is good practice to do this yourself

some comments on your code :
- getting threadid is not necessary since you don't use it. in fact your exitthread call is not needed. returning from a thread will close it properly
- you might wanna put freelibraryandexitthread using hDll as the library handle for when you're done. you can just copy hDll to a global variable
- some things like ( obtainedHandle == False ) can be shortened to ( !obtainedHandle ) which i think is more readable but that is a choice of preference
- there is no need for the HANDLE WINAPI before you assign hProcess
- if an IF condition has only one line inside it the braces can be removed but that is also preference

eg.
Code:
if ( GetAsyncKeyState( VK_NUMPAD9 ) )
{
  WPM......
}


can be changed to :

Code:
if ( GetAsyncKeyState( VK_NUMPAD9 ) )
  WPM......


- your method of waiting for the window to appear is what is known as a tight loop and will rape your CPU. instead of having that boolean you could do this

Code:
HWND hWnd = NULL;
while( !hWnd )
{
  Sleep(100);
  hWnd = FindWindow.....
}

GetWindowThreadProcessId.....
hProcess = OpenProcess.....


........

CloseHandle(hProcess);


you also have a bunch of unneeded global variables and i have no clue what you're doing with the 'while(1)'
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Fri Jun 19, 2009 8:39 pm    Post subject: Reply with quote

Code:
   HWND hWnd = NULL;
   DWORD dwProcID = NULL;
   HANDLE hProc = INVALID_HANDLE_VALUE;

   for(;;Sleep(100))
      if((hWnd = FindWindow(NULL, TEXT("Minesweeper"))) != NULL)
         break;

   GetWindowThreadProcessId(hWnd, &dwProcID);
   if(dwProcID != NULL)
      if((hProc = OpenProcess(PROCESS_VM_OPERATION, false, dwProcID)) != INVALID_HANDLE_VALUE)
         CloseHandle(hProc);
      else
         CloseHandle(hProc);


You get the point.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Jun 19, 2009 8:42 pm    Post subject: Reply with quote

; wrote:
Code:
   HWND hWnd = NULL;
   DWORD dwProcID = NULL;
   HANDLE hProc = INVALID_HANDLE_VALUE;

   for(;;Sleep(100))
      if((hWnd = FindWindow(NULL, TEXT("Minesweeper"))) != NULL)
         break;

   GetWindowThreadProcessId(hWnd, &dwProcID);
   if(dwProcID != NULL)
      if((hProc = OpenProcess(PROCESS_VM_OPERATION, false, dwProcID)) != INVALID_HANDLE_VALUE)
         CloseHandle(hProc);
      else
         CloseHandle(hProc);


You get the point.

superfluous else condition. if the handle is INVALID_HANDLE_VALUE why would you want to close it.. kthx
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Fri Jun 19, 2009 8:44 pm    Post subject: Reply with quote

Slugsnack wrote:
; wrote:
Code:
   HWND hWnd = NULL;
   DWORD dwProcID = NULL;
   HANDLE hProc = INVALID_HANDLE_VALUE;

   for(;;Sleep(100))
      if((hWnd = FindWindow(NULL, TEXT("Minesweeper"))) != NULL)
         break;

   GetWindowThreadProcessId(hWnd, &dwProcID);
   if(dwProcID != NULL)
      if((hProc = OpenProcess(PROCESS_VM_OPERATION, false, dwProcID)) != INVALID_HANDLE_VALUE)
         CloseHandle(hProc);
      else
         CloseHandle(hProc);


You get the point.

superfluous else condition. if the handle is INVALID_HANDLE_VALUE why would you want to close it.. kthx
Maybe because I want too?
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Jun 19, 2009 8:46 pm    Post subject: Reply with quote





Rolling Eyes
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Fri Jun 19, 2009 8:51 pm    Post subject: Reply with quote

What, I can't take extra precautions now? What are you anti - precaution hitler?
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Jun 19, 2009 9:01 pm    Post subject: Reply with quote

it's not a precaution you retard.. read the code that you claim you wrote and you'll realise that if execution ever gets to that point then the handle can not be open. you did the equivalent of going inside a greenhouse and putting up an umbrella to protect yourself against rain as 'a precaution'

maybe instead of hacking and mutilating other people's shit together you could take the time to actually learn programming.. seriously, does your idiocy have no limit ?
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Fri Jun 19, 2009 9:46 pm    Post subject: Reply with quote

; wrote:
What, I can't take extra precautions now? What are you anti - precaution hitler?


retarddd stop flooding the General Programming section with bullshit.

-1 as a HANDLE is just a HANDLE to the current process.

Yea, let's try CloseHandle on the process we're currently in lmfao. smart kid right here
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Fri Jun 19, 2009 10:06 pm    Post subject: Reply with quote

smartz993 wrote:
; wrote:
What, I can't take extra precautions now? What are you anti - precaution hitler?


retarddd stop flooding the General Programming section with bullshit.

-1 as a HANDLE is just a HANDLE to the current process.

Yea, let's try CloseHandle on the process we're currently in lmfao. smart kid right here
I'm sorry weren't you the kid who took Timbus's packet editor and attached a account logger than released it as your own?
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Fri Jun 19, 2009 10:17 pm    Post subject: Reply with quote

; wrote:
smartz993 wrote:
; wrote:
What, I can't take extra precautions now? What are you anti - precaution hitler?


retarddd stop flooding the General Programming section with bullshit.

-1 as a HANDLE is just a HANDLE to the current process.

Yea, let's try CloseHandle on the process we're currently in lmfao. smart kid right here
I'm sorry weren't you the kid who took Timbus's packet editor and attached a account logger than released it as your own?


lmfao aren't you the nig he claimed suspending GG's process would never result in stopping the CRC from functioning ;[
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Fri Jun 19, 2009 11:00 pm    Post subject: Reply with quote

smartz993 wrote:
; wrote:
smartz993 wrote:
; wrote:
What, I can't take extra precautions now? What are you anti - precaution hitler?


retarddd stop flooding the General Programming section with bullshit.

-1 as a HANDLE is just a HANDLE to the current process.

Yea, let's try CloseHandle on the process we're currently in lmfao. smart kid right here
I'm sorry weren't you the kid who took Timbus's packet editor and attached a account logger than released it as your own?


lmfao aren't you the nig he claimed suspending GG's process would never result in stopping the CRC from functioning ;[
Sounds reasonable? Thought GG would check it.

Aren't you the one who thought MS had a HWND for it's chatbox? ;[
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites