void InjectMain(void) { HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (h == INVALID_HANDLE_VALUE) throw 3; PROCESSENTRY32 pEntry = { 0 }; pEntry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(h, &pEntry) == false) throw 3; do { if (_tccmp(pEntry.szExeFile, g_szInjectProcessName) == 0) { InjectProcessToDll(pEntry.th32ProcessID); } } while (Process32Next(h, &pEntry)); }
void PrintProcessNameAndID(DWORD processID, bool theFirst) { TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); if (NULL != hProcess) { HMODULE hMod; DWORD cbNeeded; if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) { GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(TCHAR)); } } if (_tccmp(szProcessName, L"<unknown>")) { if (theFirst) { twar[i] = processID; i++; } switch (GetPriorityClass(hProcess)) { case IDLE_PRIORITY_CLASS: ListBox_AddString(hWndListOne, _tcscat(szProcessName,L" - IDLE")); break; case NORMAL_PRIORITY_CLASS: ListBox_AddString(hWndListOne, _tcscat(szProcessName, L" - Normal")); break; case REALTIME_PRIORITY_CLASS: ListBox_AddString(hWndListOne, _tcscat(szProcessName, L" - Realtime") ); break; case HIGH_PRIORITY_CLASS: ListBox_AddString(hWndListOne, _tcscat(szProcessName, L" - High")); break; } } CloseHandle(hProcess); }
///<summary>Checks whether the given file is a valid image file.</summary> ///<param name="szFile">The path to the file to check.</param> ///<returns>Whether the file is valid.</summary> BOOL IsValidImageFile(LPTSTR szFile) { TCHAR szFileExt[8] = { 0 }; // The file extension of the actual file DWORD dwExtSize; // Size of the file extension TCHAR szFilterExt[8] = { 0 }; // The current filter extension being compared BOOL bIsValidExt = FALSE; TCHAR* curChar; TCHAR period = TEXT('.'); TCHAR* lastPeriod = NULL; for (curChar = szFile; *curChar != 0; curChar++) { if (*curChar == period) lastPeriod = curChar; } // No file extension? Definitely not valid then if (lastPeriod == NULL) return FALSE; // Extension size of 0 or greater than 7? Never heard of that format... dwExtSize = (DWORD)(curChar - lastPeriod - 1); if (!dwExtSize || dwExtSize > 7) { return FALSE; } // Save the extension and convert it to uppercase for comparison with our filter _tcsnccpy_s(szFileExt, lastPeriod + 1, dwExtSize + 1); // + 1 will copy the null terminator too for (curChar = szFileExt; *curChar != 0; curChar++) { *curChar = _totupper(*curChar); } // Yep, I'm re-using variables // Skip the description part of our filter to get to the actual extensions for (curChar = szFilter; *curChar != 0; curChar++); curChar += 3; // Skip to first character of the first extension lastPeriod = curChar - 1; // Compare each valid file extension against the one for our image for (; *curChar != 0; curChar++) { if (*curChar == TEXT(';')) { dwExtSize = (DWORD)(curChar - lastPeriod - 1); _tcsnccpy_s(szFilterExt, lastPeriod + 1, dwExtSize); szFilterExt[dwExtSize + 1] = 0; if (_tccmp(szFileExt, szFilterExt) == 0) { bIsValidExt = TRUE; break; } curChar += 3; // Skip the "*." after the semicolon in the list lastPeriod = curChar - 1; } } // Final comparison if there's no trailing semicolon if (*(curChar - 1) != TEXT(';') && !bIsValidExt) { dwExtSize = (DWORD)(curChar - lastPeriod - 1); _tcsnccpy_s(szFilterExt, lastPeriod + 1, dwExtSize); szFilterExt[dwExtSize + 1] = 0; if (_tccmp(szFileExt, szFilterExt) == 0) { bIsValidExt = TRUE; } } if (!bIsValidExt) return FALSE; // Finally, check if the file actually exists DWORD dwAttrib = GetFileAttributes(szFile); return dwAttrib != INVALID_FILE_ATTRIBUTES; }