In an attempt to fix the problem, I updated madCHook.dll to the one included in version 2.5.0.15. It did not help. I changed the code to add more debugging messages after calling InjectLibrary. GetLastError() returns 122 which is the code for ERROR_INSUFFICIENT_BUFFER. I need help on how to fix it.
My real problem is that my customed dll is supposed to be hooked into current and future instances of target application. It appears that it's only hooked for the current instances. When I started a new instance of the target application, the hook does not seem to be there. I think it's due to the error with InjectLibrary I mentioned above. A portion of the code is copied below
Code: Select all
#include "stdafx.h"
#define PRINTFILE_TEMP "C:\\JTI\\PrintFile\\Temp"
#define PRINTFILE_INQUEUE "C:\\JTi\\PrintFile\\InQueue\\"
#define ENDFILE "end.end" // Use to end the application
#define INJECTSERVICE "NfuMonSvc" // Name of injection service
#define INJECTDLL "NfuMonApi.dll" // Name of injection dll
typedef struct
// this is the information record which we receive via Ipc
TDllInjectRequest {
CHAR cAction; // 'I' = InjectDLL; 'U' = UninjectDll; 'R' = Remove the service
CHAR szModuleName [MAX_PATH + 1]; // Name of DLL
DWORD dwTimeOut;
DWORD dwSession;
...
...
//-------------------------------------------------------------------------
// Purpose: Inject the DLL into system wide process
//-------------------------------------------------------------------------
BOOL Inject(CHAR cAction)
// (un)inject our dll system wide
{
TDllInjectRequest dir;
BOOL res;
BOOL result;
char cMsg[200];
DWORD wError;
// first let's try to inject the dlls without the help of the service
if (cAction == 'I')
{
result = InjectLibrary(ALL_SESSIONS | SYSTEM_PROCESSES, INJECTDLL, 10000);
wError = GetLastError ();
sprintf (cMsg, "Injection result %d, error = %d", result, wError);
// This shows result = -1 and error = 122
MessageBox (NULL, cMsg, "NfuDirMon", MB_OK);
}
else result = UninjectLibrary(ALL_SESSIONS | SYSTEM_PROCESSES, INJECTDLL, 10000);
if (!result && osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
// didn't work, so let's try to ask our service for help
// first of all we wait until the service is ready to go
MessageBox (NULL, "Asking service to inject/uninject", "NfuDirMon", MB_OK);
WaitForService(INJECTSERVICE);
// then we prepare a dll injection request record
dir.cAction = cAction;
dir.dwTimeOut = 5000;
dir.dwSession = GetCurrentSessionId();
strcpy (dir.szModuleName, INJECTDLL);
// now we try to contact our injection service
result = SendIpcMessage(INJECTSERVICE, &dir, sizeof(dir), &res, sizeof(res), 15000, true) && res;
}
return result;
}
...
Thanks,
Patrick