#define NOMINMAX #include #include #include #include #include int __cdecl main() { using namespace WinLib; DWORD pid = 0; std::cout << "Enter pid: "; std::cin >> pid; std::cout << "Enter handle value: "; HANDLE hExternal = NULL; std::cin >> std::hex >> hExternal; HANDLE hProc = GetCurrentProcess(); ScopedWrapper<> hSourceProc(OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid), &CloseHandle); if(!*hSourceProc) { std::cout << "Couldn't open source process handle: " << GetLastError() << '\n'; return 1; } HANDLE hSourceFile = NULL; DuplicateHandle(*hSourceProc, hExternal, hProc, &hSourceFile, GENERIC_READ, FALSE, 0); if(!hSourceFile) { std::cout << "Couldn't duplicate handle: " << GetLastError() << '\n'; return 2; } ScopedWrapper<> source(hSourceFile, &CloseHandle); std::cin.ignore(std::numeric_limits::max(), '\n'); std::cout << "Enter output file name: "; std::string fileName; std::getline(std::cin, fileName); ScopedWrapper<> hFile(CreateFileA(fileName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL), &CloseHandle); if((*hFile) == INVALID_HANDLE_VALUE) { std::cout << "Could open output file " << GetLastError() << '\n'; return 3; } char buf[8192]; DWORD bytesRead = 0; BOOL bRet = FALSE; SetFilePointer(*source, 0, NULL, FILE_BEGIN); while((bRet = ReadFile(*source, buf, sizeof(buf), &bytesRead, NULL)) && bytesRead) { DWORD written = 0; if(!WriteFile(*hFile, buf, bytesRead, &written, NULL)) { std::cout << "WriteFile failed " << GetLastError() << '\n'; return 4; } } return 0; }