| View previous topic :: View next topic |
| Author |
Message |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Mon Jun 09, 2008 9:38 pm Post subject: [C++] OpenProcess function w/ process name? |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Jun 09, 2008 10:53 pm Post subject: |
|
|
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 |
|
 |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Tue Jun 10, 2008 8:47 am Post subject: |
|
|
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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Tue Jun 10, 2008 1:22 pm Post subject: |
|
|
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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue Jun 10, 2008 2:59 pm Post subject: |
|
|
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 |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Tue Jun 10, 2008 3:27 pm Post subject: |
|
|
| It's very strange that PROCESS_ALL_ACCESS doesn't work. It's always worked for me.
|
|
| Back to top |
|
 |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Tue Jun 10, 2008 5:20 pm Post subject: |
|
|
| 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 |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
|
| Back to top |
|
 |
Cx Master Cheater
Reputation: 0
Joined: 27 Jul 2007 Posts: 367
|
Posted: Wed Jun 11, 2008 6:08 pm Post subject: |
|
|
Told ya Irwin.
_________________
armed with this small butterfly net
i will face the world alone
& never be lonely. |
|
| Back to top |
|
 |
|