Bonjour,
j'ai écrit un programme qui permet de charger une dll dans un processus, mais le programme fonctionne seulement en DEBUG, en RELEASE, j'ai un problème avec CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0), est-ce que cette fonction de marche qu'en debug?
voici mon code :
#include <iostream>
#include <atlpath.h>
#include "windows.h"
#include <tlhelp32.h>
using namespace std;
BOOL Injecter(char *dllPath, DWORD pid)
{
HANDLE hProc, hThread;
HANDLE hAddress;
HANDLE hKernel;
VOID *pMem;
DWORD dwExitCode;
// Ouvre le processus
hProc = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION, FALSE, pid);
if(!hProc)
return FALSE;
// Obtient l'adresse de la fonction LoadLibrary
hKernel = GetModuleHandle("kernel32.dll");
pMem = GetProcAddress((HMODULE)hKernel, "LoadLibraryA");
if(!pMem)
{
CloseHandle(hProc);
return FALSE;
}
// Crée un espace pour stocker l'emplacement de la dll dans le processus distant
hAddress = VirtualAllocEx(hProc, NULL, strlen(dllPath), MEM_COMMIT, PAGE_READWRITE);
if(!hAddress)
{
CloseHandle(hProc);
return FALSE;
}
// Copie le chemin de la dll
WriteProcessMemory(hProc, hAddress, dllPath, strlen(dllPath), NULL);
// Lance un thread pour charger la dll
hThread = CreateRemoteThread(hProc, NULL, NULL, (LPTHREAD_START_ROUTINE)pMem, hAddress, NULL, NULL);
if(!hThread)
{
VirtualFreeEx(hProc, hAddress, NULL, MEM_RELEASE);
CloseHandle(hProc);
return FALSE;
}
WaitForSingleObject(hThread, INFINITE);
GetExitCodeThread(hThread, &dwExitCode);
VirtualFreeEx(hProc, hAddress, NULL, MEM_RELEASE);
CloseHandle(hProc);
CloseHandle(hThread);
return dwExitCode>0?TRUE:FALSE;
}
int main(int argc, char *argv[])
{
HANDLE hTool;
PROCESSENTRY32 proc;
BOOL bFound;
// Vérifie les arguments
if((argc < 2) || (!ATLPath::FileExists(argv[1])))
{
cout << "Nombre d'arguments incorrects ou dll introuvable." << endl;
cout << "Inject [dll] [nom processus]";
return 1;
}
hTool = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(!hTool)
{
cout << "Impossible de creer hTool!" << endl;
return 1;
}
bFound = FALSE;
if(Process32First(hTool, &proc))
{
do
{
if(strcmp(proc.szExeFile, argv[2]) == 0)
{
if(!Injecter(argv[1], proc.th32ProcessID))
{
cout << "Echec d'injection." << endl;
}
else
{
cout << "Injection reussie." << endl;
}
bFound = TRUE;
}
}while(!bFound && Process32Next(hTool, &proc));
}
if(!bFound)
cout << "Processus introuvable";
CloseHandle(hTool);
return 0;
}
Mon programme en release me donne toujours "Processus introuvable", alors qu'en debug il fonctionne très bien.