| 1130 | static PDETOUR_EXE_HELPER s_pHelper = NULL; |
| 1131 | |
| 1132 | VOID CALLBACK DetourFinishHelperProcess(_In_ HWND, |
| 1133 | _In_ HINSTANCE, |
| 1134 | _In_ LPSTR, |
| 1135 | _In_ INT) |
| 1136 | { |
| 1137 | LPCSTR * rlpDlls = NULL; |
| 1138 | DWORD Result = 9900; |
| 1139 | DWORD cOffset = 0; |
| 1140 | DWORD cSize = 0; |
| 1141 | HANDLE hProcess = NULL; |
| 1142 | |
| 1143 | if (s_pHelper == NULL) { |
| 1144 | DETOUR_TRACE(("DetourFinishHelperProcess called with s_pHelper = NULL.\n")); |
| 1145 | Result = 9905; |
| 1146 | goto Cleanup; |
| 1147 | } |
| 1148 | |
| 1149 | hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, s_pHelper->pid); |
| 1150 | if (hProcess == NULL) { |
| 1151 | DETOUR_TRACE(("OpenProcess(pid=%lu) failed: %lu\n", |
| 1152 | s_pHelper->pid, GetLastError())); |
| 1153 | Result = 9901; |
| 1154 | goto Cleanup; |
| 1155 | } |
| 1156 | |
| 1157 | rlpDlls = new NOTHROW LPCSTR [s_pHelper->nDlls]; |
| 1158 | cSize = s_pHelper->cb - sizeof(DETOUR_EXE_HELPER); |
| 1159 | for (DWORD n = 0; n < s_pHelper->nDlls; n++) { |
| 1160 | size_t cchDest = 0; |
| 1161 | HRESULT hr = StringCchLengthA(&s_pHelper->rDlls[cOffset], cSize - cOffset, &cchDest); |
| 1162 | if (!SUCCEEDED(hr)) { |
| 1163 | Result = 9902; |
| 1164 | goto Cleanup; |
| 1165 | } |
| 1166 | |
| 1167 | rlpDlls[n] = &s_pHelper->rDlls[cOffset]; |
| 1168 | cOffset += (DWORD)cchDest + 1; |
| 1169 | } |
| 1170 | |
| 1171 | if (!DetourUpdateProcessWithDll(hProcess, rlpDlls, s_pHelper->nDlls)) { |
| 1172 | DETOUR_TRACE(("DetourUpdateProcessWithDll(pid=%lu) failed: %lu\n", |
| 1173 | s_pHelper->pid, GetLastError())); |
| 1174 | Result = 9903; |
| 1175 | goto Cleanup; |
| 1176 | } |
| 1177 | Result = 0; |
| 1178 | |
| 1179 | Cleanup: |
| 1180 | if (rlpDlls != NULL) { |
| 1181 | delete[] rlpDlls; |
| 1182 | rlpDlls = NULL; |
| 1183 | } |
| 1184 | |
| 1185 | // Note: s_pHelper is allocated as part of injecting the payload in DetourCopyPayloadToProcess(..), |
| 1186 | // it's a fake section and not data allocated by the system PE loader. |
| 1187 | |
| 1188 | // Delete the payload after execution to release the memory occupied by it |
| 1189 | if (s_pHelper != NULL) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…