No puedo hacer que SetSystemTime funcione en Windows Vista usando C # con Interop (P / Invoke)

Me está costando que SetSystemTime funcione en mi código C #. SetSystemtime es una función kernel32.dll. Estoy usando P / invoke (interop) para llamarlo. SetSystemtime devuelve falso y el error es "Parámetro no válido". He publicado el código a continuación. Insisto en que GetSystemTime funciona bien. He probado esto en Vista y Windows 7. Según algunas publicaciones de grupos de noticias que he visto, apagué UAC. Ninguna diferencia. He buscado un poco este problema. Encontré este enlace:http://groups.google.com.tw/group/microsoft.public.dotnet.framework.interop/browse_thread/thread/805fa8603b00c267

donde se informa el problema pero parece que no se encuentra ninguna resolución. Tenga en cuenta que UAC también se menciona, pero no estoy seguro de que este sea el problema. También tenga en cuenta que este caballero no obtiene Win32Error real.

¿Alguien puede probar mi código en XP?¿Alguien puede decirme qué estoy haciendo mal y cómo solucionarlo? Si la respuesta es cambiar de algún modo la configuración de permisos mediante programación, necesitaría un ejemplo. Sin embargo, habría pensado que apagar UAC debería cubrir eso.No estoy obligado a usar esta forma particular (SetSystemTime). Solo estoy tratando de introducir alguna "deriva del reloj" para probar algo. Si hay otra forma de hacerlo, dígamelo. Francamente, me sorprende que necesite usar Interop para cambiar la hora del sistema. Pensé que hay un método .NET.

Muchas gracias por cualquier ayuda o idea. Andrés

Código:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SystemTimeInteropTest
{
    class Program
    {
        #region ClockDriftSetup
        [StructLayout(LayoutKind.Sequential)]
        public struct SystemTime
        {
            [MarshalAs(UnmanagedType.U2)]
            public short Year;
            [MarshalAs(UnmanagedType.U2)]
            public short Month;
            [MarshalAs(UnmanagedType.U2)]
            public short DayOfWeek;
            [MarshalAs(UnmanagedType.U2)]
            public short Day;
            [MarshalAs(UnmanagedType.U2)]
            public short Hour;
            [MarshalAs(UnmanagedType.U2)]
            public short Minute;
            [MarshalAs(UnmanagedType.U2)]
            public short Second;
            [MarshalAs(UnmanagedType.U2)]
            public short Milliseconds;
        }

        [DllImport("kernel32.dll")]
        public static extern void GetLocalTime(
        out SystemTime systemTime);

        [DllImport("kernel32.dll")]
        public static extern void GetSystemTime(
        out SystemTime systemTime);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetSystemTime(
        ref SystemTime systemTime);

        //[DllImport("kernel32.dll", SetLastError = true)]
        //public static extern bool SetLocalTime(
        //ref SystemTime systemTime);
        [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetLocalTime")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool SetLocalTime([InAttribute()] ref SystemTime lpSystemTime);



        #endregion ClockDriftSetup
        static void Main(string[] args)
        {
            try
            {
            SystemTime sysTime;
             GetSystemTime(out sysTime);
                sysTime.Milliseconds += (short)80;
                sysTime.Second += (short)3000;
                bool bResult = SetSystemTime(ref sysTime);

                if (bResult == false)
                    throw new System.ComponentModel.Win32Exception();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Drift Error: " + ex.Message);
            }
        }
    }
}

Respuestas a la pregunta(0)

Su respuesta a la pregunta