Code: Select all
Index: src/auto/XSTools/win32/utils.cpp
===================================================================
--- src/auto/XSTools/win32/utils.cpp (revision 6600)
+++ src/auto/XSTools/win32/utils.cpp (working copy)
@@ -97,8 +97,11 @@
}
bool
-InjectDLL (DWORD ProcID, const char *dll)
+InjectDLL(DWORD ProcID, const char *dll, int dlllen)
{
+ WCHAR *unicode;
+ DWORD dwMemLen;
+
#define TESTING_INJECT9x 0
#ifdef TESTING_INJECT9x
#define debug(x) MessageBox(0, x, "Debug", 0)
@@ -107,11 +110,15 @@
#endif
init ();
+
+ unicode = utf8ToWidechar(dll, dlllen);
+ dwMemLen = (unicode) ? ((lstrlenW(unicode) + 1) * sizeof(WCHAR)) : (strlen(dll) + 1);
+
if (TESTING_INJECT9x || !isNT) {
HMODULE lib;
int i;
HWND hwnd;
- typedef int WINAPI __declspec(dllexport) (*injectSelfFunc) (HWND hwnd);
+ typedef int (WINAPI *injectSelfFunc) (HWND hwnd);
injectSelfFunc injectSelf;
// The window may not appear immediately so we try for at least 5 seconds
@@ -124,12 +131,16 @@
}
if (!hwnd) {
debug ("No RO window found.");
+ if (unicode)
+ free(unicode);
return false;
}
- lib = LoadLibrary (dll);
+ lib = (unicode) ? LoadLibraryW(unicode) : LoadLibraryA(dll);
if (!lib) {
debug ("Could not load library.");
+ if (unicode)
+ free(unicode);
return false;
}
@@ -137,34 +148,43 @@
if (!injectSelf) {
debug ("No injectSelf() function.");
FreeLibrary (lib);
+ if (unicode)
+ free(unicode);
return false;
}
injectSelf (hwnd);
+
+ if (unicode)
+ free(unicode);
+
return true;
}
/* Attach to ragexe */
HANDLE hProcessToAttach = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcID);
- if (!hProcessToAttach) {
+ if (!hProcessToAttach)
+ {
+ if (unicode)
+ free(unicode);
return false;
}
LPVOID pAttachProcessMemory = NULL;
DWORD dwBytesWritten = 0;
- char * dllRemove;
/* Allocate a piece of memory in ragexe. */
- dllRemove = (char*)calloc(strlen(dll) + 1, 1);
pAttachProcessMemory = VirtualAllocEx(
hProcessToAttach,
NULL,
- strlen(dll) + 1,
+ dwMemLen,
MEM_COMMIT,
PAGE_EXECUTE_READWRITE );
if (!pAttachProcessMemory) {
CloseHandle(hProcessToAttach);
+ if (unicode)
+ free(unicode);
return false;
}
@@ -172,46 +192,55 @@
WriteProcessMemory(
hProcessToAttach,
pAttachProcessMemory,
- (LPVOID)dll, strlen(dll) + 1,
+ (unicode) ? ((LPVOID)unicode) : ((LPVOID)dll), dwMemLen,
&dwBytesWritten );
- if (!dwBytesWritten) {
+ if (!dwBytesWritten)
+ {
+ VirtualFreeEx(
+ hProcessToAttach,
+ pAttachProcessMemory,
+ dwMemLen,
+ MEM_RELEASE);
+ if (unicode)
+ free(unicode);
return false;
}
-
/* Create a remote thread in the ragexe.exe process, which
calls LoadLibraryA(our DLL filename) */
HMODULE kDLL = GetModuleHandle("Kernel32");
HANDLE hThread = CreateRemoteThread( hProcessToAttach, NULL, 0,
- (LPTHREAD_START_ROUTINE)GetProcAddress(kDLL, "LoadLibraryA"),
+ (LPTHREAD_START_ROUTINE)GetProcAddress(kDLL, (unicode) ? "LoadLibraryW" : "LoadLibraryA"),
(LPVOID)pAttachProcessMemory, 0,
NULL);
- if (!hThread) {
+ if (!hThread)
+ {
+ VirtualFreeEx(
+ hProcessToAttach,
+ pAttachProcessMemory,
+ dwMemLen,
+ MEM_RELEASE);
+ if (unicode)
+ free(unicode);
return false;
}
WaitForSingleObject(hThread, INFINITE);
/* Free the string we created */
- WriteProcessMemory(
- hProcessToAttach,
- pAttachProcessMemory,
- (LPVOID)dllRemove, strlen(dll) + 1,
- &dwBytesWritten );
-
- if (!dwBytesWritten) {
- return false;
- }
VirtualFreeEx(
hProcessToAttach,
pAttachProcessMemory,
- strlen(dll) + 1,
+ dwMemLen,
MEM_RELEASE);
+
+ CloseHandle(hThread);
+ CloseHandle(hProcessToAttach);
- if (hThread) {
- CloseHandle(hThread);
- }
+ if (unicode)
+ free(unicode);
+
return true;
}
Index: src/auto/XSTools/win32/utils.h
===================================================================
--- src/auto/XSTools/win32/utils.h (revision 6600)
+++ src/auto/XSTools/win32/utils.h (working copy)
@@ -8,9 +8,10 @@
*
* @param ProcID A process ID.
* @param dll The DLL's filename.
+ * @param dlllen The length of dll, in bytes.
* @return Whether the injection succeeded.
*/
-bool InjectDLL (DWORD ProcID, const char *dll);
+bool InjectDLL(DWORD ProcID, const char *dll, int dlllen);
/**
* Find the process ID of a process with the given name.
Index: src/auto/XSTools/win32/wrapper.xs
===================================================================
--- src/auto/XSTools/win32/wrapper.xs (revision 6600)
+++ src/auto/XSTools/win32/wrapper.xs (working copy)
@@ -19,8 +19,21 @@
bool
InjectDLL(ProcID, dll)
unsigned long ProcID
- char *dll
+ SV *dll
+INIT:
+ RETVAL = 0;
+CODE:
+ if (dll && SvOK(dll)) {
+ char *fullpath;
+ STRLEN len;
+ fullpath = SvPV(dll, len);
+ if (fullpath)
+ RETVAL = InjectDLL(ProcID, fullpath, len);
+ }
+OUTPUT:
+ RETVAL
+
int
ShellExecute(handle, operation, file)
unsigned int handle
@@ -154,14 +167,14 @@
unsigned long lpAddr
SV *svData
INIT:
- LPCVOID lpBuffer;
+ LPVOID lpBuffer;
STRLEN dwSize;
DWORD bytesWritten;
CODE:
if (0 == SvPOK(svData)) {
RETVAL = 0;
} else {
- lpBuffer = (LPCVOID) SvPV(svData, dwSize);
+ lpBuffer = (LPVOID) SvPV(svData, dwSize);
if (0 == WriteProcessMemory((HANDLE)ProcHND, (LPVOID)lpAddr, lpBuffer, (SIZE_T)dwSize, (SIZE_T*)&bytesWritten)) {
RETVAL = 0;
} else {



