InjectDLL for UNICODE support.

Wrote new code? Fixed a bug? Want to discuss technical stuff? Feel free to post it here.

Moderator: Moderators

UltimaWeapon
Human
Human
Posts: 37
Joined: 04 Apr 2008, 22:55
Noob?: Yes
Location: Thailand

InjectDLL for UNICODE support.

#1 Post by UltimaWeapon »

Now, InjectDLL function can use UTF-8 as DLL path parameter. It compile successfully under MSVC 6.0. But I'm not already tested. Need someone test it. I think it do not work under Win9X. Because 'LoadLibraryW' may not supported in Win9X.

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 {
I may make you misunderstand. Because my English isn't good enough. So Sorry.
Image
Hiden
Noob
Noob
Posts: 1
Joined: 31 Oct 2008, 23:00
Noob?: Yes

Re: InjectDLL for UNICODE support.

#2 Post by Hiden »

works perfect, but until you get to / map load / sgt reaches the error

Stack trace:
Assertion ('HASH(0x38405ec)' must be of class 'Actor::You') failed!
at D:/openkore-2.0.5.1-win32/openkore-2.0.6.1-win32/openkore-2.0.6.1/src/deps/Carp/Assert.pm line 271
Carp::Assert::assert('', '\'HASH(0x38405ec)\' must be of class \'Actor::You\'') called at src/Utils/Assert.pm line 31
Utils::Assert::assertClass('HASH(0x38405ec)', 'Actor::You') called at src/Misc.pm line 228
Misc::checkValidity('Packet: public_chat') called at src/Network/Receive.pm line 419
Network::Receive::parse('Network::Receive::ServerType8_4=HASH(0x1c4f8a0)', '\x{8d}\x{0}\x{14}\x{0}\x{ce}\x{8c}\x{1e}\x{0}Reisha : xd\x{0}') called at src/functions.pl line 1192
main::parseIncomingMessage('\x{8d}\x{0}\x{14}\x{0}\x{ce}\x{8c}\x{1e}\x{0}Reisha : xd\x{0}') called at src/functions.pl line 545
main::mainLoop_initialized() called at src/functions.pl line 70
main::mainLoop() called at src/Interface.pm line 75
Interface::mainLoop('Interface::Console::Win32=HASH(0x29c9308)') called at openkore.pl line 96
main::__start() called at start.pl line 119
at D:/openkore-2.0.5.1-win32/openkore-2.0.6.1-win32/openkore-2.0.6.1/src/deps/Carp/Assert.pm line 271
Carp::Assert::assert('', '\'HASH(0x38405ec)\' must be of class \'Actor::You\'') called at src/Utils/Assert.pm line 31
Utils::Assert::assertClass('HASH(0x38405ec)', 'Actor::You') called at src/Misc.pm line 228
Misc::checkValidity('Packet: public_chat') called at src/Network/Receive.pm line 419
Network::Receive::parse('Network::Receive::ServerType8_4=HASH(0x1c4f8a0)', '\x{8d}\x{0}\x{14}\x{0}\x{ce}\x{8c}\x{1e}\x{0}Reisha : xd\x{0}') called at src/functions.pl line 1192
main::parseIncomingMessage('\x{8d}\x{0}\x{14}\x{0}\x{ce}\x{8c}\x{1e}\x{0}Reisha : xd\x{0}') called at src/functions.pl line 545
main::mainLoop_initialized() called at src/functions.pl line 70
main::mainLoop() called at src/Interface.pm line 75
Interface::mainLoop('Interface::Console::Win32=HASH(0x29c9308)') called at openkore.pl line 96
main::__start() called at start.pl line 119
UltimaWeapon
Human
Human
Posts: 37
Joined: 04 Apr 2008, 22:55
Noob?: Yes
Location: Thailand

Re: InjectDLL for UNICODE support.

#3 Post by UltimaWeapon »

Your error probably not from this patch.
I may make you misunderstand. Because my English isn't good enough. So Sorry.
Image
VCL
Administrator
Administrator
Posts: 11
Joined: 04 Apr 2008, 09:24

Re: InjectDLL for UNICODE support.

#4 Post by VCL »

It's 2008. Why do you still care about Win9x? There's absolutely no reason anymore to use a pre-NT Windows version.
UltimaWeapon
Human
Human
Posts: 37
Joined: 04 Apr 2008, 22:55
Noob?: Yes
Location: Thailand

Re: InjectDLL for UNICODE support.

#5 Post by UltimaWeapon »

OK. This is another version with LoadLibrary Error checking.

How it work?

GetExitCodeThread using for retrieve value that returned by LoabLibrary function.
You do not have the required permissions to view the files attached to this post.
I may make you misunderstand. Because my English isn't good enough. So Sorry.
Image