Snippets

Richard Yu VCMP Launcher

You are viewing an old version of this snippet. View the current version.
Revised by Richard Yu 21bb18f
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdbool.h>

#define IP "127.0.0.1"
#define PORT 8192
#define NAME "YSC"
#define PASSWORD "password"

#define GTA_VC_EXE ""
#define VCMP_GAME_DLL ""

int main()
{
    // Process the VCMP command line.
    char commandLine[128];
    bool password = false;
    if (password)
        sprintf_s(commandLine, 128, "-c -h %s -c -p %u -n %s -z %s", IP, PORT, NAME, PASSWORD);
    else
        sprintf_s(commandLine, 128, "-c -h %s -c -p %u -n %s", IP, PORT, NAME);

    // Get GTA directory.
    char GTADriectory[MAX_PATH];
    strcpy_s(GTADriectory, MAX_PATH, GTA_VC_EXE);
    char *pos = strrchr(GTADriectory, '\\');
    if (pos)
        pos[0] = 0;

    // Create GTA process.
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION pi;
    if (CreateProcess(GTA_VC_EXE, commandLine, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, GTADriectory, &si, &pi))
    {
        // Alloc memory in GTA process.
        size_t dllLength = strlen(VCMP_GAME_DLL) + 1;
        LPVOID lpMem = VirtualAllocEx(pi.hProcess, NULL, dllLength, MEM_COMMIT, PAGE_READWRITE);
        if (lpMem)
        {
            // Wirte VCMP dll path to GTA process.
            if (WriteProcessMemory(pi.hProcess, lpMem, VCMP_GAME_DLL, dllLength, NULL))
            {
                // Get kernel32.dll handle.
                HMODULE hKernel = GetModuleHandle("kernel32.dll");
                if (hKernel)
                {
                    // Get LoadLibraryA address.
                    FARPROC fnLoadLibraryA = GetProcAddress(hKernel, "LoadLibraryA");
                    if (fnLoadLibraryA)
                    {
                        // Create remote thread in GTA process to inject VCMP dll.
                        HANDLE hInjectThread = CreateRemoteThread(pi.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)fnLoadLibraryA, lpMem, 0, NULL);
                        if (hInjectThread)
                        {
                            // Wiat for the inject thread.
                            if (WaitForSingleObject(hInjectThread, 10000) == WAIT_OBJECT_0)
                            {
                                ResumeThread(pi.hThread);
                                CloseHandle(hInjectThread);
                                CloseHandle(pi.hProcess);
                                CloseHandle(pi.hThread);
                                printf("OK!\n");
                            }
                            else
                                printf("Injected thread hung!\n");
                        }
                        else
                            printf("CreateRemoteThread failed! (%u)\n", GetLastError());
                    }
                    else
                        printf("GetProcAddress failed! (%u)\n", GetLastError());
                }
                else
                    printf("GetModuleHandle failed! (%u)\n", GetLastError());
            }
            else
                printf("WriteProcessMemory failed! (%u)\n", GetLastError());
        }
        else
            printf("VirtualAllocEx failed! (%u)\n", GetLastError());
    }
    else
        printf("CreateProcess failed! (%u)\n", GetLastError());

    return 0;
}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.