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++] OpenProcess function w/ process name?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Cx
Master Cheater
Reputation: 0

Joined: 27 Jul 2007
Posts: 367

PostPosted: Mon Jun 09, 2008 9:38 pm    Post subject: [C++] OpenProcess function w/ process name? Reply with quote

I tried coding a function for OpenProcess that takes the process name, but it doesn't work.
Any help here?

Code:
HANDLE OpenProcessByName(TCHAR * szProcessName)
{
   DWORD access =   PROCESS_TERMINATE | PROCESS_CREATE_THREAD | PROCESS_SET_SESSIONID |
               PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_DUP_HANDLE |
               PROCESS_CREATE_PROCESS | PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
               PROCESS_QUERY_INFORMATION | PROCESS_SUSPEND_RESUME | PROCESS_VM_READ |
               SYNCHRONIZE;
   DWORD pID;
   HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
   PROCESSENTRY32 pe32;
   pe32.dwSize = sizeof(PROCESSENTRY32);
   Process32First(hSnapshot, &pe32);
   do {
      if (wcscmp(pe32.szExeFile, szProcessName) == 0)
      {
         pID = pe32.th32ProcessID;
         CloseHandle(hSnapshot);
         return OpenProcess(access, FALSE, pID);
      }
   } while (Process32Next(hSnapshot, &pe32));
   CloseHandle(hSnapshot);
   return NULL;
}

_________________

armed with this small butterfly net
i will face the world alone
& never be lonely.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon Jun 09, 2008 10:53 pm    Post subject: Reply with quote

Probably has to do with your access. Along with that, if you are using TCHAR, you should make sure of the TCHAR functions when comparing strings and such. Heres a redone function I wrote to do what you want:

Code:
HANDLE _OpenProcess( TCHAR* tszProcName )
{
   // Add access as needed... This should be enough for the basics..
   DWORD         dwAccess   = PROCESS_QUERY_INFORMATION|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE;
   HANDLE         hHandle      = NULL;
   PROCESSENTRY32   pe32      = {0};
   
   hHandle = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
   if( hHandle == INVALID_HANDLE_VALUE || hHandle == NULL )
      return INVALID_HANDLE_VALUE;
   
   pe32.dwSize = sizeof( PROCESSENTRY32 );
   if( !Process32First( hHandle, &pe32 ) )
   {
      CloseHandle( hHandle );
      return INVALID_HANDLE_VALUE;
   }

   while( Process32Next( hHandle, &pe32 ) )
   {
      if( _tcsncicmp( tszProcName, pe32.szExeFile, _tcslen( tszProcName ) ) == 0 )
      {
         CloseHandle( hHandle );
         return OpenProcess( dwAccess, FALSE, pe32.th32ProcessID );
      }
   }
   CloseHandle( hHandle );
   return INVALID_HANDLE_VALUE;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Cx
Master Cheater
Reputation: 0

Joined: 27 Jul 2007
Posts: 367

PostPosted: Tue Jun 10, 2008 8:47 am    Post subject: Reply with quote

For some reason, Wiccaan's works but yours, Irwin, doesn't. Haven't checked why yet though, I have to leave.
_________________

armed with this small butterfly net
i will face the world alone
& never be lonely.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Jun 10, 2008 1:22 pm    Post subject: Reply with quote

His fails because of using PROCESS_ALL_ACCESS without adjusting the debug token. You can adjust that using:

Code:
BOOL EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable)
{
    HANDLE              hToken;
    TOKEN_PRIVILEGES    tp;
    LUID                luid;
    BOOL                ret;

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY | TOKEN_READ, &hToken))
        return FALSE;

    if (!LookupPrivilegeValue(NULL, lpszPrivilegeName, &luid))
        return FALSE;

    tp.PrivilegeCount           = 1;
    tp.Privileges[0].Luid       = luid;
    tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;

    ret = AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
    CloseHandle(hToken);

    return ret;
}


Just call it before you attempt to open the process handle using:

Code:
EnablePrivilege(SE_DEBUG_NAME, TRUE);


And you can reset it when you are done using FALSE as the second param.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue Jun 10, 2008 2:59 pm    Post subject: Reply with quote

Lol. I can't believe I actually knew that was why it was messing up. Btw, is every program like that (where you have to change debug tokens for PROCESS_ALL_ACCESS to work)? And btw, what is a token/token privilege, and a LUID?
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Tue Jun 10, 2008 3:27 pm    Post subject: Reply with quote

It's very strange that PROCESS_ALL_ACCESS doesn't work. It's always worked for me.
Back to top
View user's profile Send private message MSN Messenger
Cx
Master Cheater
Reputation: 0

Joined: 27 Jul 2007
Posts: 367

PostPosted: Tue Jun 10, 2008 5:20 pm    Post subject: Reply with quote

noz3001 wrote:
It's very strange that PROCESS_ALL_ACCESS doesn't work. It's always worked for me.

Never has worked for me.

_________________

armed with this small butterfly net
i will face the world alone
& never be lonely.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Jun 11, 2008 12:18 am    Post subject: Reply with quote

http://nsylvain.blogspot.com/2008/01/winverwin32winnt-mayhem.html

That covers why PROCESS_ALL_ACCESS fails with Visual Studio 2008.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Cx
Master Cheater
Reputation: 0

Joined: 27 Jul 2007
Posts: 367

PostPosted: Wed Jun 11, 2008 6:08 pm    Post subject: Reply with quote

Told ya Irwin.
_________________

armed with this small butterfly net
i will face the world alone
& never be lonely.
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
Page 1 of 1

 
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