13 Replies - 2563 Views - Last Post: 04 January 2014 - 05:41 PM Rate Topic: -----

#1 vinoji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-December 13

with my code i can't change the system time on windows 7?

Posted 23 December 2013 - 03:37 AM

Here am reading the time and date format via serial port for every second. i already displayed that with both txt box and desktop time,
But i can't able to write this time on system clock i.e. RTC clock.
Please help me how to solve this, am using windows 7 pC.
I tried with reg edit function, but there only NTP time synch is there. am using Serial port.
and i want to known difference between user32.dll and kernel32.dll,
i studied in web sites, using kernel32.dll only we can do the synchronization at all. here i used user.dll?
any pblm will be there while using user32.dll???

Imports System
Imports System.IO.Ports
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Class form1
    Dim arry As String
    Dim b As String = Chr(&H24)
    Dim c As String = Chr(&HD)
    Dim d As String = Chr(&H3A)
    Dim rr As String = "NUL"
    Dim fg As String = "TIME SYNCHRONIZATION"
    Dim K As String = "TIME"
    Dim l As String = "DATE"
    Dim m As String = Chr(&H20)
    Dim n As String = "IN SYNC"
    Dim o As String = "STATUS"
    Dim p As String = Chr(&H2F)


    Private Property timer2_enabled As Boolean
    Private timesettings As New List(Of regionalsettings)
    Private Class regionalsettings
        Public entry As String
        Public value As String
        Public Sub New(ByVal key As String, ByVal value As String)
            Me.entry = key : Me.value = value
        End Sub

    End Class

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "com5"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.ReceivedBytesThreshold = 16
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox("port alrdy open")
        End Try

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim q As String
        Dim u As String
        timer2_enabled = True
        arry = SerialPort1.ReadExisting 'Automatically called every time a data is received at the serialPort

        If arry(0) = b Then
            RichTextBox1.Text = fg & System.Environment.NewLine & K & m & d & m & arry(3) & arry(4) & d & arry(5) & arry(6) & d & arry(7) & arry(8) & System.Environment.NewLine & l & d & arry(9) & arry(10) & d & arry(11) & arry(12) & d & arry(13) & arry(14)
            RichTextBox1.ForeColor = Color.DarkBlue
            RichTextBox1.BackColor = Color.LightSkyBlue
            q = arry(3) & arry(4) & d & arry(5) & arry(6) & d & arry(7) & arry(8)
            u = arry(9) & arry(10) & d & arry(11) & arry(12) & d & arry(13) & arry(14)

            With timesettings
                .Add(New regionalsettings("iCountry", "91"))
                .Add(New regionalsettings("iCurrDigits", "2"))
                .Add(New regionalsettings("iCurrency", "0"))
                .Add(New regionalsettings("iDate", "1"))
                .Add(New regionalsettings("iDigits", "2"))
                .Add(New regionalsettings("iLZero", "1"))
                .Add(New regionalsettings("iMeasure", "1"))
                .Add(New regionalsettings("iNegCurr", "0"))
                .Add(New regionalsettings("iTime", "0"))
                .Add(New regionalsettings("iTLZero", "0"))
                .Add(New regionalsettings("Locale", "00000091"))
                .Add(New regionalsettings("s1159", "AM"))
                .Add(New regionalsettings("s2359", "PM"))
                .Add(New regionalsettings("sCountry", "INDIA"))
                .Add(New regionalsettings("sCurrency", "RS"))
                .Add(New regionalsettings("sDate", "/"))
                .Add(New regionalsettings("sDecimal", "."))
                .Add(New regionalsettings("sLanguage", "ENG"))
                .Add(New regionalsettings("sList", ","))
                .Add(New regionalsettings("sLongDate", "dddd MMMM dd ,yyyy"))
                .Add(New regionalsettings("sShortDate", u))
                .Add(New regionalsettings("sThousand", ","))
                .Add(New regionalsettings("sTime", ":"))
                .Add(New regionalsettings("sTimeFormat", q))
                .Add(New regionalsettings("iTimePrefix", "0"))
                .Add(New regionalsettings("sMonDecimalSep", "."))
                .Add(New regionalsettings("sMonThousandSep", ","))
                .Add(New regionalsettings("iNegNumber", "1"))
                .Add(New regionalsettings("sNativeDigits", "0123456789"))
                .Add(New regionalsettings("NumShape", "1"))
                .Add(New regionalsettings("iCalendarType", "1"))
                .Add(New regionalsettings("iFirstDayOfWeek", "6"))
                .Add(New regionalsettings("iFirstWeekOfYear", "0"))
                .Add(New regionalsettings("sGrouping", "3;0"))
                .Add(New regionalsettings("sMonGrouping", "3;0"))
                .Add(New regionalsettings("sPositiveSign", ""))
                .Add(New regionalsettings("sNegativeSign", "-"))
            End With
            ' Setdevicetime(p)
            'Label1.Text = arry(3) & arry(4) + arry(5) & arry(6) + arry(7) & arry(8)
            Label2.Text = o & d & n
            Label2.ForeColor = Color.Blue
        Else
            Label2.Text = o & d & rr
            Label2.ForeColor = Color.Green
        End If
        For Each reg As regionalsettings In timesettings
            Microsoft.Win32.Registry.SetValue("HKEY_current_User\Control panel\International", reg.entry, reg.value)
        Next

        Notifyinternationalchanges()
    End Sub



    Private Sub NotifyInternationalchanges()
        'Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ms725497%28v=vs.85%29.aspx
        Dim HWND_BROADCAST As New IntPtr(&HFFFF) 'broadcast to entire system
        Dim Lparam As IntPtr = System.Runtime.InteropServices.Marshal.StringToBSTR("intl")
        sendNotifyMessage(HWND_BROADCAST, &H1A, UIntPtr.Zero, Lparam)
        System.Runtime.InteropServices.Marshal.FreeBSTR(Lparam)
    End Sub

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function sendNotifyMessage(ByVal hWnd As IntPtr, _
                                       ByVal msg As UInt32, _
                                        ByVal wParam As UIntPtr, _
                                        ByVal lParam As IntPtr) As Boolean

    End Function

End Class

This post has been edited by AdamSpeight2008: 23 December 2013 - 08:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: with my code i can't change the system time on windows 7?

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: with my code i can't change the system time on windows 7?

Posted 23 December 2013 - 04:52 AM

Hi,
I have not used this method to change the time/date before but, is there a reason that the answers a gave you HERE did not work for you?
Was This Post Helpful? 0
  • +
  • -

#3 vinoji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-December 13

Re: with my code i can't change the system time on windows 7?

Posted 23 December 2013 - 06:04 AM

Thanks for your reply,,

i tried that both code, but it showing error,n that second code always it shows the message box only( time/date is not valid).
and so am receiving serial data continously. bt on that that code button option is there, thats y i tried this bro....
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: with my code i can't change the system time on windows 7?

Posted 23 December 2013 - 07:19 AM

Out of curiosity try putting this in a button click event on a new project with 1 TextBox on it and see if the date and time is displayed in the same format that you are sending the date and time to the SetDeviceTime sub in the 2nd example i showed.


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = Now.ToString
    End Sub



Mine displays "12/23/2013 9:13:46 AM" in the textbox. That is the format which i sent the date/time to the SetDeviceTime sub. The examples i posted work good on my machine so perhaps you just need to change the format of the date/time string your sending. B)
Was This Post Helpful? 0
  • +
  • -

#5 vinoji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-December 13

Re: with my code i can't change the system time on windows 7?

Posted 26 December 2013 - 12:46 AM

hi bro,

with that program code i can able to get system time. but it is not writing time and information what ever we are given in that strings.
tell me one thing, go through of your code we need to manually enter the time & date strings,, i agree with this concept we can easly understand how ths system time get changing.
pls tell me while you using this codes both desktop and internal clock both get changed for you ah??
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: with my code i can't change the system time on windows 7?

Posted 31 December 2013 - 07:01 AM

Quote

with that program code i can able to get system time. but it is not writing time and information what ever we are given in that strings.

First i want to say sorry for the delay. Anyways, i did forget to mention that i am pretty sure you need to run this as an Administrator or it will not set the systems time. If you right click on the Visual Basic shortcut you can set it to run as Administrator. Then open Visual Basic and try running your program.

Quote

tell me one thing, go through of your code we need to manually enter the time & date strings,, i agree with this concept we can easly understand how ths system time get changing.

This is going to be up to you to figure out which controls you use to let the user enter the date and time. As far as the date you could use a DateTimePicker control and for the time you could use a TextBox or a MaskedTextBox which has a mask for only letting the user enter a time. Then it is a mater of combining the date and time into the proper string format to send to the SetDeviceTime sub.

Quote

pls tell me while you using this codes both desktop and internal clock both get changed for you ah??

I am not sure what you mean by internal clock but yes it sets the time on my computer as soon as i set it to a new time.

B)

This post has been edited by IronRazer: 31 December 2013 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,891
  • Joined: 12-December 12

Re: with my code i can't change the system time on windows 7?

Posted 31 December 2013 - 07:12 AM

IronRazer said:

for the time you could use a TextBox or a MaskedTextBox which has a mask for only letting the user enter a time.

I avoid the MaskedTextBox. If you use a DateTimePicker and set ShowUpDown, True and Format, Time it will not show the calendar and only allow a valid time.

You can also set Format, Custom and CustomFormat to, for example, HH:mm.

Custom Date and Time Formats

This post has been edited by andrewsw: 31 December 2013 - 07:19 AM

Was This Post Helpful? 1
  • +
  • -

#8 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: with my code i can't change the system time on windows 7?

Posted 31 December 2013 - 07:54 AM

@ andrewsw,
Good idea. Why i didn`t think of using a DateTimePicker for setting the time i don`t know. Maybe i am wrong but, i believe you would still need to use 2 DateTimePicker`s 1 for choosing the Date and one for choosing the Time because if i remember correctly the one used for setting the time will always return a value with the current date and the newly set time.

I do agree that the MaskedTextBox is kind of a pain and looks ugly too, and a Textbox would require some error checking to make sure the user entered the valid time format. ;)
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,891
  • Joined: 12-December 12

Re: with my code i can't change the system time on windows 7?

Posted 31 December 2013 - 08:01 AM

Yes I was assuming, as you suggested, using two controls and ignoring the date aspect for the time-version.

It is possible to use one DTP, without the calendar and with a custom format of, for example:

dd MMM yy HH:mm
Clicking on Dec and using the up/down arrows will change to Jan, Feb, etc.
Was This Post Helpful? 1
  • +
  • -

#10 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: with my code i can't change the system time on windows 7?

Posted 03 January 2014 - 08:07 AM

I didn't read it all, but to make windows clock changes, you need it to run as administrator using Windows Vista and above.

I personally never done this, so i can't help you.

This post has been edited by HeBTeMy: 03 January 2014 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

#11 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: with my code i can't change the system time on windows 7?

Posted 04 January 2014 - 07:30 AM

View PostHeBTeMy, on 03 January 2014 - 10:07 AM, said:

I didn't read it all, but to make windows clock changes, you need it to run as administrator using Windows Vista and above.

I personally never done this, so i can't help you.

This needs to be run as an Administrator in XP also. I am always logged in as an Administrator so i often forget to mention that. :)

This post has been edited by IronRazer: 04 January 2014 - 07:30 AM

Was This Post Helpful? 0
  • +
  • -

#12 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: with my code i can't change the system time on windows 7?

Posted 04 January 2014 - 07:57 AM

View PostIronRazer, on 04 January 2014 - 02:30 PM, said:

View PostHeBTeMy, on 03 January 2014 - 10:07 AM, said:

I didn't read it all, but to make windows clock changes, you need it to run as administrator using Windows Vista and above.

I personally never done this, so i can't help you.

This needs to be run as an Administrator in XP also. I am always logged in as an Administrator so i often forget to mention that. :)/>


in XP? Are you sure?
I don't have it right now, but the programs used to inherit the priviledges from the host running on XP...
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1458
  • View blog
  • Posts: 3,700
  • Joined: 01-February 13

Re: with my code i can't change the system time on windows 7?

Posted 04 January 2014 - 08:30 AM

Quote

in XP? Are you sure?
I don't have it right now, but the programs used to inherit the priviledges from the host running on XP...

Yea, i am running XP and it does not throw an error but, will not set the time unless you have Administrator privileges.
Was This Post Helpful? 0
  • +
  • -

#14 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: with my code i can't change the system time on windows 7?

Posted 04 January 2014 - 05:41 PM

View PostIronRazer, on 04 January 2014 - 03:30 PM, said:

Quote

in XP? Are you sure?
I don't have it right now, but the programs used to inherit the priviledges from the host running on XP...

Yea, i am running XP and it does not throw an error but, will not set the time unless you have Administrator privileges.


Well, that's odd... now you make me want to power up my 9 year old P4 with WinXP SP3... ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1