Ejemplo n.º 1
0
        public static Dictionary <string, string> GetEnvironmentVariablesCore(IntPtr hProcess)
        {
            IntPtr penv = GetPenv(hProcess);

            int dataSize;

            if (!HasReadAccess(hProcess, penv, out dataSize))
            {
                throw new Win32Exception("Unable to read environment block.");
            }

            const int maxEnvSize = 32767;

            if (dataSize > maxEnvSize)
            {
                dataSize = maxEnvSize;
            }

            var  envData = new byte[dataSize];
            var  res_len = IntPtr.Zero;
            bool b       = ProcessNativeMethods.ReadProcessMemory(
                hProcess,
                penv,
                envData,
                new IntPtr(dataSize),
                ref res_len);

            if (!b || (int)res_len != dataSize)
            {
                throw new Win32Exception("Unable to read environment block data.");
            }

            return(EnvToDictionary(envData));
        }
Ejemplo n.º 2
0
        // should just be ReadIntPtr not TryReadIntPtr, throw when failed
        private static bool TryReadIntPtr(IntPtr hProcess, IntPtr ptr, out IntPtr readPtr)
        {
            var dataSize = new IntPtr(IntPtr.Size);
            var res_len  = IntPtr.Zero;

            if (!ProcessNativeMethods.ReadProcessMemory(
                    hProcess,
                    ptr,
                    out readPtr,
                    dataSize,
                    ref res_len))
            {
                // automatically GetLastError() and format message
                throw new Win32Exception();
            }

            // This is more like an assert
            return(res_len == dataSize);
        }