Beispiel #1
0
        }         // proc HttpRefreshProperties

        #endregion

        #region -- Start/Stop Process -------------------------------------------------

        private unsafe char[] CreateEnvironment(IntPtr hToken, string userName, bool loadProfile)
        {
            char *pEnv;

            if (hToken == IntPtr.Zero)
            {
                pEnv = NativeMethods.GetEnvironmentStrings();
                if (pEnv == null)
                {
                    throw new Win32Exception();
                }
            }
            else
            {
                if (loadProfile)
                {
                    var profileInfo = new NativeMethods.PROFILEINFO
                    {
                        dwSize     = Marshal.SizeOf(typeof(NativeMethods.PROFILEINFO)),
                        lpUserName = userName
                    };
                    if (!NativeMethods.LoadUserProfile(hToken, ref profileInfo))
                    {
                        throw new Win32Exception();
                    }
                    hProfile = profileInfo.hProfile;
                }
                else
                {
                    hProfile = IntPtr.Zero;
                }

                if (!NativeMethods.CreateEnvironmentBlock(out pEnv, loadProfile ? hToken : IntPtr.Zero, false))
                {
                    throw new Win32Exception();
                }
            }

            try
            {
                // Create environment dictionary
                var c = pEnv;

                var pName    = c;
                var pNameEnd = c;
                var pValue   = c;
                var dict     = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

                while (true)
                {
                    if (*c == '=')                     // property name split
                    {
                        pNameEnd = c;
                        pValue   = c + 1;
                    }
                    else if (*c == '\0')                    // value split
                    {
                        if (pName == c)
                        {
                            break;                             // doppel zero
                        }
                        var key   = new string(pName, 0, (int)(pNameEnd - pName));
                        var value = new string(pValue, 0, (int)(c - pValue));

                        dict.Add(key, value);

                        pName          =
                            pNameEnd   =
                                pValue = c + 1;
                    }

                    c++;
                }

                // change dictionary
                foreach (var env in Config.Elements(xnEnv))
                {
                    var key = env.GetAttribute("key", String.Empty);
                    if (!String.IsNullOrEmpty(key))
                    {
                        var value = env.Value;
                        if (value.Contains("%%"))
                        {
                            if (dict.TryGetValue(key, out var currentValue))
                            {
                                value = value.Replace("%%", currentValue);
                            }
                            else
                            {
                                value = value.Replace("%%", String.Empty);
                            }
                        }
                        dict[key] = value;
                    }
                }

                // recreate environment
                var len = 1;
                foreach (var kv in dict)
                {
                    len += kv.Key.Length;
                    len += kv.Value.Length;
                    len += 2;
                }
                var envBuffer = new char[len];
                var offset    = 0;

                void CopyStringToBuffer(string v)
                {
                    var l = v.Length;

                    v.CopyTo(0, envBuffer, offset, l);
                    offset += l;
                }                 // func CopyStringToBuffer

                foreach (var kv in dict)
                {
                    CopyStringToBuffer(kv.Key);
                    envBuffer[offset++] = '=';
                    CopyStringToBuffer(kv.Value);
                    envBuffer[offset++] = '\0';
                }
                envBuffer[offset++] = '\0';

                return(envBuffer);
            }
            finally
            {
                // Zerstöre den Block wieder
                if (hToken == IntPtr.Zero)
                {
                    NativeMethods.FreeEnvironmentStrings(pEnv);
                }
                else
                {
                    NativeMethods.DestroyEnvironmentBlock(pEnv);
                }
            }
        }         // func CreateEnvironment
Beispiel #2
0
        }         // proc HttpRefreshProperties

        #endregion

        #region -- Start/Stop Process -------------------------------------------------------

        private unsafe char[] CreateEnvironment(IntPtr hToken, string userName, bool loadProfile)
        {
            char[] r;
            char * pEnv;

            if (hToken == IntPtr.Zero)
            {
                pEnv = NativeMethods.GetEnvironmentStrings();
                if (pEnv == null)
                {
                    throw new Win32Exception();
                }
            }
            else
            {
                if (loadProfile)
                {
                    var profileInfo = new NativeMethods.PROFILEINFO();
                    profileInfo.dwSize     = Marshal.SizeOf(typeof(NativeMethods.PROFILEINFO));
                    profileInfo.lpUserName = userName;
                    if (!NativeMethods.LoadUserProfile(hToken, ref profileInfo))
                    {
                        throw new Win32Exception();
                    }
                    hProfile = profileInfo.hProfile;
                }
                else
                {
                    hProfile = IntPtr.Zero;
                }

                if (!NativeMethods.CreateEnvironmentBlock(out pEnv, loadProfile ? hToken : IntPtr.Zero, false))
                {
                    throw new Win32Exception();
                }
            }

            try
            {
                // Suche das Ende im Environment
                var   envLength = 0;
                char *c         = pEnv;
                while (*c != '\0' || *(c + 1) != '\0')
                {
                    envLength++;
                    c++;
                }
                envLength++;

                // Erzeuge die Zusätze
                var sbEnvAdd = new StringBuilder();
                foreach (var env in Config.Elements(xnEnv))
                {
                    var key = env.GetAttribute("key", String.Empty);
                    if (!String.IsNullOrEmpty(key))
                    {
                        sbEnvAdd.Append(key).Append('=').Append(env.Value).Append('\0');
                    }
                }

                // Kopiere das Env
                r = new char[envLength + sbEnvAdd.Length + 1];
                Marshal.Copy(new IntPtr(pEnv), r, 0, envLength);
                sbEnvAdd.CopyTo(0, r, envLength, sbEnvAdd.Length);
                r[r.Length - 1] = '\0';

                return(r);
            }
            finally
            {
                // Zerstöre den Block wieder
                if (hToken == IntPtr.Zero)
                {
                    NativeMethods.FreeEnvironmentStrings(pEnv);
                }
                else
                {
                    NativeMethods.DestroyEnvironmentBlock(pEnv);
                }
            }
        }         // func CreateEnvironment