UIAutomation.SetUiaDateTimePickerDate.injectMemory C# (CSharp) Method

injectMemory() private method

private injectMemory ( int procId, byte buffer, IntPtr &hndProc, IntPtr &lpAddress ) : void
procId int
buffer byte
hndProc System.IntPtr
lpAddress System.IntPtr
return void
        private void injectMemory(int procId, byte[] buffer, out IntPtr hndProc, out IntPtr lpAddress)
        {
            // open process and get handle
            // hndProc = NativeMethods.OpenProcess(ProcessAccessFlags.All, true, procId);
            hndProc = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.All, true, procId);

            if (hndProc == (IntPtr)0)
            {
                AccessViolationException ex = new AccessViolationException("Unable to attach to process with an id " + procId);
                ThrowTerminatingError(new ErrorRecord(ex, "AccessDenined", ErrorCategory.SecurityError, null));
            }

            // allocate memory for object to be injected
            lpAddress = NativeMethods.VirtualAllocEx(hndProc, (IntPtr)null, (uint)buffer.Length,
                // AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite);
                NativeMethods.AllocationType.Commit | NativeMethods.AllocationType.Reserve, NativeMethods.MemoryProtection.ExecuteReadWrite);

            if (lpAddress == (IntPtr)0)
            {
                AccessViolationException ex = new AccessViolationException("Unable to allocate memory to proces with an id " + procId);
                ThrowTerminatingError(new ErrorRecord(ex, "AccessDenined", ErrorCategory.SecurityError, null));
            }

            // write data to process
            const uint wrotelen = 0;
            // uint wrotelen = 0;
            NativeMethods.WriteProcessMemory(hndProc, lpAddress, buffer, (uint)buffer.Length, (UIntPtr)wrotelen);

            if (Marshal.GetLastWin32Error() == 0) return;
            AccessViolationException ex2 = new AccessViolationException("Unable to write memory to process with an id " + procId);
            ThrowTerminatingError(new ErrorRecord(ex2, "AccessDenined", ErrorCategory.SecurityError, null));

            /*
            if (Marshal.GetLastWin32Error() != 0)
            {
                AccessViolationException ex = new AccessViolationException("Unable to write memory to process with an id " + procId);
                ThrowTerminatingError(new ErrorRecord(ex, "AccessDenined", ErrorCategory.SecurityError, null));
            }
            */
        }
SetUiaDateTimePickerDate