FWP
Legacy Member
Ik loop aan een dood einde denk ik. De code volgt hieronder. Ik heb de belangrijke delen omtrent deze discussie in het vet groen gezet.
Als ik geen TCHAR gebruik, en dus char voor die dllFileName variable dan werkt het programma, ook als ik het programma naar Multi-Byte Character encoding zet.
Maar dan is mijn programma niet meer UNICODE conform. Ik moet een manier vinden om dit op het laatste punt om te zetten (in die WriteProcessMemory functie), maar tot nu toe is me dat nog niet gelukt.
Code:
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/////// Name: Worms: Reloaded Windowed Loader v2.0
/////// Description: Allows you to run the game in windowed mode flawlessly
/////// Author: Frederique W. Piccart
/////// Email: [email protected]
/////// URL: http://forums.steampowered.com/forums/showthread.php?p=16796532
/////// License: GPLv3 (or later)
///////
/////// This program is free software: you can redistribute it and/or modify
/////// it under the terms of the GNU General Public License as published by
/////// the Free Software Foundation, either version 3 of the License, or
/////// (at your option) any later version.
///////
/////// This program is distributed in the hope that it will be useful,
/////// but WITHOUT ANY WARRANTY; without even the implied warranty of
/////// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/////// GNU General Public License for more details.
///////
/////// You should have received a copy of the GNU General Public License
/////// along with this program. If not, see <http://www.gnu.org/licenses/>.
///////
//////// Contributors (see changelog in readme.txt for exact details):
//////// Name: CyberShadow
//////// Email: [email protected]
//////// Website: http://thecybershadow.net/
//////// Profile: http://forums.steampowered.com/forums/member.php?u=595488
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// Definitions definitions
#ifdef UNICODE
#define LoadLibraryGeneric "LoadLibraryW"
#else
#define LoadLibraryGeneric "LoadLibraryA"
#endif
// Dependencies
#pragma comment(lib,"advapi32")
#pragma comment(lib,"shell32")
#pragma comment(lib,"user32")
// Library inclusion
#include <direct.h>
#include <tchar.h>
#include <windows.h>
#include <tlhelp32.h>
// General project properties
const TCHAR gameName[] = _T("Worms: Reloaded");
const TCHAR gameExecutable[MAX_PATH] = _T("steam://rungameid/22600"); // The initial executable can sometimes differ from the actual game process
const TCHAR gameProcess[MAX_PATH] = _T("WormsReloaded.exe");
[COLOR="PaleGreen"][B]const TCHAR dllFileName[MAX_PATH] = _T("WormsReloadedWindowedDll.dll");[/B][/COLOR]
const unsigned int injectTimeout = 10000; // Amount of time in miliseconds before the loader will stop looking for the process to inject
// Function to retrieve the process handle
HANDLE GetProcessHandle(LPCTSTR szExeName);
// The function that injects the dynamic link library
[COLOR="PaleGreen"][B]bool DllInject(HANDLE hProcess, LPCTSTR lpszDllPath);[/B][/COLOR]
// Main entry point of the loader
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Open the game
ShellExecute(0, _T("open"), gameExecutable, 0, 0, SW_SHOWNORMAL);
// Look for the game process
HANDLE hProcess;
do
{
hProcess = GetProcessHandle(gameProcess);
Sleep(1);
}while(hProcess == 0);
[COLOR="PaleGreen"][B] // Obtain the full path of the dynamic link library to pass on
TCHAR location[MAX_PATH] = _T("");
_tgetcwd(location, MAX_PATH);
_tcscat_s(location, _T("\\"));
_tcscat_s(location, dllFileName);[/B][/COLOR]
// Error message
TCHAR errorMessage[300] = _T("");
_tcscat_s(errorMessage, _T("Failed to inject the dynamic link library into "));
_tcscat_s(errorMessage, gameName);
_tcscat_s(errorMessage, _T(". Do you wish to start the game normally?"));
[COLOR="PaleGreen"] [B]if(!DllInject(hProcess, location))[/B][/COLOR]
{
// Injection failure, stop the game and ask the user whether to run the game normally
TerminateProcess(hProcess, 0);
if(IDYES == MessageBox(0, errorMessage, _T("Loader Injection Failure"), MB_YESNO | MB_ICONERROR))
{
ShellExecute(0, _T("open"), gameExecutable, 0, 0, SW_SHOWNORMAL);
}
}
return 0;
}
// Injection process
[COLOR="PaleGreen"][B]bool DllInject(HANDLE hProcess, LPCTSTR lpszDllPath)[/B][/COLOR]
{
HMODULE hmKernel = GetModuleHandle(_T("kernel32"));
if(hmKernel == 0 || hProcess == 0)
{
// Unable to proceed with injection due to lack of necessary information
return false;
}
[COLOR="PaleGreen"][B] int nPathLength = _tcsclen(lpszDllPath) + 1;[/B][/COLOR]
LPVOID lpvMemory = VirtualAllocEx(hProcess, 0, nPathLength, MEM_COMMIT, PAGE_READWRITE);
[COLOR="PaleGreen"][B] WriteProcessMemory(hProcess, lpvMemory, lpszDllPath, nPathLength, 0);[/B][/COLOR]
DWORD dwWaitResult, dwExitResult = 0;
HANDLE hThread = CreateRemoteThread(hProcess, 0, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(hmKernel, LoadLibraryGeneric), lpvMemory, 0, 0);
if(hThread != 0)
{
dwWaitResult = WaitForSingleObject(hThread, injectTimeout);
GetExitCodeThread(hThread, &dwExitResult);
CloseHandle(hThread);
}
VirtualFreeEx(hProcess, lpvMemory, 0, MEM_RELEASE);
return ((dwWaitResult != WAIT_TIMEOUT) && (dwExitResult > 0));
}
// Retrieving the process handle
HANDLE GetProcessHandle(LPCTSTR szExeName)
{
PROCESSENTRY32 pEntry = { sizeof(PROCESSENTRY32) };
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
HANDLE hProcess = 0;
if(Process32First(hSnapshot, &pEntry))
{
do
{
if(!lstrcmp(pEntry.szExeFile, szExeName))
{
HANDLE hToken;
if(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
{
LUID luidProcess;
if(LookupPrivilegeValue(0, SE_DEBUG_NAME, &luidProcess))
{
TOKEN_PRIVILEGES tpProcess;
tpProcess.PrivilegeCount = 1;
tpProcess.Privileges[0].Luid = luidProcess;
tpProcess.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if(AdjustTokenPrivileges(hToken, FALSE, &tpProcess, sizeof(TOKEN_PRIVILEGES), 0, 0))
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, true, pEntry.th32ProcessID);
}
}
}
if(hProcess == 0)
{
DWORD dwError = GetLastError();
dwError = 0;
}
}
}while(Process32Next(hSnapshot, &pEntry));
}
return hProcess;
}
Als ik geen TCHAR gebruik, en dus char voor die dllFileName variable dan werkt het programma, ook als ik het programma naar Multi-Byte Character encoding zet.
Maar dan is mijn programma niet meer UNICODE conform. Ik moet een manier vinden om dit op het laatste punt om te zetten (in die WriteProcessMemory functie), maar tot nu toe is me dat nog niet gelukt.
)