Search code examples
c#visual-studio-codevisual-studio-2015

Cannot implicitly convert type 'long' to 'uint' and Use of unassigned local variable in C#


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

Please Guide me

How can I solve the error and I am also a beginner in C# or There was something wrong in the implementation of the code I created or indeed the code I used was wrong

Thanks

The code below I use :

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)
            };
            
    }
}


Solution

  • Thanks for @dr.null , @TinyWang , @Jimi problem solved

    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();
        }
    }