Skip to content

Commit

Permalink
Merge pull request #9 from steeno/hotfix/issue-#6
Browse files Browse the repository at this point in the history
Fix issue #6
  • Loading branch information
steeno authored Nov 15, 2020
2 parents bb7f7c0 + 8342df7 commit 30ab03a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 25 additions & 3 deletions EU4ConsolePatcher/ModuleManager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "stdafx.h"
#include "ModuleManager.h"
#include "defines.h"

ModuleManager::ModuleManager(const DWORD& processId)
{
Expand Down Expand Up @@ -29,11 +30,32 @@ bool ModuleManager::FindModule(const wchar_t* moduleName, MODULEENTRY32& me32)

bool ModuleManager::UpdateModuleList()
{
HANDLE moduleSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, this->processId);
if (moduleSnapshot == INVALID_HANDLE_VALUE) {
DEBUG(L"CreateToolhelp32Snapshot failed: " << GetLastError());
HANDLE moduleSnapshot;
DWORD lastError;
BYTE retries = 0;

while (true) {
moduleSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, this->processId);
if (moduleSnapshot != INVALID_HANDLE_VALUE) {
break;
}
// If CTH32S failed with ERROR_PARTIAL_COPY error, try again
lastError = GetLastError();
if (lastError == ERROR_PARTIAL_COPY) {
++retries;
// If CTH32S failed to often with ERROR_PARTIAL_COPY error, cancel
if (retries > MAX_MODULE_SNAPSHOT_RETRY_COUNT) {
DEBUG(L"CreateToolhelp32Snapshot failed: " << lastError);
return false;
}
Sleep(1000);
continue;
}
// IF CTH32S failed with another error, cancel
DEBUG(L"CreateToolhelp32Snapshot failed: " << lastError);
return false;
}

MODULEENTRY32 me32;
me32.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(moduleSnapshot, &me32)) {
Expand Down
4 changes: 3 additions & 1 deletion EU4ConsolePatcher/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
#define MEMORY_SIGNATURE_B L"xxxxxxxxxxxxxxxxxxxxxxxxxxxx????xxxxxx"

#define TARGET_PROCESS_NAME L"eu4.exe"
#define TARGET_MODULE_NAME L"eu4.exe"
#define TARGET_MODULE_NAME L"eu4.exe"

#define MAX_MODULE_SNAPSHOT_RETRY_COUNT 10

0 comments on commit 30ab03a

Please sign in to comment.