Win32 Exception: Access is denied while attempting to use OpenProcess

-1

this error is appearing while I am attempting to do two things.

while attempting this (Code block 1):

_class = new Proc(Process.GetProcessesByName("procname")[0]);

then in the class Proc whats happening is

public Proc(Process _SelectedProcess)
{
    Process = _SelectedProcess;
}
public Process Process
{
    get
    {
        return SelectedProcess;
    }
    set
    {
        SelectedProcess = value;
        if (SelectedProcess != null)
        {
            Process.EnterDebugMode();
            _Reader = new Win32_Memory(value.Handle, value.MainModule.BaseAddress.ToInt32(), value.Id);
         }
    }
}

That's some of the ways I get the exception, sometimes this passes without any exception for no apparent reason as far as I see.

Note: it never passes in windows 7, I'm using windows 10 and sometimes it happens that the function works

but if it does pass, the next time I need to use OpenProcess() outside of the Process class, I almost always get the exception, and if i do, then afterwards it fails executing code block 1 if I try to do so again.

this (code block 2) also gets the same access denied error, and sometimes doesnt...

if (_Reader.ReadInt(_addr) == 1) _Reader.Write(_addr, 0);
public bool Write(int address, long value)
{
    hProc = OpenProcess(ProcessAccessFlags.VMWrite, false, ID);
    byte[] val = BitConverter.GetBytes(value);
    bool worked = WriteProcessMemory(hProc, new IntPtr(address), val, (uint)val.LongLength, 0);
    CloseHandle(hProc);
    return worked;
}

the access flags:

[Flags]
public enum ProcessAccessFlags : uint
{
    All = 0x001F0FFF,
    Terminate = 0x00000001,
    CreateThread = 0x00000002,
    VMOperation = 0x00000008,
    VMRead = 0x00000010,
    VMWrite = 0x00000020,
    DupHandle = 0x00000040,
    SetInformation = 0x00000200,
    QueryInformation = 0x00000400,
    Synchronize = 0x00100000
}

the imports:

[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, int unused);

also worth noting that sometimes all of this code gets executed without ANY error and will work for as long as I do not reopen this application or if i do not restart the targeted application.

please help me out on this one, if I wasn't clear on some things - this is my first question and I haven't really ever needed to ask one before this one... so I will explain anything necessary afterwards

c#
.net
access-denied
win32exception
asked on Stack Overflow Nov 7, 2015 by Srdjan N. • edited Nov 7, 2015 by Srdjan N.

1 Answer

0

If, as your last comment indicates, the processes truly have nothing to do with each other, then that exactly explains the AccessDeniedException. You aren't allowed to just modify memory of any random process. That would be a security hole.

Both processes have to be setup and agree to share memory with each other. There are many ways to do inter-process communication between cooperating processes: here's a start: Shared memory between 2 processes (applications)

answered on Stack Overflow Nov 7, 2015 by modal_dialog • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0