summaryrefslogtreecommitdiff
path: root/inject
diff options
context:
space:
mode:
Diffstat (limited to 'inject')
-rw-r--r--inject/inject.h38
-rw-r--r--inject/injectee.cpp9
-rw-r--r--inject/injector.cpp4
3 files changed, 34 insertions, 17 deletions
diff --git a/inject/inject.h b/inject/inject.h
index e915e4f4..873160e5 100644
--- a/inject/inject.h
+++ b/inject/inject.h
@@ -94,18 +94,21 @@ getModuleName(char *szModuleName, size_t n, const char *szFilename) {
#define USE_SHARED_MEM 1
-#if USE_SHARED_MEM
+struct SharedMem
+{
+ BOOL bReplaced;
+ char szDllName[4096 - sizeof(BOOL)];
+};
-#define SHARED_MEM_SIZE 4096
-static LPVOID pSharedMem = NULL;
+static SharedMem *pSharedMem = NULL;
static HANDLE hFileMapping = NULL;
-static LPSTR
+static SharedMem *
OpenSharedMemory(void) {
if (pSharedMem) {
- return (LPSTR)pSharedMem;
+ return pSharedMem;
}
hFileMapping = CreateFileMapping(
@@ -113,7 +116,7 @@ OpenSharedMemory(void) {
NULL, // lpAttributes
PAGE_READWRITE, // read/write access
0, // dwMaximumSizeHigh
- SHARED_MEM_SIZE, // dwMaximumSizeLow
+ sizeof(SharedMem), // dwMaximumSizeLow
TEXT("injectfilemap")); // name of map object
if (hFileMapping == NULL) {
fprintf(stderr, "Failed to create file mapping\n");
@@ -122,7 +125,7 @@ OpenSharedMemory(void) {
BOOL bAlreadyExists = (GetLastError() == ERROR_ALREADY_EXISTS);
- pSharedMem = MapViewOfFile(
+ pSharedMem = (SharedMem *)MapViewOfFile(
hFileMapping,
FILE_MAP_WRITE, // read/write access
0, // dwFileOffsetHigh
@@ -134,10 +137,10 @@ OpenSharedMemory(void) {
}
if (!bAlreadyExists) {
- memset(pSharedMem, 0, SHARED_MEM_SIZE);
+ memset(pSharedMem, 0, sizeof *pSharedMem);
}
- return (LPSTR)pSharedMem;
+ return pSharedMem;
}
@@ -157,13 +160,15 @@ CloseSharedMem(void) {
static inline VOID
SetSharedMem(LPCSTR lpszSrc) {
- LPSTR lpszDst = OpenSharedMemory();
- if (!lpszDst) {
+ SharedMem *pSharedMem = OpenSharedMemory();
+ if (!pSharedMem) {
return;
}
+ LPSTR lpszDst = pSharedMem->szDllName;
+
size_t n = 1;
- while (*lpszSrc && n < SHARED_MEM_SIZE) {
+ while (*lpszSrc && n < sizeof pSharedMem->szDllName) {
*lpszDst++ = *lpszSrc++;
n++;
}
@@ -173,16 +178,15 @@ SetSharedMem(LPCSTR lpszSrc) {
static inline VOID
GetSharedMem(LPSTR lpszDst, size_t n) {
- LPCSTR lpszSrc = OpenSharedMemory();
- if (!lpszSrc) {
+ SharedMem *pSharedMem = OpenSharedMemory();
+ if (!pSharedMem) {
return;
}
+ LPCSTR lpszSrc = pSharedMem->szDllName;
+
while (*lpszSrc && --n) {
*lpszDst++ = *lpszSrc++;
}
*lpszDst = '\0';
}
-
-
-#endif /* USE_SHARED_MEM */
diff --git a/inject/injectee.cpp b/inject/injectee.cpp
index c60bc451..d7a192c7 100644
--- a/inject/injectee.cpp
+++ b/inject/injectee.cpp
@@ -211,6 +211,9 @@ replaceModule(HMODULE hModule,
} else {
LPVOID *lpOldAddress = (LPVOID *)(&pFirstThunk->u1.Function);
replaceAddress(lpOldAddress, (LPVOID)pNewProc);
+ if (pSharedMem) {
+ pSharedMem->bReplaced = TRUE;
+ }
}
++pOriginalFirstThunk;
@@ -452,6 +455,9 @@ MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName) {
assert(hModule != g_hThisModule);
for (unsigned i = 0; i < numReplacements; ++i) {
if (hModule == replacements[i].hReplaceModule) {
+ if (pSharedMem) {
+ pSharedMem->bReplaced = TRUE;
+ }
return GetProcAddress(hModule, lpProcName);
}
}
@@ -473,6 +479,9 @@ MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName) {
if (VERBOSITY >= 2) {
debugPrintf(" replacing %s!%s\n", szBaseName, lpProcName);
}
+ if (pSharedMem) {
+ pSharedMem->bReplaced = TRUE;
+ }
return pProcAddress;
} else {
if (VERBOSITY > 0) {
diff --git a/inject/injector.cpp b/inject/injector.cpp
index 0290760b..c5f017b5 100644
--- a/inject/injector.cpp
+++ b/inject/injector.cpp
@@ -302,6 +302,10 @@ main(int argc, char *argv[])
// Wait for it to finish
WaitForSingleObject(hProcess, INFINITE);
+ if (pSharedMem && !pSharedMem->bReplaced) {
+ fprintf(stderr, "warning: %s was never used: application probably does not use this API\n", szDll);
+ }
+
DWORD exitCode = ~0;
GetExitCodeProcess(hProcess, &exitCode);