コード例 #1
0
            internal static IEnumerable <string> EnumeratePorts()
            {
                uint needed   = 0;
                uint returned = 0;

                // As per MSDN, call EnumPorts with an empty buffer.
                // This call will fail, but will populate 'needed' with the required size of the buffer.
                using (SafeUnmanagedMemoryHandle nullBuffer = new SafeUnmanagedMemoryHandle(0))
                {
                    EnumPorts(null, 2, nullBuffer, 0, ref needed, ref returned);
                }

                // Allocate the required memory and retrieve the port data
                using (SafeUnmanagedMemoryHandle buffer = new SafeUnmanagedMemoryHandle((int)needed))
                {
                    if (EnumPorts(null, 2, buffer, needed, ref needed, ref returned))
                    {
                        PortInfo[] ports       = new PortInfo[returned];
                        IntPtr     currentPort = buffer.DangerousGetHandle();
                        for (int i = 0; i < returned; i++)
                        {
                            ports[i]    = Marshal.PtrToStructure <PortInfo>(currentPort);
                            currentPort = IntPtr.Add(currentPort, Marshal.SizeOf <PortInfo>());
                        }
                        return(ports.Select(n => n.PortName));
                    }
                    else
                    {
                        throw new Win32Exception();
                    }
                }
            }
コード例 #2
0
        /// <summary> Constructor. </summary>
        /// <param name="size"> The size of the array. </param>
        public UnsafeGridItemArray(int size)
        {
            var numberOfBytes = size * SizeOfGridItem;

            _dataArray = new SafeUnmanagedMemoryHandle(numberOfBytes);
            _root      = (GridItem *)_dataArray.Handle;
        }
コード例 #3
0
ファイル: HidDevice.cs プロジェクト: mayl8822/GestureSign
 protected HidDevice(IntPtr rawInputBuffer, ref RAWINPUT raw)
 {
     _hPreparsedData = GetPreparsedData(raw.header.hDevice);
     _pRawData       = GetRawDataPtr(rawInputBuffer, ref raw);
     _dwCount        = raw.hid.dwCount;
     _dwSizHid       = raw.hid.dwSizHid;
 }
        /// <summary>
        ///   Checks whether the primary access token of the process belongs to a user account that is a member of the local Administrator group, even if it currently is not elevated.
        /// </summary>
        /// <returns>
        ///   True if the primary access token of the process belongs to a user account that is a member of the Administrators group; false otherwise.
        /// </returns>
        public static bool IsUserInAdminGroup()
        {
            // Default token's received aren't impersonation tokens, we are looking for an impersonation token.
            bool isImpersonationToken = false;

            // Open the access token of the current process.
            SafeTokenHandle processToken;

            if (!AdvApi32.OpenProcessToken(Process.GetCurrentProcess().Handle, (uint)(TokenAccessLevels.Query | TokenAccessLevels.Duplicate), out processToken))
            {
                MarshalHelper.ThrowLastWin32ErrorException();
            }

            // Starting from Vista linked tokens are supported which need to be checked.
            if (EnvironmentHelper.VistaOrHigher)
            {
                // Determine token type: limited, elevated, or default.
                int marshalSize         = sizeof(AdvApi32.TokenElevationType);
                var elevationTypeHandle = SafeUnmanagedMemoryHandle.FromNewlyAllocatedMemory(marshalSize);
                if (!AdvApi32.GetTokenInformation(processToken, AdvApi32.TokenInformationClass.TokenElevationType, elevationTypeHandle.DangerousGetHandle(), marshalSize, out marshalSize))
                {
                    MarshalHelper.ThrowLastWin32ErrorException();
                }
                var tokenType = (AdvApi32.TokenElevationType)Marshal.ReadInt32(elevationTypeHandle.DangerousGetHandle());

                // If limited, get the linked elevated token for further check.
                if (tokenType == AdvApi32.TokenElevationType.TokenElevationTypeLimited)
                {
                    // Get the linked token.
                    marshalSize = IntPtr.Size;
                    var linkedTokenHandle = SafeUnmanagedMemoryHandle.FromNewlyAllocatedMemory(marshalSize);
                    if (!AdvApi32.GetTokenInformation(processToken, AdvApi32.TokenInformationClass.TokenLinkedToken, linkedTokenHandle.DangerousGetHandle(), marshalSize, out marshalSize))
                    {
                        MarshalHelper.ThrowLastWin32ErrorException();
                    }
                    processToken         = new SafeTokenHandle(Marshal.ReadIntPtr(linkedTokenHandle.DangerousGetHandle()));
                    isImpersonationToken = true;                     // Linked tokens are already impersonation tokens.
                }
            }

            // We need an impersonation token in order to check whether it contains admin SID.
            if (!isImpersonationToken)
            {
                SafeTokenHandle impersonatedToken;
                if (!AdvApi32.DuplicateToken(processToken, AdvApi32.SecurityImpersonationLevel.SecurityIdentification, out impersonatedToken))
                {
                    MarshalHelper.ThrowLastWin32ErrorException();
                }
                processToken = impersonatedToken;
            }

            // Check if the token to be checked contains admin SID.
            var identity  = new WindowsIdentity(processToken.DangerousGetHandle());
            var principal = new WindowsPrincipal(identity);

            return(principal.IsInRole(WindowsBuiltInRole.Administrator));
        }
コード例 #5
0
            internal static void XcvPortData(SafePrinterHandle xcvMonitor, string command, object portData)
            {
                // TCPMON XCV commands are listed at https://msdn.microsoft.com/en-us/windows/hardware/drivers/print/tcpmon-xcv-commands
                int size = Marshal.SizeOf(portData);

                using (SafeUnmanagedMemoryHandle inputDataPointer = new SafeUnmanagedMemoryHandle(size))
                {
                    Marshal.StructureToPtr(portData, inputDataPointer.DangerousGetHandle(), true);
                    if (!XcvData(xcvMonitor, command, inputDataPointer, (uint)size, IntPtr.Zero, 0, out uint outputNeededSize, out uint status))
                    {
                        throw new Win32Exception();
                    }
                }
            }
コード例 #6
0
 internal static extern bool EnumPorts([MarshalAs(UnmanagedType.LPWStr)] string pName, uint level, SafeUnmanagedMemoryHandle pPorts, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned);
コード例 #7
0
 internal static extern bool XcvData(SafePrinterHandle hXcv, string pszDataName, SafeUnmanagedMemoryHandle pInputData, uint cbInputData, IntPtr pOutputData, uint cbOutputData, out uint pcbOutputNeeded, out uint pdwStatus);
コード例 #8
0
 internal static extern bool EnumPrintProcessors(string pName, string pEnvironment, uint level, SafeUnmanagedMemoryHandle pPrintProcessorInfo, uint cbBuf, ref uint cbNeeded, ref uint cReturned);
コード例 #9
0
 public DuckDBState DuckDBQuery(DuckDBNativeConnection connection, SafeUnmanagedMemoryHandle query, out DuckDBResult result)
 {
     return NativeMethods.DuckDBQuery(connection, query, out result);
 }
コード例 #10
0
 public static extern DuckDBState DuckDBQuery(DuckDBNativeConnection connection, SafeUnmanagedMemoryHandle query, out DuckDBResult result);