Sign in to follow this  

OpenProcess always returning false

This topic is 3493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
	}

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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);






Unless you really need to dynamically allocate that memory it would be best for you to create the buffer statically..

try this instead

char MyRead[9] = {0}; // Or you could do char MyRead[9] and a memset to zero's 9 to prevent buffer over for whatever possible reason that may occur
SIZE_T MyLen = 0;
ReadProcessMemory(SWGHandle, ULongToPtr(0x0019D356), MyRead, 8, &MyLen);
printf("Len: %u \n", MyLen);
printf("Value: %s \n", MyRead);




Share this post


Link to post
Share on other sites
And the one thing you're all failing to do that is so important... Check for errors!


char* Variable = new char[7];
SIZE_T VariableLen = 0;
if (!ReadProcessMemory(SWGHandle, (LPVOID)0x0019D356, Variable, 8, &VariableLen))
{
printf("Len: %u \n", VariableLen);
printf("Value: %s \n", Variable);
} else {
printf("FAILED! Check GetLastError()!\n");
}



Reading (or learning how to use it) MSDN will be a great help to you. It has everything you need to solve all these problems on your own in stead of waiting days for replies.

ReadProcessMemory returns non-zero on success and 0 on failure.

Share this post


Link to post
Share on other sites

This topic is 3493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this