MCPcopy Create free account
hub / github.com/bwapi/bwapi / ApplyPatchSuspended

Function ApplyPatchSuspended

bwapi/BWAPI_PluginInjector/chaoslauncher.cpp:50–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}
49
50extern "C" __declspec(dllexport) bool ApplyPatchSuspended(HANDLE hProcess, DWORD)
51{
52 // Get target file name
53 char szTarget[MAX_PATH];
54 strncpy(szTarget, GetBWAPITarget().c_str(), MAX_PATH-1);
55 szTarget[MAX_PATH-1] = '\0';
56
57 // Check if the file exists, INVALID_FILE_ATTRIBUTES will have this bit set too
58 if ( GetFileAttributesA(GetBWAPITarget().c_str()) & FILE_ATTRIBUTE_DIRECTORY )
59 return BWAPIError("Unable to find %s.", szTarget);
60
61 // Get the address for the LoadLibrary procedure
62 HMODULE hKernalModule = GetModuleHandle(L"Kernel32");
63 if ( !hKernalModule )
64 return BWAPIError("Unable to get module handle for Kernel32.");
65
66 LPTHREAD_START_ROUTINE loadLibAddress = (LPTHREAD_START_ROUTINE)GetProcAddress(hKernalModule, "LoadLibraryA" );
67 if ( !loadLibAddress )
68 return BWAPIError("Could not get Proc Address for LoadLibraryA.");
69
70 // Create a remote allocation
71 VAlloc alloc(hProcess, MAX_PATH);
72 if ( !alloc )
73 return BWAPIError("Could not allocate memory for DLL path.");
74
75 // Write the DLL path to the allocation
76 if ( !alloc.Write(szTarget, MAX_PATH) )
77 return BWAPIError("Write process memory failed.");
78
79 // Create a remote thread for LoadLibrary and pass the DLL path as a parameter
80 RemoteThread thread(hProcess, loadLibAddress, alloc.GetAddress());
81 if ( !thread )
82 return BWAPIError("Unable to create remote thread.");
83
84 // Wait for the thread to finish
85 if ( !thread.Wait() )
86 return BWAPIError("WaitForSingleObject failed.");
87
88 // The exit code for LoadLibrary is its return value, if it's NULL then loading failed
89 if ( thread.GetExitCode() == NULL )
90 return BWAPIError("Injection failed.\nThis is caused when BWAPI crashes before injecting completely.");
91
92 return true; //everything OK
93}

Callers

nothing calls this directly

Calls 7

GetBWAPITargetFunction · 0.85
c_strMethod · 0.80
WriteMethod · 0.80
GetAddressMethod · 0.80
GetExitCodeMethod · 0.80
BWAPIErrorFunction · 0.70
WaitMethod · 0.45

Tested by

no test coverage detected