OpenProcess always returning false

Started by
11 comments, last by lordcorm 15 years, 11 months ago
Im able to get the window's HWND, and the thread ID, but im not able to get the Handle with OpenProcess.

	HWND SWGProcess;
	HANDLE SWGHandle;
	DWORD SWGProcessID;

	SWGProcess = FindWindow(NULL,"mIRC");
	if(!SWGProcess)
	{
		printf("SwgClient.exe is not started, please close this program and start up SWG before opening this program. \n");
		system("pause");
		return;
	}
	
	GetWindowThreadProcessId(SWGProcess, &SWGProcessID);
	printf("Window Thread Process ID [%u] \n", SWGProcessID);
	SWGHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, SWGProcessID);

	if(!SWGHandle)
	{
		printf("Could not get application handle to hook. \n");
		system("pause");
		return;
	}

Advertisement
GetLastError()
Quote:Original post by marius1930
GetLastError()


Error number returned was 5. Tried searching "GetLastError() = 5" and "GetLastError()" and "GetLastError() 5" on google, but couldnt get anything :(
Do you have admin rights?
According to msdn, error code 0x5 is an access denied error.

Error Code List
No need for Google. Use FormatMessage.
Quote:Original post by lordcorm
Quote:Original post by marius1930
GetLastError()


Error number returned was 5. Tried searching "GetLastError() = 5" and "GetLastError()" and "GetLastError() 5" on google, but couldnt get anything :(


Did you really try just "GetLastError()" on google? Because the first has this paragraph:
Quote:To obtain an error string for system error codes, use the FormatMessage function. For a complete list of error codes provided by the operating system, see System Error Codes.
And the System Error Codes links to an error code table. According to this table the error is "Access Denied"

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

ALright, and by looking up information related to OpenProcess I found this gem:

Quote:Windows Server 2003 and Windows XP/2000: The size of the PROCESS_ALL_ACCESS flag increased on Windows Server 2008 and Windows Vista. If an application compiled for Windows Server 2008 and Windows Vista is run on Windows Server 2003 or Windows XP/2000, the PROCESS_ALL_ACCESS flag is too large and the function specifying this flag fails with ERROR_ACCESS_DENIED. To avoid this problem, specify the minimum set of access rights required for the operation. If PROCESS_ALL_ACCESS must be used, set _WIN32_WINNT to the minimum operating system targeted by your application (for example, #define _WIN32_WINNT _WIN32_WINNT_WINXP). For more information, see Using the Windows Headers.

edit: More Here

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Thx for the help guys! :D
Hey guys, i have another weird problem. Its when i do the ReadProcessMemory() function. I read it into my buffer, and it says it successfully copy's all of my 8 bytes over to the buffer, but when i print the buffer, or look at it in the debugger, it only has the first character.

		char* Variable = new char[7];		SIZE_T VariableLen = 0;		ReadProcessMemory(SWGHandle, (LPVOID)0x0019D356, Variable, 8, &VariableLen);		printf("Len: %u \n", VariableLen);		printf("Value: %s \n", Variable);
From what I can tell, the function returns a buffer, not a string. The second byte could be NULL and therefore you are only printing the first byte. Also you only allocate 7 bytes for your buffer while telling ReadProcessMemory to read 8. That could be overwriting something important.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement