コード例 #1
0
        public static SafeHGlobalHandle AllocHGlobalStruct <T>(T obj) where T : struct
        {
            Debug.Assert(typeof(T).StructLayoutAttribute.Value == LayoutKind.Sequential);

            SafeHGlobalHandle result = AllocHGlobal(Marshal.SizeOf(typeof(T)));

            Marshal.StructureToPtr(obj, result.pointer, false);

            return(result);
        }
コード例 #2
0
        private void GetEffectivePermissions_AuthzInitializeResourceManager(string serverName, out bool remoteServerAvailable)
        {
            remoteServerAvailable = false;

            var rpcInitInfo = new AUTHZ_RPC_INIT_INFO_CLIENT();

            rpcInitInfo.version    = AuthzRpcClientVersion.V1;
            rpcInitInfo.objectUuid = AUTHZ_OBJECTUUID_WITHCAP;
            rpcInitInfo.protocol   = RCP_OVER_TCP_PROTOCOL;
            rpcInitInfo.server     = serverName;

            SafeHGlobalHandle pRpcInitInfo = SafeHGlobalHandle.AllocHGlobalStruct(rpcInitInfo);

            if (!AuthzInitializeRemoteResourceManager(pRpcInitInfo.ToIntPtr(), out authzRM))
            {
                int error = Marshal.GetLastWin32Error();

                if (error != Win32Error.EPT_S_NOT_REGISTERED) //if not RPC server unavailable
                {
                    throw new Win32Exception(error);
                }

                if (serverName == "localhost")
                {
                    remoteServerAvailable = true;
                }

                //
                // As a fallback we do AuthzInitializeResourceManager. But the results can be inaccurate.
                //
                if (!AuthzInitializeResourceManager(
                        AuthzResourceManagerFlags.NO_AUDIT,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        "EffectiveAccessCheck",
                        out authzRM))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            else
            {
                remoteServerAvailable = true;
            }
        }
コード例 #3
0
        /// <summary>
        /// Allocates from unmanaged memory to represent a structure with a
        /// variable length array at the end and marshal these structure
        /// elements. It is the callers responsibility to marshal what preceeds
        /// the trailinh array into the unmanaged memory. ONLY structures with
        /// attribute StructLayout of LayoutKind.Sequential are supported.
        /// </summary>
        /// <typeparam name="T">Type of the trailing array of structures</typeparam>
        /// <param name="prefixBytes">Number of bytes preceeding the trailing array of structures</param>
        /// <param name="values">Collection of structure objects</param>
        /// <param name="count"></param>
        /// <returns>SafeHGlobalHandle object to an native (unmanaged) structure
        /// with a trail array of structures</returns>
        public static SafeHGlobalHandle AllocHGlobal <T>(int prefixBytes, IEnumerable <T> values, int count) where T
        : struct
        {
            Debug.Assert(typeof(T).StructLayoutAttribute.Value == LayoutKind.Sequential);

            SafeHGlobalHandle result = AllocHGlobal(prefixBytes + Marshal.SizeOf(typeof(T)) * count);

            IntPtr ptr = new IntPtr(result.pointer.ToInt32() + prefixBytes);

            foreach (var value in values)
            {
                Marshal.StructureToPtr(value, ptr, false);
                ptr.Increment <T>();
            }

            return(result);
        }
コード例 #4
0
        static SafeHGlobalHandle AllocHGlobal(int cb)
        {
            if (cb < 0)
            {
                throw new ArgumentOutOfRangeException("cb", "The value of this argument must be non-negative");
            }

            SafeHGlobalHandle result = new SafeHGlobalHandle();

            //
            // CER
            //
            RuntimeHelpers.PrepareConstrainedRegions();
            try { }
            finally
            {
                result.pointer = Marshal.AllocHGlobal(cb);
            }

            return(result);
        }