C# -> File.Exists mit Enum

  • Dann ist die gta_sa.exe noch irgendwo / irgendwie geöffnet


    //Edit: Mögliche Lösung ist evtl auch die gta_sa.set zu löschen

    "Bevor ich mir Informationen aus der "Bild" hole,
    werde ich anfangen, Wahlergebnisse danach vorauszusagen,
    neben welchen Busch unsere Katze gepinkelt hat."

    Margarete Stokowski

  • Ich habs mir auch mal durchgelesen, da ich selber hilfe zu dem Thema brauche.
    Nun habe ich es auch so gemacht, aber das geht nicht ^^
    Der connected mit ":7777" und hat immer "wrong server password"


    C
    Process Proz = new Process();
            	Proz.StartInfo.WorkingDirectory = std_dir;
            	Proz.StartInfo.FileName = "samp.exe";
            	Proz.StartInfo.Arguments = "-c -n " + loggedName + " -h " + server_ip + " -p " + server_port + " -z " + server_password;
            	Proz.Start();
  • Und wie mache ich das? Ich finde in Google nur haufenweise Codes für Injektoren, aber schaffe es iwie nicht die zu verwenden ^^


    zB habe ich mir mal den hier genommen:

    C
    using System;using System.Diagnostics;using System.IO;using System.Runtime.InteropServices;using System.Text;namespace samp_launcher{	public enum DllInjectionResult	{    	DllNotFound,    	GameProcessNotFound,    	InjectionFailed,    	Success	}	public sealed class DllInjector	{    	static readonly IntPtr INTPTR_ZERO = (IntPtr)0;    	[DllImport("kernel32.dll", SetLastError = true)]    	static extern IntPtr OpenProcess(uint dwDesiredAccess, int bInheritHandle, uint dwProcessId);    	[DllImport("kernel32.dll", SetLastError = true)]    	static extern int CloseHandle(IntPtr hObject);    	[DllImport("kernel32.dll", SetLastError = true)]    	static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);    	[DllImport("kernel32.dll", SetLastError = true)]    	static extern IntPtr GetModuleHandle(string lpModuleName);    	[DllImport("kernel32.dll", SetLastError = true)]    	static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, uint flAllocationType, uint flProtect);    	[DllImport("kernel32.dll", SetLastError = true)]    	static extern int WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, uint size, int lpNumberOfBytesWritten);    	[DllImport("kernel32.dll", SetLastError = true)]    	static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttribute, IntPtr dwStackSize, IntPtr lpStartAddress,        	IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);    	static DllInjector _instance;    	public static DllInjector GetInstance    	{        	get        	{            	if (_instance == null)            	{                	_instance = new DllInjector();            	}            	return _instance;        	}    	}    	DllInjector() { }    	public DllInjectionResult Inject(string sProcName, string sDllPath)    	{        	if (!File.Exists(sDllPath))        	{            	return DllInjectionResult.DllNotFound;        	}        	uint _procId = 0;        	Process[] _procs = Process.GetProcesses();        	for (int i = 0; i < _procs.Length; i++)        	{            	if (_procs[i].ProcessName == sProcName)            	{                	_procId = (uint)_procs[i].Id;                	break;            	}        	}        	if (_procId == 0)        	{            	return DllInjectionResult.GameProcessNotFound;        	}        	if (!bInject(_procId, sDllPath))        	{            	return DllInjectionResult.InjectionFailed;        	}        	return DllInjectionResult.Success;    	}    	bool bInject(uint pToBeInjected, string sDllPath)    	{        	IntPtr hndProc = OpenProcess((0x2 | 0x8 | 0x10 | 0x20 | 0x400), 1, pToBeInjected);        	if (hndProc == INTPTR_ZERO)        	{            	return false;        	}        	IntPtr lpLLAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");        	if (lpLLAddress == INTPTR_ZERO)        	{            	return false;        	}        	IntPtr lpAddress = VirtualAllocEx(hndProc, (IntPtr)null, (IntPtr)sDllPath.Length, (0x1000 | 0x2000), 0X40);        	if (lpAddress == INTPTR_ZERO)        	{            	return false;        	}        	byte[] bytes = Encoding.ASCII.GetBytes(sDllPath);        	if (WriteProcessMemory(hndProc, lpAddress, bytes, (uint)bytes.Length, 0) == 0)        	{            	return false;        	}        	if (CreateRemoteThread(hndProc, (IntPtr)null, INTPTR_ZERO, lpLLAddress, lpAddress, 0, (IntPtr)null) == INTPTR_ZERO)        	{            	return false;        	}        	CloseHandle(hndProc);        	return true;    	}	}}


    Nur weiß ich einfach nicht, wie ich auf "Inject(string sProcName, string sDllPath)" zugreifen kann..

  • Ich hatte mal einen gefunden und auf meine Bedrüfnisse angepasst.
    Einfach eine neue Klasse erstellen den Code da rein und verwenden,
    wie du die Funktion kommst, solltest du wissen :D


    Injektor
    class Injektor
    {
    #region Injektor Code by The_Undead : Rhys M.
    [DllImport("kernel32")]
    public static extern IntPtr CreateRemoteThread(
    IntPtr hProcess,
    IntPtr lpThreadAttributes,
    uint dwStackSize,
    UIntPtr lpStartAddress, // raw Pointer into remote process
    IntPtr lpParameter,
    uint dwCreationFlags,
    out IntPtr lpThreadId
    );

    Injektor
    [DllImport("kernel32.dll")]
    public static extern IntPtr OpenProcess(
    UInt32 dwDesiredAccess,
    Int32 bInheritHandle,
    Int32 dwProcessId
    );

    Injektor
    [DllImport("kernel32.dll")]
    public static extern Int32 CloseHandle(
    IntPtr hObject
    );

    Injektor
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern bool VirtualFreeEx(
    IntPtr hProcess,
    IntPtr lpAddress,
    UIntPtr dwSize,
    uint dwFreeType
    );

    Injektor
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
    public static extern UIntPtr GetProcAddress(
    IntPtr hModule,
    string procName
    );

    Injektor
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern IntPtr VirtualAllocEx(
    IntPtr hProcess,
    IntPtr lpAddress,
    uint dwSize,
    uint flAllocationType,
    uint flProtect
    );

    Injektor
    [DllImport("kernel32.dll")]
    static extern bool WriteProcessMemory(
    IntPtr hProcess,
    IntPtr lpBaseAddress,
    string lpBuffer,
    UIntPtr nSize,
    out IntPtr lpNumberOfBytesWritten
    );

    Injektor
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetModuleHandle(
    string lpModuleName
    );

    Injektor
    [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
    internal static extern Int32 WaitForSingleObject(
    IntPtr handle,
    Int32 milliseconds
    );
    public void InjectDLL(IntPtr hProcess, String strDLLName)
    {
    IntPtr bytesout;

    Injektor
    // Length of string containing the DLL file name +1 byte padding
    Int32 LenWrite = strDLLName.Length + 1;
    // Allocate memory within the virtual address space of the target process
    IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40); //allocation pour WriteProcessMemory

    Injektor
    // Write DLL file name to allocated memory in target process
    WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
    // Function pointer "Injector"
    UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

    Injektor
    if (Injector == null)
    {
    MessageBox.Show(" Injector Error! \n ");
    // return failed
    return;
    }

    Injektor
    // Create thread in target process, and store handle in hThread
    IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
    // Make sure thread handle is valid
    if (hThread == null)
    {
    //incorrect thread handle ... return failed
    MessageBox.Show(" hThread [ 1 ] Error! \n ");
    return;
    }
    // Time-out is 10 seconds...
    int Result = WaitForSingleObject(hThread, 10 * 1000);
    // Check whether thread timed out...
    if (Result == 0x00000080L || Result == 0x00000102L)
    {
    /* Thread timed out... */
    MessageBox.Show(" hThread [ 2 ] Error! \n ");
    // Make sure thread handle is valid before closing... prevents crashes.
    if (hThread != null)
    {
    //Close thread in target process
    CloseHandle(hThread);
    }
    return;
    }
    // Sleep thread for 1 second
    Thread.Sleep(1000);
    // Clear up allocated space ( Allocmem )
    VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
    // Make sure thread handle is valid before closing... prevents crashes.
    if (hThread != null)
    {
    //Close thread in target process
    CloseHandle(hThread);
    }
    // return succeeded
    return;
    }
    #endregion
    public void StartInjekt(string Path, string Prozess, string Args, string dllToInjekt)
    {
    Process Proz = new Process();
    Proz.StartInfo.WorkingDirectory = Path;
    Proz.StartInfo.FileName = Prozess;
    Proz.StartInfo.Arguments = Args;
    Proz.Start();
    if (Proz.Id >= 0)
    {
    IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, Proz.Id);
    InjectDLL(hProcess, Path + "\" + dllToInjekt);
    }
    Proz.Dispose();
    }
    }

    "Bevor ich mir Informationen aus der "Bild" hole,
    werde ich anfangen, Wahlergebnisse danach vorauszusagen,
    neben welchen Busch unsere Katze gepinkelt hat."

    Margarete Stokowski

  • Ein Pointer (Zeiger / Handle), ist aber platformabhängig, sprich gibts nur bei Windows ^^

    "Bevor ich mir Informationen aus der "Bild" hole,
    werde ich anfangen, Wahlergebnisse danach vorauszusagen,
    neben welchen Busch unsere Katze gepinkelt hat."

    Margarete Stokowski

  • Hmm, iwie mache ich es falsch :P


    C
    Process Proz = new Process();
            	Proz.StartInfo.WorkingDirectory = std_dir;
            	Proz.StartInfo.FileName = "gta_sa.exe";
            	Proz.Start();
            	IntPtr proz_intptr = Proz.MainWindowHandle;
            	Injektor injector = new Injektor();
            	injector.InjectDLL(proz_intptr, std_dir+@"\samp.dll -c -n " + loggedName + " -h " + server_ip + " -p " + server_port + " -z " + server_password + " -d");
  • Ja definitiv...


    Nutz doch einfach die Funktion, die es in der Klasse gibt.


    und zwar so
    Injektor inj = new Injektor();
    inj.StartInjekt(Path, "gta_sa.exe", "Argumente", "samp.dll");

    "Bevor ich mir Informationen aus der "Bild" hole,
    werde ich anfangen, Wahlergebnisse danach vorauszusagen,
    neben welchen Busch unsere Katze gepinkelt hat."

    Margarete Stokowski

  • Achso sorry, habe die Funktion wohl übersehen ^^


    Aber der startet bei mir ganz normal GTA-SA ohne SAMP :/


    C
    injector.StartInjekt(std_dir, "gta_sa.exe", "-c -n " + loggedName + " -h " + server_ip + " -p " + server_port + " -z " + server_password + " -d", "samp.dll");
  • Also in C# ist es schonmal eine scheiß Idee die Strings mit + zusammen zusetzen.
    Dafür gibt es schließlich den Stringbuilder ;)


    Aber es scheint ein Problem mit deiner .dll oder deinen Parametern zu sein.
    Vielleicht hast du auch dein gta im Laufwerk C ?


    Könnte Probleme geben bzw. Adminrechte erfordern.

    "Bevor ich mir Informationen aus der "Bild" hole,
    werde ich anfangen, Wahlergebnisse danach vorauszusagen,
    neben welchen Busch unsere Katze gepinkelt hat."

    Margarete Stokowski

  • Soll also heißen, dein GTA ist im Laufwerk C...


    Probier es mal in einem anderen.
    Könnte wirklich daran liegen.
    SA:MP hat selber Probleme damit :S

    "Bevor ich mir Informationen aus der "Bild" hole,
    werde ich anfangen, Wahlergebnisse danach vorauszusagen,
    neben welchen Busch unsere Katze gepinkelt hat."

    Margarete Stokowski