SolvedAccess Denied (Error 5) when using OpenProcess()

Posts 13 of 3 · Page 1 of 1
Access Denied (Error 5) when using OpenProcess()
"System.ComponentModel.Win32Exception: Access is denied", this error is appearing while I am attempting to do two things:

while attempting this (Code block 1):

Code:
_class = new Proc(Process.GetProcessesByName("procname")[0]);
then in the class Proc whats happening is

Code:
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);
         }
    }
}
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...

Code:
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:

Code:
[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:

Code:
[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.

this happens only on windows 10 (sometimes it works, sometimes it doesn't)

on windows 7 ive also tested and it never worked, everytime I opened it I always got error code 5 access is denied

besides my own program, I've also tried cheat engine on this process and it also got access denied, as well as artmoney pro and olydbg - all of them got access denied when using the OpenProcess() function
in my opinion , i find Windows 10 Shitty for .NET Apps for now .... just saying :/
Try testing your app on different OS , did you include Admin Request on launch ?
Most likely your process is being protected.
Posts 13 of 3 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?