#include <Windows.h>
#include <atlbase.h>
#include <Shlobj.h>
#include <string>
#include "MinHook.h"
#include "pch.h"
#include <string>
#include <stdlib.h>
#include "stdafx.h"
#include <iostream>

// Path to modified game files store in AppData
std::wstring MOD_FILES_PATH;

// Path to the apps protected resources in WindowsApps
// Don't use the full path name, just keep the Publisher.AppName part
std::wstring APP_LOCATION(L"C:\\Program Files\\WindowsApps\\Publisher.AppName");

// Sets a hook on the function at origAddress function and provides a trampoline to the original function
BOOL setHook(LPVOID* origAddress, LPVOID* hookFunction, LPVOID* trampFunction);

// Attaches a hook on a function given the name of the owning module and the name of the function
BOOL attach(LPWSTR wstrModule, LPCSTR strFunction, LPVOID* hook, LPVOID* original);

// Basic hook setup for CreateFileW
typedef HANDLE(WINAPI* PfnCreateFileW)(LPCWSTR lpFilename, DWORD dwAccess, DWORD dwSharing, LPSECURITY_ATTRIBUTES saAttributes, DWORD dwCreation, DWORD dwAttributes, HANDLE hTemplate);
PfnCreateFileW pfnCreateFileW = NULL; // Will hold the trampoline to the original CreateFileW function

// CreateFileW hook function
HANDLE WINAPI HfnCreateFileW(LPCWSTR lpFilename, DWORD dwAccess, DWORD dwSharing, LPSECURITY_ATTRIBUTES saAttributes, DWORD dwCreation, DWORD dwAttributes, HANDLE hTemplate)
{
	std::wstring filePath(lpFilename);

	// Check if the app is accessing protected resources
	if (filePath.find(APP_LOCATION) != filePath.npos)
	{
		std::wstring newPath(MOD_FILES_PATH);

		// Windows provides the app the location of the WindowsApps directory, so the first half the file path will use back slashes
		// After that, some apps will use back slashes while others use forward slashes so be aware of what the app uses
		newPath += filePath.substr(filePath.find(L"\\", APP_LOCATION.size()) + 1, filePath.size());

		// Check if the file being accessed exists at the new path and reroute access to that file
		// Don't reroute directories as bad things can happen such as directories being ghost locked
		if (PathFileExists(newPath.c_str()) && !PathIsDirectory(newPath.c_str()))
			return pfnCreateFileW(newPath.c_str(), dwAccess, dwSharing, saAttributes, dwCreation, dwAttributes, hTemplate);
	}

	// Let the app load other files normally
	return pfnCreateFileW(lpFilename, dwAccess, dwSharing, saAttributes, dwCreation, dwAttributes, hTemplate);
}

BOOL Initialize()
{
	// Initialize MinHook
	if (MH_Initialize() != MH_OK)
		return FALSE;

	// Get the path to the apps AppData folder
	// When inside a UWP app, CSIDL_LOCAL_APPDATA returns the location of the apps AC folder in AppData
	TCHAR szPath[MAX_PATH];
	if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, szPath)))
	{
		// Get the path to the mod files folder
		std::wstring appData(szPath);
		appData = appData.substr(0, appData.rfind(L"AC")); // Get the base directory
		appData += L"LocalState\\ModFiles\\"; // Get the location of any new files you want the app to use

		MOD_FILES_PATH = appData;
	}
	else
		return FALSE;

	// Attach a hook on CreateProcessW and return the status of the hook
	BOOL hook = TRUE;
	hook = attach(L"KernelBase.dll", "CreateFileW", (LPVOID*)&HfnCreateFileW, (LPVOID*)&pfnCreateFileW);

	return hook;
}

BOOL Uninitialize()
{
	// Uninitialize MinHook
	if (MH_Uninitialize() != MH_OK)
		return FALSE; // This status will end up being ignored

	return TRUE;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		return Initialize(); // If initialization failed, the DLL will detach
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		Uninitialize(); // Return value doesn't matter when detaching
		break;
	}
	return TRUE;
}

BOOL setHook(LPVOID* origAddress, LPVOID* hookFunction, LPVOID* trampFunction)
{
	if (MH_CreateHook(origAddress, hookFunction, reinterpret_cast<LPVOID*>(trampFunction)) != MH_OK)
		return FALSE;

	if (MH_EnableHook(origAddress) != MH_OK)
		return FALSE;

	return TRUE;
}

BOOL attach(LPWSTR wstrModule, LPCSTR strFunction, LPVOID* hook, LPVOID* original)
{
	HMODULE hModule = GetModuleHandle(wstrModule);
	if (hModule == NULL)
		return FALSE;

	FARPROC hFunction = GetProcAddress(hModule, strFunction);
	if (hFunction == NULL)
		return FALSE;

	return setHook((LPVOID*)hFunction, hook, original);
} 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}