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 


Killing/Ending/Closing a Process/Window
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Tue Aug 19, 2008 6:55 pm    Post subject: Reply with quote

allah have mercy, use int main
Back to top
View user's profile Send private message
BirdsEye
Advanced Cheater
Reputation: 0

Joined: 05 Apr 2008
Posts: 94

PostPosted: Tue Aug 19, 2008 7:10 pm    Post subject: Reply with quote

slovach wrote:
allah have mercy, use int main


It was int main in my previous code, I just used void main because it was in Rot1's code. Can you explain to me what's the difference? Because I only learned to begin a console's main code using int main. Also if theres more thingys(sorry) like void main and int main, can you tell me them and explain also.
Thanks.
Back to top
View user's profile Send private message
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Tue Aug 19, 2008 7:50 pm    Post subject: Reply with quote

In simple terms, when a program starts, the Windows PE Loader does all kinds of initialization. Then, it calls the entry point. In C/C++ programs, the "main()" function is the entry point. When the function returns, the return value is the exit code for the process. This is the technical explanation.

But, there's really no need for the process exit code. It's more of a formality than a necessity. It started with ISO C and ISO is a standard, so it was adopted. Now, there's really no need. Ignore all the people who say it must be int, because it doesn't matter. If you want to be really formal, you can say to them that it's supposed to be unsigned int or DWORD, not int.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Tue Aug 19, 2008 10:58 pm    Post subject: Reply with quote

rapion124 wrote:
In simple terms, when a program starts, the Windows PE Loader does all kinds of initialization. Then, it calls the entry point. In C/C++ programs, the "main()" function is the entry point. When the function returns, the return value is the exit code for the process. This is the technical explanation.

But, there's really no need for the process exit code. It's more of a formality than a necessity. It started with ISO C and ISO is a standard, so it was adopted. Now, there's really no need. Ignore all the people who say it must be int, because it doesn't matter. If you want to be really formal, you can say to them that it's supposed to be unsigned int or DWORD, not int.


When you exit, you tell the shell how shit went down.

Basically, the moral of this story is, if you use void, you'll likely be ridiculed for being a dolt.
Back to top
View user's profile Send private message
igoticecream
Grandmaster Cheater Supreme
Reputation: 0

Joined: 23 Apr 2006
Posts: 1807
Location: 0x00400000

PostPosted: Wed Aug 20, 2008 10:38 pm    Post subject: Reply with quote

slovach wrote:
rapion124 wrote:
In simple terms, when a program starts, the Windows PE Loader does all kinds of initialization. Then, it calls the entry point. In C/C++ programs, the "main()" function is the entry point. When the function returns, the return value is the exit code for the process. This is the technical explanation.

But, there's really no need for the process exit code. It's more of a formality than a necessity. It started with ISO C and ISO is a standard, so it was adopted. Now, there's really no need. Ignore all the people who say it must be int, because it doesn't matter. If you want to be really formal, you can say to them that it's supposed to be unsigned int or DWORD, not int.


When you exit, you tell the shell how shit went down.

Basically, the moral of this story is, if you use void, you'll likely be ridiculed for being a dolt.


Fuck off the story...

the correct way of a C/C++ application entry point is int main(void), why? because startup routines that call main could be assuming that the return value will be pushed onto the stack. If main() does not do this, then this could lead to stack corruption in the program's exit sequence, and cause it to crash... so if you want your application be portable do main return int

btw, int main(void) is used when you do not require access to the command line arguments (int main ( int argc, char *argv[] ))

and before someone thinks " int main() = int main(void)" my asnwer is no, because int main() is deemd to take an unknown number of arguments. If you declare void as parameter of main, you are telling th ecompiler that main takes no parameter.

_________________
+~
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Thu Aug 21, 2008 3:07 pm    Post subject: Reply with quote

igoticecream wrote:
and before someone thinks " int main() = int main(void)" my asnwer is no, because int main() is deemd to take an unknown number of arguments. If you declare void as parameter of main, you are telling th ecompiler that main takes no parameter.


That would be dependent on the compiler alone, not the language. VS2008 will handle a blank argument count as nothing, thus void. And if you attempt to pass a function without a param list, it will error and not compile.

Either way, doing int main(void) or int main(), yields the same code in VS2008:

Code:
00401000 >  55              PUSH EBP
00401001    8BEC            MOV EBP,ESP
00401003    33C0            XOR EAX,EAX
00401005    5D              POP EBP
00401006    C3              RET


Can it be different for other compilers? Sure. A compiler should be designed to follow standards, and I'm sure somewhere in the huge ass PDF it states that a function should be treated as ( void ) if theres no param list set.



Anyway, ontopic. To detect a specific process, I would suggest not using FindWindow, but instead, detect by the process name. You can do this by using CreateToolhelp32Snapshot, Process32First, and Process32Next.

Code:
#include <windows.h>
#include <tchar.h>
#include <tlhelp32.h>

int main( int argc, TCHAR argv[] )
{
   PROCESSENTRY32 pe32 = { sizeof( PROCESSENTRY32 ) };
   HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

   if( hSnapshot == INVALID_HANDLE_VALUE )
      return 0;

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

   while( Process32Next( hSnapshot, &pe32 ) )
   {
      if( _tcsicmp( _T("notepad.exe"), pe32.szExeFile ) == 0 )
      {
         CloseHandle( hSnapshot );
         // Process found, do what ever you want here..
         return 0;
      }
   }

   CloseHandle( hSnapshot );
   return 0;
}


Doesn't do everything you are trying to do, just showing you how to use the API. Work in what you need, and such.

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

Joined: 23 Apr 2006
Posts: 1807
Location: 0x00400000

PostPosted: Thu Aug 21, 2008 8:42 pm    Post subject: Reply with quote

i see, never used vs2008 before
_________________
+~
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Aug 21, 2008 9:31 pm    Post subject: Reply with quote

igoticecream wrote:
i see, never used vs2008 before


Dunno if my memory is hazy but, int main(void) is a C thing, while it's not so much for C++.
Back to top
View user's profile Send private message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Fri Aug 22, 2008 4:58 am    Post subject: Reply with quote

BirdsEye wrote:
Rot1 wrote:
BirdsEye wrote:
Rot1 wrote:
Follow these steps carefully or you will BSOD (joking):

To kill a process by getting its ID via Window Caption:
FindWindow -> GetWindowThreadProcessId -> OpenProcess (PROCESS_TERIMINATE access) -> TerminateProcess

To kill a process by getting its ID via looping around the snapshot list:
CreateToolhelp32Snapshot -> Process32First/Next -> Compare statement -> OpenProcess -> TerminateProcess


Thanks Kasper but I'm a bit confused with choice A. I use FindWindow to get a handle on the window and then I used the returned handle in GetWindowThreadProcessId but I don't understand the second parameter. And where would I use the handle that GetWindowThreadProcessId returned? And for OpenProcess also. I'm all confused and don't understand the last param of OpenProcess. Which handle do you use in TerminateProcess? Did I mention I'm confused?... Can you please give an example? Or collaborate a bit more?


Code:
#include <windows.h>

DWORD pID; //variable that will hold the pID

void main( void )
{
HWND hWnd = FindWindow(NULL, L"GTA: San Andreas");
GetWindowThreadProcessId(hWnd, &pID); //Gets the ID and stores it
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pID); //gets handle and access
TerminateProcess(hProcess, 0); //we aim it on the handle we got
return;
}


o.o"... My code was exactly like that except with different FindWindow paramaters. I used your code and filled accordingly but it still doesn't work o.o...
EDIT: I found out that it my mistake. After I filled out the exact Window name and made my first FindWindow param NULL, it worked. It was my fault.


Quick lesson about FindWindow, first parameter is the Class name of the window, second is the Window caption name, example:

FindWindow(L"MapleStoryClass",L"MapleStory");
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 Previous  1, 2
Page 2 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