| 74 | bool is_patched; |
| 75 | |
| 76 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 77 | { |
| 78 | if(fdwReason == DLL_PROCESS_ATTACH) |
| 79 | { |
| 80 | is_patched = false; |
| 81 | |
| 82 | if(GetModuleHandle(L"Audio Router.exe") != NULL) |
| 83 | return FALSE; |
| 84 | |
| 85 | hheap = HeapCreate(0, 0, 0); |
| 86 | patch_ntcreateuserprocess = (ntcreateuserprocess_patcher_t*) |
| 87 | HeapAlloc(hheap, 0, sizeof(ntcreateuserprocess_patcher_t)); |
| 88 | new (patch_ntcreateuserprocess) ntcreateuserprocess_patcher_t(ntcreateuserprocess_patch); |
| 89 | |
| 90 | if(!try_init_bootstrapper(patch_ntcreateuserprocess)) |
| 91 | return FALSE; |
| 92 | } |
| 93 | else if(fdwReason == DLL_THREAD_ATTACH) |
| 94 | { |
| 95 | // restore patch if it was reverted when audio router was closed |
| 96 | // (only if audio router is present again) |
| 97 | CHandle hfile(OpenMutexW(SYNCHRONIZE, FALSE, L"Local\\audio-router-mutex")); |
| 98 | if(hfile && !is_patched) |
| 99 | try_init_bootstrapper(patch_ntcreateuserprocess); |
| 100 | else if(!hfile && is_patched) |
| 101 | { |
| 102 | patch_ntcreateuserprocess->lock(); |
| 103 | patch_ntcreateuserprocess->revert(); |
| 104 | is_patched = false; |
| 105 | patch_ntcreateuserprocess->unlock(); |
| 106 | } |
| 107 | } |
| 108 | else if(fdwReason == DLL_PROCESS_DETACH) |
| 109 | { |
| 110 | // TODO: remove |
| 111 | patch_ntcreateuserprocess->lock(); |
| 112 | patch_ntcreateuserprocess->revert(); |
| 113 | is_patched = false; |
| 114 | patch_ntcreateuserprocess->unlock(); |
| 115 | } |
| 116 | |
| 117 | return TRUE; |
| 118 | } |
| 119 | |
| 120 | NTSTATUS NTAPI ntcreateuserprocess_patch( |
| 121 | PHANDLE ProcessHandle, |
nothing calls this directly
no test coverage detected