How can I GetProcessID by name

+1 Tatrasiel R · October 1, 2015
I am trying to make some modifications to a program that I have running on the fly and I thought that DLL Injection would be well suited for this solution.

The way that I know of executing dll injection requires the PID or ProcessID How can I get the Process ID from a program name?



#include 
#include "Functs.h"
#include
#include

#include
#include
#include

char * DLLWeAreInjecting = "bar.dll";

char * ProcessName = "foo.exe";


void InjectDLL(HANDLE PROCNAME )
{
    DWORD ProcessID  = GetProcessId(PROCNAME);
    HANDLE hProc;
    LPVOID paramAddr;
    HINSTANCE hDll = LoadLibrary("KERNEL32");
    typedef HINSTANCE(*fpLoadLibrary)(char*);
    fpLoadLibrary LoadLibraryAddr = (fpLoadLibrary)GetProcAddress(hDll, "LoadLibraryA");
    DWORD FindProcessId(const std::wstring& processName);

    hProc = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessID);/*??????? ???????*/
    paramAddr = VirtualAllocEx(hProc, 0, strlen(DLLWeAreInjecting) + 1, MEM_COMMIT, PAGE_READWRITE);// ????? PATH ?????? ??? ? ???????? ????? ? ??????
    bool memoryWritten = WriteProcessMemory(hProc, paramAddr, DLLWeAreInjecting, strlen(DLLWeAreInjecting) + 1, NULL);

    CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, paramAddr, 0, 0);
    CloseHandle(hProc);
    //return memoryWritten;
}


This DWORD ProcessID  = GetProcessId(PROCNAME);
Doesn't get the processID by name.




Post a Reply

Replies

Oldest  Newest  Rating
0 c student · October 1, 2015
GetProcesId's parameter is a handle to the process, which I assume you acquire from the OpenProcess function.  To get a process ID, you can cycle through the list of running processes until you have a match with your process name.  Try this example here:

https://msdn.microsoft.com/en-us/library/ms686701%28VS.85%29.aspx
  • 1

C++

107,084 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator