Sign in to follow this  
abort

WriteProcessMemory

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

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