无法将类型“long”隐式转换为“uint”以及在 C# 中使用未分配的局部变量

问题描述 投票:0回答:1

我有一个错误

"Cannot implicitly convert type 'long' to 'uint'. An explicit conversion exists (are you missing a cast?)" and "Use of unassigned local variable 'lASTINPUTINFO'"

请指导我

我该如何解决这个错误,我也是C#初学者,或者我创建的代码的实现有问题或者我使用的代码确实是错误的

谢谢

我使用下面的代码:

namespace Utilities
{
    internal class Win32
    {
        public Win32()
        {
        }

        public static uint GetIdleTime()
        {
            LASTINPUTINFO lASTINPUTINFO = new LASTINPUTINFO()
            {
`Error below line code Use of unassigned local variable`
                cbSize = (uint)Marshal.SizeOf<LASTINPUTINFO>(lASTINPUTINFO)
            };
            Win32.GetLastInputInfo(ref lASTINPUTINFO);
`Error below line code Cannot implicitly convert type long to uint`
            return Environment.TickCount - lASTINPUTINFO.dwTime;
        }

        [DllImport("Kernel32.dll", CharSet=CharSet.None, ExactSpelling=false)]
        private static extern uint GetLastError();

        [DllImport("User32.dll", CharSet=CharSet.None, ExactSpelling=false)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        public static long GetLastInputTime()
        {
            LASTINPUTINFO lASTINPUTINFO = new LASTINPUTINFO()
            {
`Error below line code Use of unassigned local variable`
                cbSize = (uint)Marshal.SizeOf<LASTINPUTINFO>(lASTINPUTINFO)
            };
            
    }
}

c# visual-studio-code visual-studio-2015
1个回答
0
投票

感谢@dr.null,@TinyWang,@Jimi 问题解决了

namespace Utilities
{
    internal class Win32
    {
        public Win32()
        {
        }
        public static uint GetIdleTime()
        {
            LASTINPUTINFO lASTINPUTINFO = new LASTINPUTINFO()
            {
                cbSize = (uint)Marshal.SizeOf<LASTINPUTINFO>()
            };
            Win32.GetLastInputInfo(ref lASTINPUTINFO);
            return (uint)Environment.TickCount - lASTINPUTINFO.dwTime;
        }

        [DllImport("Kernel32.dll", CharSet=CharSet.None, ExactSpelling=false)]
        private static extern uint GetLastError();

        [DllImport("User32.dll", CharSet=CharSet.None, ExactSpelling=false)]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

        public static long GetLastInputTime()
        {
            LASTINPUTINFO lASTINPUTINFO = new LASTINPUTINFO()
            {
                cbSize = (uint)Marshal.SizeOf<LASTINPUTINFO>()
            };
            if (!Win32.GetLastInputInfo(ref lASTINPUTINFO))
            {
                throw new Exception(Win32.GetLastError().ToString());
            }
            return (long)lASTINPUTINFO.dwTime;
        }

        public static long GetTickCount()
        {
            return (long)Environment.TickCount;
        }

        [DllImport("User32.dll", CharSet=CharSet.None, ExactSpelling=false)]
        public static extern bool LockWorkStation();
    }
}


© www.soinside.com 2019 - 2024. All rights reserved.