#include <windows.h> #include <Psapi.h> #include, hModular is returning error 6 which is Invalid Handle, I cant figure out whats going on
#include <windows.h> #include <Psapi.h> #include, hModular is returning error 6 which is Invalid Handle, I cant figure out whats going on
There is so much wrong with that code I don't even know where to start... Did you even TRY to read the documentation on MSDN??
Grab the book "Windows via C/C++" and learn the basics of unmanaged Windows programming. Right now you're WAAAAAY out of your depth.
P.S. You're lucky the Australian Open is on tonight and I'm in a good mood.
P.P.S. You don't need to flush the output stream that often. Prefer '\n' to std::endl when you have the option. It's a LOT faster. The only reason I use std::endl in my code so much is that I often use my logging class which timestamps lines after a buffer flush. Unless you have a good reason like that, just use '\n'.
Last edited by Cypher; 01-22-2011 at 06:59 AM.
Trying to print a HANDLE, seriously?
Seems like you are NOT ready for such programming, try going in small steps instead of jumping to the end, you wont learn anything by it.
I think it's a matter of taste... a handle's value is nothing important for the programmer, as long as it is not invalid (NULL in this case).
That's why I normally prefer error handling code like
Code:HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId); if (!hProc) { std::cout << "Process handle is invalid.\n"; std::cout << "ErrorCode: " << std::hex << GetLastError() << std::endl; }
That could give you an invalid error code.
You should call GetLastError DIRECTLY after you call the API (well, it doesn't necessarily have to be directly after, but it does need to be before any other statements with side-effects that could affect its value).
Think about it... What happens if an internal API that std::cout uses fails somewhere (or even succeeds! as some APIs reset the LastError value to ERROR_SUCCESS when they succeed)?? The error code you want will be lost!
This is how you should be doing it:
Code:HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId); if (!hProc) { DWORD const LastError = GetLastError(); std::cout << "Process couldn't be opened.\nErrorCode: " << LastError << std::endl; }
Last edited by Cypher; 01-23-2011 at 06:45 AM.
Yeah okay but I don't think that he was wrong printing it for testing reasons. This code looks nothing like a full program so as long as he is in this testing and finding out phase it isn't wrong to print a Handle so flaming him like ejt did is kinda useless since he is not doing anything wrong by printing the handle.
Even though I agree with the rest of his statement to some extend. I know I am pointing out something small but I dunno I want to fight for the newbs since I am one myself and flaming something that is not necessarily wrong when there are other things that deserve flaming is kinda strange to me.
Not trying to offend anyone, just saying.
Did I hear someone yell at me for printing a handle, hey ******* I was seeing if the Handle was properly being assigned.
---------- Post added at 08:26 PM ---------- Previous post was at 08:24 PM ----------
Yeah, I just wanted to see if I could accomplish something. Oh yeah and im reading the Windows System Programming books, they give you some pretty ****ing Epic shit.