| IWishIKnew (469) | |||
|
Ok, i REALLY HATE win API, but it executes faster than batch commands, bla bla bla. So, first: What is a 'HANDLE' supposed to be? a String? an Integer? The compiler says it's void, but it doesn't even compile the thing:
so, if i got a vector of strings with the image name of each process that needs to be terminated, how can i convert to HANDLES??? Second: What's with the "in"'s and "out"'s??? It makes no sense:
"_in_" I going to try and guess: a variable type? Third: UINT- again: is that a string, int, how am i supposed to convert us-able data into whatever a "UINT" is? I'm sorry, but it's so frustrating trying to learn this on my own. Thank you so much for your time! | |||
|
|
|||
| modoran (1245) | |
UINT is declared as typedef unsigned int UINT .A HANDLE must only be obtained from win32 APIs and only used as parameter to other win32 APIs, why do you want to convert it ? See this link for Windows data types: http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx In your case use OpenProcess() to obtain the handle required for TerminateProcess() if you have the process ID. http://msdn.microsoft.com/en-us/library/windows/desktop/ms684320%28v=vs.85%29.aspx | |
|
Last edited on
|
|
| IWishIKnew (469) | ||
so.... how can i even get the handle, when I don't even know the process ID??
where does "the process image name.exe" fit into this? if DWORDs are integers, and the only other thing there is a bool, how am I supposed to use this to get the process' handle? (quite literally, because as you can see, I don't know much about the windows API) | ||
|
Last edited on
|
||
| andywestken (1966) | |
|
You don't convert an image name to a handle... You have to enumerate the running processes and find the ones which are running the image you're looking for, and get the process ids from them. You then feed that into OpenProcess. One way to enumerate through the processes is the Toolhelp API: CreateToolhelp32Snapshot, Process32First, and Process32Next. Then you're got to contend with the security privileges, etc. Andy PS _In_, _Etc_ are nothing to do with C++ -- they are macros which compile to nothing. But they are used by the Microsoft's static analysis tool (they're par of the Source Code Annotation Language - aka SAL). PPS See also Taking a Snapshot and Viewing Processes (Windows) http://msdn.microsoft.com/en-gb/library/windows/desktop/ms686701%28v=vs.85%29.aspx How To Terminate an Application "Cleanly" in Win32 http://support.microsoft.com/kb/178893 | |
|
Last edited on
|
|
| IWishIKnew (469) | ||
1st problem:
"TH32CS_SNAPPROCESS" comes from absolutely nowhere. This is the problem i have with win API, they have all these variables that aren't declared. Could someone please explain how this is supposed to even work? Also, ty andy and modoran for your replies, they were very helpful, but raise a lot more questions than they answer.... | ||
|
|
||
| andywestken (1966) | ||
|
TH32CS_SNAPPROCESS doesn't come from nowhere, it's in TlHelp32.h Searching MSDN, or Googling, for CreateToolhelp32Snapshot, as I'd assume you'd do to learn more about the call, you would have found: CreateToolhelp32Snapshot function (Windows) http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682489%28v=vs.85%29.aspx which tells you, in the Requirements section:
This hold for most if not all Windows API calls. Whenever I come across an API call I don't know, or use one I've not used for a while, then that's what I do! Andy | ||
|
Last edited on
|
||
| IWishIKnew (469) | ||
|
so.... Windows doesn't even follow the rules? The way I understand it, function calls look like this:
| ||
|
|
||
| naraku9333 (1038) | |
|
@IWishIKnew Where are you getting that? Maybe looking at this sample app that gets a process snapshot (from the link andywestken provided) will help http://msdn.microsoft.com/en-gb/library/windows/desktop/ms686701(v=vs.85).aspx | |
|
Last edited on
|
|
| Imadatobanisa (647) | ||
|
I think you want to terminate a specific process, right? TerminateProcess requires a valid process HANDLE (process address). The HANDLE should have the attribute PROCESS_TERMINATE with a correct ProcessId. Use the function OpenProcess. If you want to find a specific process, you'll need to follow some steps :
- Get a HANDLE value via the function CreateToolhelp32Snapshot. Specify the value TH32CS_SNAPPROCESS here. - Create a PROCESSENTRY32 variable. (Not pointer) - Use the Process32First,and Process32Next loop. And, you asked what is HANDLE? Actually it's a PVOID value (*void), means it's a unknown pointer variable definition. This definition type specifies a specific address. A reference link : http://www.cplusplus.com/forum/beginner/86531/ Need more details or examples? Hope all goes well . <:) | ||
|
Last edited on
|
||
| IWishIKnew (469) | ||
Specify the value of what?? What does that "value" represent?? | ||
|
|
||
| andywestken (1966) | |||
|
"HANDLE value" = the handle of the process you want to terminate "the value TH32CS_SNAPPROCESS" = the value of dwFlags in
See CreateToolhelp32Snapshot function (Windows) http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682489%28v=vs.85%29.aspx | |||
|
Last edited on
|
|||
| modoran (1245) | |
|
You don't need to know what TH32CS_SNAPPROCESS or HANDLE is, just follow the function documentation as it sais in MSDN. Did you even read it ? If yes, read it again more carefully ! | |
|
|
|
| IWishIKnew (469) | |||||
|
@ modoran Yes I did, and didn't understand a word of it because I don't know what a handle is, or how winAPI even works. I also don't understand why you have to write a humungus function just to simplify the dang thing to a "create_process(string process_name)". Yes, yes... a handle is the "handle of the process you want to terminate"... but WHAT is it: a string?? How am I supposed to 'treat' a handle? Is is a variable at all? is it as simple as HANDLE a_handle = "process_name.exe"??and not to mention this:
ok, i will give this a wack:
Tell me if i'm wrong (I'm pretty sure I am...). | |||||
|
|
|||||
| Imadatobanisa (647) | |
|
-OpenProcess Third parameter requires a DWORD value (means an unsigned integer value). So, you can't attach your string to this slot. This causes a compling error. It is a ProcessId, have you read it carefully? How to get it? Use the Process32First, Process32Next and CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS,0) with structure (PROCESSENTRY32) (Process linear search) Need more details or examples? Tip : (Another solution & instant result) Open Task Manager and see the process PID Edit : The first parameter : If you want to terminate a process, please, specify the value PROCESS_TERMINATE here. Hope all goes well, dude :) | |
|
Last edited on
|
|
| modoran (1245) | |||
This sample code is taken from MSDN, from the link already posted in this topic:
| |||
|
|
|||
| Imadatobanisa (647) | |
|
@modoran Certainly, Microsoft samples are really perfect, but the sample you mentioned requires lots of knowledge and some of this are not related.... The OP now is still asking about Variable definition and how to use a WinAPI function. So, the OP should take a simpler sample or a snippet code example. That's better. | |
|
|
|
| andywestken (1966) | |
|
Terminating a process is not beginner level code on Windows. It requires knowledge about the Win32 security mechanism and how Windows manages processes. There is no simpler answer. The call to TerminateProcess itself is the easy bit. It's finding the handle from the image path and adjusting the user privileges which are the problem. Andy PS Regarding handles... A handle is an abstract (typically integer) identifier; for Win32, it's a 32 bit integer, for Win64 it's a 64 bit one. You give the handle to Windows and it uses it to finds the corresponding object. How it's actually stored inside is hidden from you. Note that the handle approach is not unique to Windows. Handle (computing) http://en.wikipedia.org/wiki/Handle_%28computing%29 What is a Windows Handle? http://stackoverflow.com/questions/902967/what-is-a-windows-handle | |
|
|
|
| IWishIKnew (469) | |
|
so... Mabey some direction with the windows API would be helpful. I'm not particularly well versed with it. Thank you for all of your help, I really really appreciate it! | |
|
|
|