How to start a game from a C++ script

Started by
6 comments, last by hplus0603 3 years, 4 months ago

Hello!

I'm trying to open gta_sa.exe from a script in C++... the problem is... it just starts the process (verified in task manager) but nothing happens... I tried using ''system'' function and still nothing happened. When I try open another .exe files it works... What's the problem?

Advertisement

Hard to say, since you don't show any code.

Did you try setting the working folder to the executables folder? Could be that gta_sa.exe tries to read files from its folder.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

// ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <Windows.h>
#include <TLHELP32.h>

#define CREATE_THREAD_ACCESS (PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ)

DWORD FindProcessId(const char* processname)
{
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;
    DWORD result = NULL;

    // Take a snapshot of all processes in the system.
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (INVALID_HANDLE_VALUE == hProcessSnap) return(FALSE);

    pe32.dwSize = sizeof(PROCESSENTRY32); // <----- IMPORTANT

    // Retrieve information about the first process,
    // and exit if unsuccessful
    if (!Process32First(hProcessSnap, &pe32))
    {
        CloseHandle(hProcessSnap);          // clean the snapshot object
        printf("!!! Failed to gather information on system processes! \n");
        return(NULL);
    }

    do
    {
       // printf("Checking process %ls\n", pe32.szExeFile);
        if (0 == strcmp(processname, pe32.szExeFile))
        {
            result = pe32.th32ProcessID;
            break;
        }
    } while (Process32Next(hProcessSnap, &pe32));

    CloseHandle(hProcessSnap);

    return result;
}

BOOL InjectDLL(DWORD ProcessID)
{
    LPCSTR DLL_PATH = "C:\\Users\\alexm\\source\\repos\\RemasterSAMPDLL\\Debug\\RemasterSAMPDLL.dll";
    LPVOID LoadLibAddy, RemoteString;

    if (!ProcessID)
        return false;

    HANDLE Proc = OpenProcess(CREATE_THREAD_ACCESS, FALSE, ProcessID);

    if (!Proc)
    {
        std::cout << "OpenProcess() failed: " << GetLastError() << std::endl;
        return false;
    }

    LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

    RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, strlen(DLL_PATH) + 1, MEM_COMMIT, PAGE_READWRITE);
    WriteProcessMemory(Proc, RemoteString, (LPVOID)DLL_PATH, strlen(DLL_PATH) + 1, NULL);
    CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, RemoteString, NULL, NULL);

    CloseHandle(Proc);

    return true;
}

BOOL InjectDLL_EAX(DWORD ProcessID)
{
    LPCSTR DLL_PATH = "C:\\Users\\alexm\\Desktop\\GTA - San Andreas\\eax.dll";
    LPVOID LoadLibAddy, RemoteString;

    if (!ProcessID)
        return false;

    HANDLE Proc = OpenProcess(CREATE_THREAD_ACCESS, FALSE, ProcessID);

    if (!Proc)
    {
        std::cout << "OpenProcess() failed: " << GetLastError() << std::endl;
        return false;
    }

    LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

    RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, strlen(DLL_PATH) + 1, MEM_COMMIT, PAGE_READWRITE);
    WriteProcessMemory(Proc, RemoteString, (LPVOID)DLL_PATH, strlen(DLL_PATH) + 1, NULL);
    CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, RemoteString, NULL, NULL);

    CloseHandle(Proc);

    return true;
}

BOOL InjectDLL_OGG(DWORD ProcessID)
{
    LPCSTR DLL_PATH = "C:\\Users\\alexm\\Desktop\\GTA - San Andreas\\ogg.dll";
    LPVOID LoadLibAddy, RemoteString;

    if (!ProcessID)
        return false;

    HANDLE Proc = OpenProcess(CREATE_THREAD_ACCESS, FALSE, ProcessID);

    if (!Proc)
    {
        std::cout << "OpenProcess() failed: " << GetLastError() << std::endl;
        return false;
    }

    LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

    RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, strlen(DLL_PATH) + 1, MEM_COMMIT, PAGE_READWRITE);
    WriteProcessMemory(Proc, RemoteString, (LPVOID)DLL_PATH, strlen(DLL_PATH) + 1, NULL);
    CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, RemoteString, NULL, NULL);

    CloseHandle(Proc);

    return true;
}

BOOL InjectDLL_vorbis(DWORD ProcessID)
{
    LPCSTR DLL_PATH = "C:\\Users\\alexm\\Desktop\\GTA - San Andreas\\vorbis.dll";
    LPVOID LoadLibAddy, RemoteString;

    if (!ProcessID)
        return false;

    HANDLE Proc = OpenProcess(CREATE_THREAD_ACCESS, FALSE, ProcessID);

    if (!Proc)
    {
        std::cout << "OpenProcess() failed: " << GetLastError() << std::endl;
        return false;
    }

    LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

    RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, strlen(DLL_PATH) + 1, MEM_COMMIT, PAGE_READWRITE);
    WriteProcessMemory(Proc, RemoteString, (LPVOID)DLL_PATH, strlen(DLL_PATH) + 1, NULL);
    CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, RemoteString, NULL, NULL);

    CloseHandle(Proc);

    return true;
}

BOOL InjectDLL_vorbisFile(DWORD ProcessID)
{
    LPCSTR DLL_PATH = "C:\\Users\\alexm\\Desktop\\GTA - San Andreas\\vorbisFile.dll";
    LPVOID LoadLibAddy, RemoteString;

    if (!ProcessID)
        return false;

    HANDLE Proc = OpenProcess(CREATE_THREAD_ACCESS, FALSE, ProcessID);

    if (!Proc)
    {
        std::cout << "OpenProcess() failed: " << GetLastError() << std::endl;
        return false;
    }

    LoadLibAddy = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

    RemoteString = (LPVOID)VirtualAllocEx(Proc, NULL, strlen(DLL_PATH) + 1, MEM_COMMIT, PAGE_READWRITE);
    WriteProcessMemory(Proc, RemoteString, (LPVOID)DLL_PATH, strlen(DLL_PATH) + 1, NULL);
    CreateRemoteThread(Proc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddy, RemoteString, NULL, NULL);

    CloseHandle(Proc);

    return true;
}
int main()
{
    std::cout << "Magda este o fata si are 15 ani!\n";
    ShellExecute(0, "open", "C:\\Users\\alexm\\Desktop\\GTASanAndreas\\gta_sa.exe", NULL, NULL, SW_SHOWDEFAULT);
   // system("start C:\\Users\\alexm\\Desktop\\GTASanAndreas\\gta_sa.exe");
    
    //Sleep(10000);
    //InjectDLL(FindProcessId("gta_sa.exe"));
   
    //ShellExecute(0, "open", "C:\\Users\\alexm\\Desktop\\Nimic\\Winter Modpack High ENB\\samp.exe", NULL, NULL, NULL);
    //InjectDLL(FindProcessId("samp.exe"));

    //C:\\Users\\alexm\\Desktop\\Nimic\\Winter Modpack High ENB\\samp.exe

}

//End of script//

Isn't that... gta_sa.exe it's in the right folder with all it needs.... I just paste the directory into

the parameter. When I run it by double-cllicking it... it work perfectly, but when I run it through the

function... it just opens the process, but nothing happens. Ignore the InjectDLL_EAX and others... I tried something non-sense

Try with ShellExecuteEx, and set lpDirectory to the path “C:\\Users\\alexm\\Desktop\\GTASanAndreas”.

If that doesn't work either, check in the Event Viewer if there's any hint on why it doesn't start up.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

quency148 said:
I'm trying to open gta_sa.exe from a script in C++

just out of interest, why are you trying to run it from c++? why not from the command-line as it was intended?

The script he's building is injecting some DLLs, seemingly to do with vorbis.

Maybe he's trying to replace some sound files with some vorbis files he has?

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement