/** Create a new process and wait for it to complete (OP_CREATE_PROCESS opcode handler) */ BOOL OpCreateProcess(LPVOID *p) { LPTSTR ApplicationName; LPTSTR CommandLine; GetCreateProcessInfo(p, &ApplicationName, &CommandLine); CreateAndWaitForProcess(ApplicationName, CommandLine); LocalFree(ApplicationName); LocalFree(CommandLine); return TRUE; }
int CALLBACK _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { /* Find name of image */ if (!GetModuleFileName(NULL, ImageFileName, MAX_PATH)) { FATAL("Failed to get executable name (error %lu).", GetLastError()); return -1; } /* Set up environment */ SetEnvironmentVariable(_T("OCRA_EXECUTABLE"), ImageFileName); SetConsoleCtrlHandler(&ConsoleHandleRoutine, TRUE); /* Open the image (executable) */ HANDLE hImage = CreateFile(ImageFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hImage == INVALID_HANDLE_VALUE) { FATAL("Failed to open executable (%s)", ImageFileName); return -1; } /* Create a file mapping */ DWORD FileSize = GetFileSize(hImage, NULL); HANDLE hMem = CreateFileMapping(hImage, NULL, PAGE_READONLY, 0, FileSize, NULL); if (hMem == INVALID_HANDLE_VALUE) { FATAL("Failed to create file mapping (error %lu)", GetLastError()); CloseHandle(hImage); return -1; } /* Map the image into memory */ LPVOID lpv = MapViewOfFile(hMem, FILE_MAP_READ, 0, 0, 0); if (lpv == NULL) { FATAL("Failed to map view of executable into memory (error %lu).", GetLastError()); } else { if (!ProcessImage(lpv, FileSize)) ExitStatus = -1; if (!UnmapViewOfFile(lpv)) FATAL("Failed to unmap view of executable."); } if (!CloseHandle(hMem)) FATAL("Failed to close file mapping."); if (!CloseHandle(hImage)) FATAL("Failed to close executable."); if (PostCreateProcess_ApplicationName && PostCreateProcess_CommandLine) { DEBUG("**********"); DEBUG("Starting app in: %s", InstDir); DEBUG("**********"); CreateAndWaitForProcess(PostCreateProcess_ApplicationName, PostCreateProcess_CommandLine); } if (DeleteInstDirEnabled) { DEBUG("Deleting temporary installation directory %s", InstDir); SHFILEOPSTRUCT shop; shop.hwnd = NULL; shop.wFunc = FO_DELETE; InstDir[lstrlen(InstDir) + sizeof(TCHAR)] = 0; shop.pFrom = InstDir; shop.pTo = NULL; shop.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI; SHFileOperation(&shop); } ExitProcess(ExitStatus); /* Never gets here */ return 0; }
int CALLBACK _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { TCHAR TempPath[MAX_PATH]; GetTempPath(MAX_PATH, TempPath); GetTempFileName(TempPath, _T("ocrastub"), 0, InstDir); DEBUG("Temporary directory: %s\n", InstDir); SetConsoleCtrlHandler(&ConsoleHandleRoutine, TRUE); /* Attempt to delete the temp file created by GetTempFileName. Ignore errors, i.e. if it doesn't exist. */ (void)DeleteFile(InstDir); /* Create the temporary directory that will hold the extracted files */ if (!CreateDirectory(InstDir, NULL)){ FATAL("Failed to create temporary directory.\n"); return -1; } /* Find name of image */ TCHAR ImageFileName[MAX_PATH]; if (!GetModuleFileName(NULL, ImageFileName, MAX_PATH)) { FATAL("Failed to get executable name (error %lu).\n", GetLastError()); return -1; } /* Set up environment */ SetEnvironmentVariable(_T("OCRA_EXECUTABLE"), ImageFileName); /* Open the image (executable) */ HANDLE hImage = CreateFile(ImageFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hImage == INVALID_HANDLE_VALUE) { FATAL("Failed to open executable (%s)\n", ImageFileName); return -1; } /* Create a file mapping */ DWORD FileSize = GetFileSize(hImage, NULL); HANDLE hMem = CreateFileMapping(hImage, NULL, PAGE_READONLY, 0, FileSize, NULL); if (hMem == INVALID_HANDLE_VALUE) { FATAL("Failed to create file mapping (error %lu)\n", GetLastError()); CloseHandle(hImage); return -1; } /* Map the image into memory */ LPVOID lpv = MapViewOfFile(hMem, FILE_MAP_READ, 0, 0, 0); if (lpv == NULL) { FATAL("Failed to map view of executable into memory (error %lu).\n", GetLastError()); } else { if (!ProcessImage(lpv, FileSize)) ExitStatus = -1; if (!UnmapViewOfFile(lpv)) FATAL("Failed to unmap view of executable.\n"); } if (!CloseHandle(hMem)) FATAL("Failed to close file mapping.\n"); if (!CloseHandle(hImage)) FATAL("Failed to close executable.\n"); if (PostCreateProcess_ApplicationName && PostCreateProcess_CommandLine) { CreateAndWaitForProcess(PostCreateProcess_ApplicationName, PostCreateProcess_CommandLine); } /* Remove the temporary directory */ SHFILEOPSTRUCT shop; shop.hwnd = NULL; shop.wFunc = FO_DELETE; InstDir[lstrlen(InstDir) + sizeof(TCHAR)] = 0; shop.pFrom = InstDir; shop.pTo = NULL; shop.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI; SHFileOperation(&shop); DEBUG("Removing temporary files\n"); ExitProcess(ExitStatus); /* Never gets here */ return 0; }