Skip to content

Commit 57172d0

Browse files
Fix createwith failure when run against some protected binaries (microsoft#140)
Fix can not use createwith api to start some unnormal exe, like chinese online, commercial, protected game dnf.exe https://dnf.qq.com/, but this exe can start run by explorer.
1 parent e64fe0f commit 57172d0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/uimports.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
106106
}
107107
}
108108

109+
if (inh.IMPORT_DIRECTORY.VirtualAddress != NULL && inh.IMPORT_DIRECTORY.Size == 0) {
110+
111+
// Don't worry about changing the PE file,
112+
// because the load information of the original PE header has been saved and will be restored.
113+
// The change here is just for the following code to work normally
114+
115+
PIMAGE_IMPORT_DESCRIPTOR pImageImport = (PIMAGE_IMPORT_DESCRIPTOR)(pbModule + inh.IMPORT_DIRECTORY.VirtualAddress);
116+
117+
do {
118+
IMAGE_IMPORT_DESCRIPTOR ImageImport;
119+
if (!ReadProcessMemory(hProcess, pImageImport, &ImageImport, sizeof(ImageImport), NULL)) {
120+
DETOUR_TRACE(("ReadProcessMemory failed: %u\n", GetLastError()));
121+
goto finish;
122+
}
123+
inh.IMPORT_DIRECTORY.Size += sizeof(IMAGE_IMPORT_DESCRIPTOR);
124+
if (!ImageImport.Name) {
125+
break;
126+
}
127+
++pImageImport;
128+
} while (TRUE);
129+
130+
DWORD dwLastError = GetLastError();
131+
OutputDebugString(TEXT("[This PE file has an import table, but the import table size is marked as 0. This is an error.")
132+
TEXT("If it is not repaired, the launched program will not work properly, Detours has automatically repaired its import table size for you! ! !]\r\n"));
133+
if (GetLastError() != dwLastError) {
134+
SetLastError(dwLastError);
135+
}
136+
}
137+
109138
DETOUR_TRACE((" Imports: %p..%p\n",
110139
(DWORD_PTR)pbModule + inh.IMPORT_DIRECTORY.VirtualAddress,
111140
(DWORD_PTR)pbModule + inh.IMPORT_DIRECTORY.VirtualAddress +

0 commit comments

Comments
 (0)