K
K
Kvert0072014-09-18 11:02:41
C++ / C#
Kvert007, 2014-09-18 11:02:41

How to set privileges to read process memory (C++)?

You need to access the running process to find out from which directory it started.

DWORD PIDByName(WCHAR * AProcessName)
{
  HANDLE pHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  PROCESSENTRY32 ProcessEntry;
  DWORD pid;
  LPWSTR s=NULL;
  string str;
  TCHAR sttr[MAX_PATH];;
  ProcessEntry.dwSize = sizeof(ProcessEntry);
  bool Loop = Process32First(pHandle, &ProcessEntry);
 
  while (Loop)
    {
      if (wcsstr(ProcessEntry.szExeFile, AProcessName))
      {
          pid = ProcessEntry.th32ProcessID;
          CloseHandle(pHandle);
          return pid;
      }
      Loop = Process32Next(pHandle, &ProcessEntry);
    }
    return 0;
}
With this function I get the pid of the process.
if (processHandle != NULL) {
    QueryFullProcessImageName(processHandle,0,ss,&pid);
    GetProcessImageFileName(processHandle,ss,256);
     
    if (GetModuleFileNameEx(processHandle, NULL, filename, sizeof(char)) == 0) {
    cout << GetLastError();
    cerr << "Failed to get module filename." << endl;
    } else {
      cout << "Module filename is: " << filename << endl;
    }
    CloseHandle(processHandle);
  } else {
    cerr << "Failed to open process." << endl;
  }
here I want to extract the path using the QueryFullProcessImageName and GetProcessImageFileName functions, but it throws "Unhandled exception at 0x77DD2373 (ntdll.dll) in RLE.exe: 0xC0000005: Access violation while writing at 0x00000000."
As I understand it, there are not enough rights.
I'm trying to assign permissions to these:
SetPrivilege(HANDLE hToken,LPCTSTR Privilege,BOOL bEnablePrivilege);
including #pragma comment(lib, "Advapi32.lib").
But it seems that the library is not linked, because it spits out the following:
Error 10 error LNK1120: unresolved external elements: 1
Error 9 error LNK2019: reference to an unresolved external symbol "int __cdecl SetPrivilege(void *,wchar_t const *,int)" (? [email protected]@[email protected]) in _main function
Please help!!!! Writing in visual studio 2012

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2014-09-18
@GavriKos

And in my opinion you are just trying to work with a null pointer. Specify the specific line on which the exception is thrown.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question