Improve code and add error codes to error messages in SpotifyKeyDumper

This commit is contained in:
_ 2020-12-06 17:25:22 -07:00
parent ece427863b
commit 55b43f76f1

View File

@ -17,7 +17,7 @@ DWORD GetProcId(const wchar_t* procName)
if (hSnap != INVALID_HANDLE_VALUE) if (hSnap != INVALID_HANDLE_VALUE)
{ {
PROCESSENTRY32 procEntry; PROCESSENTRY32 procEntry = {};
procEntry.dwSize = sizeof(procEntry); procEntry.dwSize = sizeof(procEntry);
if (Process32First(hSnap, &procEntry)) if (Process32First(hSnap, &procEntry))
@ -53,7 +53,6 @@ void StartSuspendedInjection()
std::wstring procNameW = std::wstring(PROC_NAME); std::wstring procNameW = std::wstring(PROC_NAME);
MessageBox(NULL, std::wstring(procNameW + std::wstring(L" has already been detected!\n\n" MessageBox(NULL, std::wstring(procNameW + std::wstring(L" has already been detected!\n\n"
"Please launch this before ") + procNameW).c_str(), NULL, MB_OK | MB_ICONERROR); "Please launch this before ") + procNameW).c_str(), NULL, MB_OK | MB_ICONERROR);
return; return;
} }
@ -63,21 +62,22 @@ void StartSuspendedInjection()
startupInfo.cb = sizeof(STARTUPINFOA); startupInfo.cb = sizeof(STARTUPINFOA);
ZeroMemory(&procInfo, sizeof(procInfo)); ZeroMemory(&procInfo, sizeof(procInfo));
if (!CreateProcess(PROC_NAME, NULL, NULL, NULL, false, CREATE_SUSPENDED, NULL, NULL, &startupInfo, &procInfo)) if (!CreateProcessW(PROC_NAME, NULL, NULL, NULL, false, CREATE_SUSPENDED, NULL, NULL, &startupInfo, &procInfo))
{ {
std::wcout << "Error: Could not start " << PROC_NAME << " (could not create process)" << std::endl; std::wcout << "Error: Could not start " << PROC_NAME << " (could not create process): " << GetLastError()
<< std::endl;
return; return;
} }
std::cout << "Injecting DLL..." << std::endl; std::cout << "Injecting DLL..." << std::endl;
if (GetFileAttributes(PROC_NAME) == INVALID_FILE_ATTRIBUTES) if (GetFileAttributesW(PROC_NAME) == INVALID_FILE_ATTRIBUTES)
{ {
std::wcout << "Error: DLL injection failed (could not find " << PROC_NAME << ")" << std::endl; std::wcout << "Error: DLL injection failed (could not find " << PROC_NAME << ")" << std::endl;
return; return;
} }
if (GetFileAttributes(DLL_FILE_PATH) == INVALID_FILE_ATTRIBUTES) if (GetFileAttributesW(DLL_FILE_PATH) == INVALID_FILE_ATTRIBUTES)
{ {
std::wcout << "Error: DLL injection failed (could not find " << DLL_FILE_PATH << ")" << std::endl; std::wcout << "Error: DLL injection failed (could not find " << DLL_FILE_PATH << ")" << std::endl;
return; return;
@ -86,13 +86,13 @@ void StartSuspendedInjection()
tmpPage = VirtualAllocEx(procInfo.hProcess, NULL, dllPathLen, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); tmpPage = VirtualAllocEx(procInfo.hProcess, NULL, dllPathLen, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (tmpPage == NULL) if (tmpPage == NULL)
{ {
std::wcout << "Error: DLL injection failed (could not allocate memory)" << std::endl; std::wcout << "Error: DLL injection failed (could not allocate memory): " << GetLastError() << std::endl;
return; return;
} }
if (!WriteProcessMemory(procInfo.hProcess, tmpPage, (PVOID) DLL_FILE_PATH, dllPathLen, NULL)) if (!WriteProcessMemory(procInfo.hProcess, tmpPage, (PVOID) DLL_FILE_PATH, dllPathLen, NULL))
{ {
std::wcout << "Error: DLL injection failed (could not write memory)" << std::endl; std::wcout << "Error: DLL injection failed (could not write memory): " << GetLastError() << std::endl;
return; return;
} }
@ -100,19 +100,20 @@ void StartSuspendedInjection()
NULL); NULL);
if (injectThread == NULL) if (injectThread == NULL)
{ {
std::wcout << "Error: DLL injection failed (could not load library)" << std::endl; std::wcout << "Error: DLL injection failed (could not load library): " << GetLastError() << std::endl;
return; return;
} }
if (WaitForSingleObject(injectThread, UINT_MAX) == WAIT_FAILED) if (WaitForSingleObject(injectThread, UINT_MAX) == WAIT_FAILED)
{ {
std::wcout << "Error: DLL injection failed (could not wait for thread to return)" << std::endl; std::wcout << "Error: DLL injection failed (could not wait for thread to return): " << GetLastError()
<< std::endl;
return; return;
} }
if (ResumeThread(procInfo.hThread) == -1) if (ResumeThread(procInfo.hThread) == -1)
{ {
std::wcout << "Error: DLL injection failed (could not resume thread)" << std::endl; std::wcout << "Error: DLL injection failed (could not resume thread): " << GetLastError() << std::endl;
return; return;
} }