MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / InjectDllForProcess

Function InjectDllForProcess

emmy_tool/src/windows/utility.cpp:337–384  ·  view source on GitHub ↗

和InjectDll 实现不同的是,不会closeHandle

Source from the content-addressed store, hash-verified

335
336// 和InjectDll 实现不同的是,不会closeHandle
337bool InjectDllForProcess(HANDLE hProcess, const char *dllDir, const char *dllFileName) {
338 bool success = true;
339 DWORD exitCode = 0;
340
341 // Set dll directory
342 void *dllDirRemote = RemoteDup(hProcess, dllDir, strlen(dllDir) + 1);
343 ExecuteRemoteKernelFuntion(hProcess, "SetDllDirectoryA", dllDirRemote, exitCode);
344 VirtualFreeEx(hProcess, dllDirRemote, 0, MEM_RELEASE);
345
346 // Load the DLL.
347 void *remoteFileName = RemoteDup(hProcess, dllFileName, strlen(dllFileName) + 1);
348 success &= ExecuteRemoteKernelFuntion(hProcess, "LoadLibraryA", remoteFileName, exitCode);
349 VirtualFreeEx(hProcess, remoteFileName, 0, MEM_RELEASE);
350 if (!success || exitCode == 0) {
351 MessageEvent("Failed to load library", MessageType_Error);
352 return false;
353 }
354
355 // Read shared data & call 'StartupHookMode()'
356 TSharedData data;
357 if (ReadSharedData(data)) {
358 DWORD threadId;
359 HANDLE thread = CreateRemoteThread(hProcess,
360 nullptr,
361 0,
362 (LPTHREAD_START_ROUTINE) data.lpInit,
363 nullptr,
364 0,
365 &threadId);
366
367 if (thread != nullptr) {
368 WaitForSingleObject(thread, INFINITE);
369 GetExitCodeThread(thread, &exitCode);
370 CloseHandle(thread);
371 success = true;
372 } else {
373 success = false;
374 }
375 }
376
377 // Reset dll directory
378 ExecuteRemoteKernelFuntion(hProcess, "SetDllDirectoryA", nullptr, exitCode);
379
380 if (success) {
381 MessageEvent("Successfully inject dll!");
382 }
383 return success;
384}
385
386
387void ReceiveLog(DWORD processId) {

Callers 1

LaunchMethod · 0.85

Calls 4

RemoteDupFunction · 0.85
MessageEventFunction · 0.85
ReadSharedDataFunction · 0.85

Tested by

no test coverage detected