MCPcopy Create free account
hub / github.com/RevoSucks/BM64Recomp / preload_executable

Function preload_executable

src/main/main.cpp:418–492  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

416};
417
418bool preload_executable(PreloadContext& context) {
419 wchar_t module_name[MAX_PATH];
420 GetModuleFileNameW(NULL, module_name, MAX_PATH);
421
422 context.handle = CreateFileW(module_name, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
423 if (context.handle == INVALID_HANDLE_VALUE) {
424 fprintf(stderr, "Failed to load executable into memory!");
425 context = {};
426 return false;
427 }
428
429 LARGE_INTEGER module_size;
430 if (!GetFileSizeEx(context.handle, &module_size)) {
431 fprintf(stderr, "Failed to get size of executable!");
432 CloseHandle(context.handle);
433 context = {};
434 return false;
435 }
436
437 context.size = module_size.QuadPart;
438
439 context.mapping_handle = CreateFileMappingW(context.handle, nullptr, PAGE_READONLY, 0, 0, nullptr);
440 if (context.mapping_handle == nullptr) {
441 fprintf(stderr, "Failed to create file mapping of executable!");
442 CloseHandle(context.handle);
443 context = {};
444 return EXIT_FAILURE;
445 }
446
447 context.view = MapViewOfFile(context.mapping_handle, FILE_MAP_READ, 0, 0, 0);
448 if (context.view == nullptr) {
449 fprintf(stderr, "Failed to map view of of executable!");
450 CloseHandle(context.mapping_handle);
451 CloseHandle(context.handle);
452 context = {};
453 return false;
454 }
455
456 DWORD pid = GetCurrentProcessId();
457 HANDLE process_handle = OpenProcess(PROCESS_SET_QUOTA | PROCESS_QUERY_INFORMATION, FALSE, pid);
458 if (process_handle == nullptr) {
459 fprintf(stderr, "Failed to open own process!");
460 CloseHandle(context.mapping_handle);
461 CloseHandle(context.handle);
462 context = {};
463 return false;
464 }
465
466 SIZE_T minimum_set_size, maximum_set_size;
467 if (!GetProcessWorkingSetSize(process_handle, &minimum_set_size, &maximum_set_size)) {
468 fprintf(stderr, "Failed to get working set size!");
469 CloseHandle(context.mapping_handle);
470 CloseHandle(context.handle);
471 context = {};
472 return false;
473 }
474
475 if (!SetProcessWorkingSetSize(process_handle, minimum_set_size + context.size, maximum_set_size + context.size)) {

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected