I want to call my function from another process. It work with LoadLibrary and my dll but when i use application-defined function, target
application crash.
Error is: The process was terminated due to an unhandled exception. Exception Info: exception code c0000005, exception address 064F...
Build as Release still the same. Remote thread don't return and still alive.
Has anyone encountered this issue before and/or know a solution?
DWORD WINAPI ThreadProc(_In_ LPVOID lpParameter) {
return (DWORD)&lpParameter;
}
int main()
{
//process
HANDLE hProcess = GetProcess();
if (!hProcess) {
cout << "!hProcess\n";
system("pause");
exit(0);
}
//function
SIZE_T nFSize = 200000;
LPVOID lpFunctionAddress = VirtualAllocEx(hProcess, NULL, nFSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!lpFunctionAddress) {
cout << "!lpFunctionAddress\n";
system("pause");
exit(0);
}
WriteProcessMemory(hProcess, lpFunctionAddress, &ThreadProc, nFSize, NULL);
//param
DWORD param = 222;
SIZE_T nPSize = sizeof(param);
LPVOID lpParamAddress = VirtualAllocEx(hProcess, NULL, nPSize, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (!lpParamAddress) {
cout << "!lpFunctionAddress\n";
system("pause");
exit(0);
}
WriteProcessMemory(hProcess, lpParamAddress, ¶m, nPSize, NULL);
//run thread
HANDLE pRemoteThread = CreateRemoteThread(hProcess, NULL, nFSize, (LPTHREAD_START_ROUTINE)lpFunctionAddress, lpParamAddress, 0, NULL);
DWORD code;
if (!pRemoteThread) {
cout << "!pRemoteThread\n";
system("pause");
exit(0);
}
WaitForSingleObject(pRemoteThread, INFINITE);
//
GetExitCodeThread(pRemoteThread, &code);
cout << code << endl;// code = 3221225477 and target crash
system("pause");
return 0;
}