0

I am working on setting Date and Time on Windows Embedded Standard 7 OS using C# .net.

I tried changing the system date using code in following link.

http://www.pinvoke.net/default.aspx/kernel32.setsystemtime

But I also saw that one should get the privileges to change the same. But I am receiving an error. here is the code to

Privileges.EnablePrivilege(SecurityEntity.SE_SYSTEMTIME_NAME);

        if (SetSystemTime(ref st) == 0)
        {
            return 0;
        }
        return 1;

For getting the privaleges I used the code from the following link.

http://www.pinvoke.net/default.aspx/advapi32/AdjustTokenPrivileges.html C# Sample Code 2 (With full error handling):

I have question: Is it possible to change the Date and Time using PInvoke. If it is possible what are the change/Setting I should do at the OS.

And what are the other ways to change the date and time.

thank you

Edit:

[DllImport("kernel32.dll")]
    extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);


    public int SetSystemDateTime(DateTime NewSystemDateTime)
    {
        SYSTEMTIME st = new SYSTEMTIME();
        st.wYear = (ushort)NewSystemDateTime.Year;
        st.wMonth = (ushort)NewSystemDateTime.Month;
        st.wDay = (ushort)NewSystemDateTime.Day;
        st.wHour = (ushort)NewSystemDateTime.Hour;
        st.wMinute = (ushort)NewSystemDateTime.Minute;
        st.wMilliseconds = (ushort)NewSystemDateTime.Millisecond;

        Privileges.EnablePrivilege(SecurityEntity.SE_SYSTEMTIME_NAME);

        if (SetSystemTime(ref st) == 0)
        {
            return 0;
        }
        return 1;

    }
Harsha
  • 1,861
  • 7
  • 28
  • 56
  • Can you post what did you set st to, maybe you are setting invalid date, and can you try with SetLocalTime and GetLocalTime – Luka Pivk Sep 13 '13 at 10:09
  • I have added the code which I used for setting the date and time. I also found System Local time (http://www.pinvoke.net/default.aspx/kernel32/SetLocalTime.html). Please let me know, I need to get the Privilege for setting these Local time. Thank you – Harsha Sep 13 '13 at 10:40
  • 1 more question, does it work if you run it as administrator? – Luka Pivk Sep 13 '13 at 10:45
  • Yes, appplication is running under admin mode. In my lab, I have PC connected to this device(which is running on Win Embbeded standard 7 and I do remote debugging. Do I need to change anything, any settings. Please let me know. thank you – Harsha Sep 13 '13 at 11:20
  • I found the solution and answered my question. thank you Luka Pivk. – Harsha Sep 13 '13 at 13:19

2 Answers2

1

Ok I did some research and I tried few things. You need admin permissions to change system date and time, one solution is to add

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

to application manifest file, this way the application will always ask user for permissions.

On code project there is an article that maybe able to help ypu without adding manifest file.

http://www.codeproject.com/Articles/125810/A-complete-Impersonation-Demo-in-C-NET

Luka Pivk
  • 466
  • 2
  • 19
1

I finally found the solution. Code in the following link works fine for me: http://www.pinvoke.net/default.aspx/kernel32/SetLocalTime.html

Also following code I found in one of the posts in stackoverflow, helped me.

using System.Security.Principal;
    bool IsUserAdministrator()
     {             
        bool isAdmin;
        try
        {
            WindowsIdentity user = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(user);
            isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch (UnauthorizedAccessException ex)
        {
            isAdmin = false;
        }
        catch (Exception ex)
        {
            isAdmin = false;
        }
        return isAdmin;
    }
Harsha
  • 1,861
  • 7
  • 28
  • 56