| View previous topic :: View next topic |
| Author |
Message |
jeremy6996 Expert Cheater
Reputation: 0
Joined: 20 May 2007 Posts: 100
|
Posted: Sun Sep 30, 2007 1:57 pm Post subject: C++ Getting Process ID |
|
|
| I was curious does anyone know any tutorials on how to aquire a processes ID using its name.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Sep 30, 2007 2:00 pm Post subject: Re: C++ Getting Process ID |
|
|
| jeremy6996 wrote: | | I was curious does anyone know any tutorials on how to aquire a processes ID using its name. |
My function for that (in C):
| Code: | DWORD GetProcessID(char* strProcessName)
{
PROCESSENTRY32 pe32;
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
return FALSE;
else
{
pe32.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hProcessSnap, &pe32))
{
CloseHandle(hProcessSnap);
return FALSE;
}
else
{
do
{
if (!stricmp(pe32.szExeFile, strProcessName))
{
CloseHandle(hProcessSnap);
return pe32.th32ProcessID;
}
}
while (Process32Next(hProcessSnap, &pe32));
}
}
CloseHandle(hProcessSnap);
return 0;
} |
|
|
| Back to top |
|
 |
jeremy6996 Expert Cheater
Reputation: 0
Joined: 20 May 2007 Posts: 100
|
Posted: Sun Sep 30, 2007 2:04 pm Post subject: |
|
|
Phew thats alot fucking easier then what the other forums said. Thankyou vary much =D
*edit* witch Imports did you use?
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Sep 30, 2007 2:18 pm Post subject: |
|
|
fixed it up a bit
| Code: |
DWORD GetProcessID(LPCTSTR ProcessName)
{
PROCESSENTRY32 pe32; //0 is still a valid Process ID so use -1!
HANDLE hSnapshot; DWORD result = -1;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE) {
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
if(_tcsicmp(pe32.szExeFile, ProcessName) == 0) {
result = pe32.th32ProcessID;
break;
}
} while (Process32Next(hSnapshot, &pe32));
}
}
if (hSnapshot) CloseHandle(hSnapshot);
return result;
}
|
| jeremy6996 wrote: | | *edit* witch Imports did you use?" |
you probably meant include, and that is tlhelp32.h. chances are you already link to kernel32.lib
Last edited by appalsap on Mon Oct 01, 2007 12:43 pm; edited 2 times in total |
|
| Back to top |
|
 |
jeremy6996 Expert Cheater
Reputation: 0
Joined: 20 May 2007 Posts: 100
|
Posted: Sun Sep 30, 2007 2:26 pm Post subject: |
|
|
I keep getting this error to every single one of the scripts I have tried, I am using a C++ compiler and I get this error
| Quote: | c:\documents and settings\home main account\my documents\visual studio 2008\projects\maysomin v1.0 cpp\maysomin v1.0 cpp\functions.h(40) : error C2664: 'stricmp' : cannot convert parameter 1 from 'WCHAR [260]' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast |
I tried directly copying it into my header file too.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Sep 30, 2007 2:32 pm Post subject: |
|
|
| Just typecast it into (char *).
|
|
| Back to top |
|
 |
assaf84 Expert Cheater
Reputation: 0
Joined: 03 Oct 2006 Posts: 238
|
Posted: Sun Sep 30, 2007 2:32 pm Post subject: |
|
|
| Try to include <string.h> ?
|
|
| Back to top |
|
 |
jeremy6996 Expert Cheater
Reputation: 0
Joined: 20 May 2007 Posts: 100
|
Posted: Sun Sep 30, 2007 2:35 pm Post subject: |
|
|
| Flyte wrote: | | Just typecast it into (char *). |
I'm sorry, I am still quite new to C++ I just finished learning how to do structures and wanted to create some sort of project with my knowledg.
Can you explain.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Sun Sep 30, 2007 2:42 pm Post subject: |
|
|
| jeremy6996 wrote: | | Flyte wrote: | | Just typecast it into (char *). |
I'm sorry, I am still quite new to C++ I just finished learning how to do structures and wanted to create some sort of project with my knowledg.
Can you explain. |
http://www.cplusplus.com/doc/tutorial/typecasting.html
|
|
| Back to top |
|
 |
jeremy6996 Expert Cheater
Reputation: 0
Joined: 20 May 2007 Posts: 100
|
Posted: Sun Sep 30, 2007 2:52 pm Post subject: |
|
|
| Thanks, I'll be sure to reffer ro this website aswell as my book.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Sep 30, 2007 5:54 pm Post subject: |
|
|
| jeremy6996 wrote: | I keep getting this error to every single one of the scripts I have tried, I am using a C++ compiler and I get this error
| Quote: | c:\documents and settings\home main account\my documents\visual studio 2008\projects\maysomin v1.0 cpp\maysomin v1.0 cpp\functions.h(40) : error C2664: 'stricmp' : cannot convert parameter 1 from 'WCHAR [260]' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast |
I tried directly copying it into my header file too. |
You're compiling in Unicode, change to Multibyte if you want to make it easier to use that code, or you can rewrite it to use Unicode format like this:
| Code: | DWORD GetProcId( TCHAR *szProcName )
{
PROCESSENTRY32 pe32;
HANDLE hSnapshot = NULL;
BOOL bFound = FALSE;
pe32.dwSize = sizeof( PROCESSENTRY32 );
hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( Process32First( hSnapshot, &pe32 ) )
{
do{
if( wcsicmp( pe32.szExeFile, szProcName ) == 0 )
{
bFound = true;
break;
}
}while( Process32Next( hSnapshot, &pe32 ) );
}
if( hSnapshot != INVALID_HANDLE_VALUE )
CloseHandle( hSnapshot );
if( bFound )
return pe32.th32ProcessID;
else
return -1;
} |
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Sun Sep 30, 2007 6:00 pm Post subject: |
|
|
Wiccaan: the version I posted works on TCHARs: it is unicode/mbcs agnostic. both yours and flyte's are not, because of your use of wcsicmp and flyte's use of stricmp & char* in the parameter.
Flyte: typecasting is a horrible idea, if the compiler lets it through, since ascii wide characters are NULL padded, so stricmp will always return not equal.
|
|
| Back to top |
|
 |
Robotex Master Cheater
Reputation: 0
Joined: 05 Sep 2006 Posts: 378 Location: The pizza country!
|
Posted: Mon Oct 01, 2007 8:02 am Post subject: |
|
|
| Code: | DWORD GetProcessID(LPCTSTR ProcessName)
{
PROCESSENTRY32 pe32; //0 is still a valid Process ID so use -1!
HANDLE hSnapshot; DWORD result = -1;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE) {
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
if(stricmp(pe32.szExeFile, ProcessName) == 0) {
result = pe32.th32ProcessID;
break;
}
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
}
return result;
} |
better, because INVALID_HANDLE_VALUE equals (HANLE)(LONG_PTR)-1, and if(hSnapshot) will try to close even it isn't valid
_________________
ASM/C++ Coder
Project Speranza lead developer |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Oct 01, 2007 9:20 am Post subject: |
|
|
| appalsap wrote: | Wiccaan: the version I posted works on TCHARs: it is unicode/mbcs agnostic. both yours and flyte's are not, because of your use of wcsicmp and flyte's use of stricmp & char* in the parameter.
Flyte: typecasting is a horrible idea, if the compiler lets it through, since ascii wide characters are NULL padded, so stricmp will always return not equal. |
Any reason you are trying to call me out after you edited your post? When you first posted it, it was:
| Code: | | DWORD GetProcessID(char* ProcessName) |
If I recall correctly after looking at it this morning. Go compile your code in unicode. It wont compile because you aren't using correct functions. Yes, I know what TCHAR is and how it works.
Your current post:
| Code: | DWORD GetProcessID(LPCTSTR ProcessName)
{
PROCESSENTRY32 pe32; //0 is still a valid Process ID so use -1!
HANDLE hSnapshot; DWORD result = -1;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVAILD_HANDLE_VALUE) {
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
if(stricmp(pe32.szExeFile, ProcessName) == 0) {
result = pe32.th32ProcessID;
break;
}
} while (Process32Next(hSnapshot, &pe32));
}
}
if (hSnapshot) CloseHandle(hSnapshot);
return result;
} |
Your claim on compiling to both:
Unicode
| Code: |
error C2065: 'INVAILD_HANDLE_VALUE' : undeclared identifier
error C2664: 'stricmp' : cannot convert parameter 1 from 'WCHAR [260]' to 'const char *' |
The first being a mistype, but figured I'd include it in there.
stricmp doesn't work with the type of param you are passing to it.
As I said in my post, 'if you want unicode...' which my code was for Unicode, not for MB. But if you want both you need to do something like this:
| Code: | DWORD GetProcId(LPCTSTR tszProcName)
{
PROCESSENTRY32 pe32;
HANDLE hSnapshot = NULL;
BOOL bFound = FALSE;
pe32.dwSize = sizeof(PROCESSENTRY32);
hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( Process32First( hSnapshot, &pe32 ) )
{
do{
if( _tcscmp( pe32.szExeFile, tszProcName ) == 0 ){ bFound = TRUE; break; }
}while( Process32Next( hSnapshot, &pe32 ) );
}
if( hSnapshot != INVALID_HANDLE_VALUE )
CloseHandle( hSnapshot );
if( bFound )
return pe32.th32ProcessID;
else
return -1;
} |
Don't change your code and try to correct me. Let alone change without testing.
|
|
| Back to top |
|
 |
appalsap Moderator
Reputation: 0
Joined: 27 Apr 2006 Posts: 6753 Location: Pakistan
|
Posted: Mon Oct 01, 2007 1:02 pm Post subject: |
|
|
| Wiccaan wrote: | Any reason you are trying to call me out after you edited your post? When you first posted it, it was:
| Code: | | DWORD GetProcessID(char* ProcessName) |
|
No. That was definitely not me, you are probably thinking of Flyte. Or are you trying to persuade me to edit it now and point and say "Aha, appalsap edited it now, appalsap's not avert to editing!!"? If so, I took the bait, and I did correct my post (for the first time since I posted it)
| Wiccaan wrote: | | If I recall correctly after looking at it this morning. Go compile your code in unicode. It wont compile because you aren't using correct functions. Yes, I know what TCHAR is and how it works. |
Right. I knew I should've used _tcsicmp, and I thought I did, but I just copied flyte's stricmp because I wanted to correct a style error, that is, he used "!" in place of "== 0" which could be confusing because "!" is NOT, and NOT anyfunction has you believe that it is not true. If you were going to use TCHARs, use _tcsicmp, and if you are going to use WCHARs, use wcsicmp, because then the errors will be spotted quickly. Why I choose to use TCHARs in examples is because some IDEs by default (dev-c++, delphi) will use mbcs and some IDEs (visual studio) will use unicode. The code will be usable by both.
| Wiccaan wrote: | | Your claim on compiling to both: |
No I did not! I said It works on TCHARs, yours and flyte's does not. I never said anything about compiling, and it's clear that I didn't compile it because of the typing error I made. also, *icmp works on constant strings, LPCTSTR is a constant string while TCHAR* is not (your first post). The code is used for illustrative purposes, and the first thing you should learn when learning a language with weak typing is to deal with type errors.
| Wiccaan wrote: | | Don't change your code and try to correct me. |
I did not change my code. I wouldn't post a change with a char*, and none of my code uses char*, (exception is when I avoid using windows types). My first post was an improvement on the general style of the code (like the NOT issue up in the post) and my second post was on TCHARs in general. Attacking my code does not attack the validity of my second post (and even you could see they were just errors, not of ignorance).
|
|
| Back to top |
|
 |
|