memexec 0.2.0

A library for loading and executing PE (Portable Executable) from memory without ever touching the disk
Documentation
#include <Windows.h>
#include <stdio.h>

// TLS callback: https://stackoverflow.com/a/36891752/11159056
void NTAPI tls_callback(PVOID DllHandle, DWORD dwReason, PVOID lpReserved) {
    MessageBoxA(0, "TLS callback", "TLS", 0);
}

#ifdef _WIN64
#pragma comment (linker, "/INCLUDE:_tls_used")  // See p. 1 below

#pragma comment (linker, "/INCLUDE:tls_callback_func")  // See p. 3 below

#else
#pragma comment (linker, "/INCLUDE:__tls_used")  // See p. 1 below

#pragma comment (linker, "/INCLUDE:_tls_callback_func")  // See p. 3 below

#endif

// Explained in p. 3 below
#ifdef _WIN64
#pragma const_seg(".CRT$XLF")
EXTERN_C const
#else
#pragma data_seg(".CRT$XLF")
EXTERN_C
#endif
PIMAGE_TLS_CALLBACK tls_callback_func = tls_callback;
#ifdef _WIN64
#pragma const_seg()
#else
#pragma data_seg()
#endif //_WIN64

void proc() {
    MessageBoxA(0, "Hello world from loaded DLL, will pop calc.exe later", "What's up", 0);
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION pi;
    if (CreateProcessA("C:\\windows\\system32\\calc.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    };
}

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
) {
    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
        proc();
        break;

    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}