# WriteProcessMemory

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

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

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 on other sites
Although I had no idea you could even do this, I can try to help. Define "but the parameter doesn't work properly".

##### Share on other sites
Quote:
 Original post by WolfdogAlthough 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 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 on other sites
Quote:
 Original post by NypyrenI 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.