Jump to content
  • Advertisement
Sign in to follow this  
abort

WriteProcessMemory

This topic is 3658 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

fixed, thx for effort guys, updated code is below! Hey there guys.. I am using writeprocessmemory to call a dll function in another process. It does call the function, but the parameter doesn't work properly. It requires a HWND parameter. I have the same function with a string and that DOES work. I would like to have it working with a HWND parameter. Maybe someone succeeded in this or knows what I am doing wrong! Everything else is working properly (e.g. opening the process, as i already did a dll injection in the same function). Application (inside a bool function):
        HWND hWnd = FindWindow(NULL, "Application Window");
        if (!hWnd) {
               cout << "WriteProcessMemory error (" << GetLastError() << ").\n";
               return false;
        }
	LPVOID lpWindow = VirtualAllocEx(hProcess, NULL, sizeof(HWND), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
	if (!lpWindow) {
		cout << "VirtualAllocEx error (" << GetLastError() << ").\n";
		return false;
	}
	
	if (!WriteProcessMemory(hProcess, lpWindow, &hWnd, sizeof(HWND), NULL)) {
		cout << "WriteProcessMemory error (" << GetLastError() << ").\n";
		return false;
	}

	HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpFunctionAddress, lpWindow, 0, NULL); // Function address doesn't matter to you guys ;b

	if (!hThread) {
		cout << "CreateRemoteThread error (" << GetLastError() << ").\n";
		return false;
	}



DLL:
extern "C" __declspec(dllexport) void Initialize(HWND* hWndptr) {
        HWND hWnd = *hWndptr;
        if (!IsWindow(hWnd)) MessageBox(NULL, "Invalid window", "test", MB_OK);
        else MessageBox(NULL, "Valid window", "test", MB_OK);
}



Any suggestions are welcome! Thanks in advance. Edit: Eh it seems the forum doesn't like the cout arrows :b [Edited by - abort on August 15, 2008 3:08:57 PM]

Share this post


Link to post
Share on other sites
Advertisement
Although I had no idea you could even do this, I can try to help. Define "but the parameter doesn't work properly".

Share this post


Link to post
Share on other sites
Quote:
Original post by Wolfdog
Although I had no idea you could even do this, I can try to help. Define "but the parameter doesn't work properly".


It seems that the HWND passed to the writeprocessfunction doesn't match with the HWND that is received by the dll function. In other words, a correct HWND becomes an incorrect HWND as soon as the dll uses it..

Share this post


Link to post
Share on other sites
I suppose you could try making some kind of system-wide memory mapped file that the DLL side knows how to access without needing to pass a parameter.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nypyren
I suppose you could try making some kind of system-wide memory mapped file that the DLL side knows how to access without needing to pass a parameter.


Hah thx for the suggestion. I just got it fixed, i'll post the updated code for anyone interested.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!