how to set the system time using WIN32 API Dll calls?  
Author Message
theblackhat





PostPosted: Visual C# General, how to set the system time using WIN32 API Dll calls? Top

hi all,

i have written a code for setting the system time.

the server will send the time to the client and that time is collected at the client side and the new time is set in the client side...

server side code:

using System;

using System.Collections.Generic;

using System.Text;

using System.Net.Sockets;

using System.Collections;

using System.Net;

using System.Timers;

using System.Threading;

namespace Server

{

class Program

{

static ASCIIEncoding ASCII = new ASCIIEncoding();

static Byte[] outstream = new Byte[256];

static TcpListener tcpl = null;

public Program()

{

}

public static void Main(string[] args)

{

tcpl = new TcpListener(9999);

tcpl.Start();

ConnectAndSend();

}

static void ConnectAndSend()

{

while (true)

{

try

{

Console.WriteLine(DateTime.Now.ToString());

Socket sock = tcpl.AcceptSocket();

Console.WriteLine("New Client At : " + sock.Remo****dPoint.ToString());

outstream = ASCII.GetBytes(DateTime.Now.ToString());

sock.Send(outstream, outstream.Length, 0);

}

catch (Exception e)

{

Console.WriteLine("Generic Message: {0}", e.Message);

}

}

}

}

}

client side code :

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.Diagnostics;

using System.Runtime.InteropServices;

namespace Client

{

class Program

{

static void Main(string[] args)

{

Connect(IPAddress.Loopback.ToString(),"hello World the blackhat !");

}

static void Connect(String server, String message)

{

try

{

IPEndPoint ClientEndPoint = new IPEndPoint(IPAddress.Parse("10.100.66.41"), 9999);

Int32 port = 9999;

TcpClient client = new TcpClient();

client.Connect(IPAddress.Parse("10.100.66.41"), 9999);

Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

NetworkStream stream = client.GetStream();

data = new Byte[256];

String responseData = String.Empty;

Int32 bytes = stream.Read(data, 0, data.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

DateTime dt = DateTime.Parse(responseData);

SYSTEMTIME st = new SYSTEMTIME();

st.day =(ushort)dt.Day;

st.dayOfWeek =(ushort) dt.DayOfWeek;

st.month = (ushort)dt.Month;

st.year = (ushort)dt.Year;

st.hour = (ushort)dt.Hour;

st.minute = (ushort)dt.Minute;

st.second = (ushort)dt.Second;

st.milliseconds = (ushort)dt.Millisecond;

bool setTime = LibWrap.SetLocalTime(ref st);

//Process p = Process.Start("CMD /c \"TIME " + newTime + "\""); here i have tried to set the time as a command line code.i failed doin tat..!

Console.WriteLine(st.hour + ":" + st.minute + ":" + st.second);

Console.WriteLine("Time set successfully..."+setTime);

stream.Close();

client.Close();

}

catch (ArgumentNullException e)

{

Console.WriteLine("ArgumentNullException: {0}", e);

}

catch (SocketException e)

{

Console.WriteLine("SocketException: {0}", e);

}

Console.WriteLine("\n Press Enter to continue...");

Console.Read();

}

}

[StructLayout( LayoutKind.Sequential )]

public struct SYSTEMTIME

{

public ushort year;

public ushort month;

public ushort dayOfWeek;

public ushort day;

public ushort hour;

public ushort minute;

public ushort second;

public ushort milliseconds;

}

public class LibWrap

{

[DllImport("Kernel32.dll")]

public static extern bool SetLocalTime(ref SYSTEMTIME st);

}

}

this is my code.

this code doesnt give me any error and the client code returns true. but the time doesnt change in the client.

is the problem due to lack of admin privileges or is it due to the wrong code i have written



Visual C#6  
 
 
ahmedilyas





PostPosted: Visual C# General, how to set the system time using WIN32 API Dll calls? Top

at a first glance, it seems ok, just be sure to debug and check the values of variables sent through from server to client. Take a look at this thread on how to set the system time:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=531057&SiteID=1

you may also wish to setlasterror = true in the DllImport, so you can then do a System.Runtime.InteropServices.Marshal.GetLastWin32Error() to see what the error was, if any.



 
 
theblackhat





PostPosted: Visual C# General, how to set the system time using WIN32 API Dll calls? Top

yeah, after setting the setlasterror as true i got the error code as 1004.

wat this error code denote..

thank you


 
 
ahmedilyas





PostPosted: Visual C# General, how to set the system time using WIN32 API Dll calls? Top

it means invalid flags! you may need to look at the format/culture of the DateTime object. Are you able to set one locally for testing So create a datetime object like so:

DateTime theDateTime = DateTime.Now.AddHours(3); //add 3 hours

//then set the system date with the above date

what happens still the same thing



 
 
theblackhat





PostPosted: Visual C# General, how to set the system time using WIN32 API Dll calls? Top

its working now..!

i don have any idea how it worked.. i just got the error code as 1004 ans now im getting 0 as error code an the time is set properly.

thanks.

ps: i would like to know the error code status for 1004..


 
 
theblackhat





PostPosted: Visual C# General, how to set the system time using WIN32 API Dll calls? Top

i didnt change anything in the code but its working fine....can u provide me the link where v can find the details abt all error codes.. it may be useful for me in the future too..
 
 
ahmedilyas





PostPosted: Visual C# General, how to set the system time using WIN32 API Dll calls? Top

http://help.netop.com/support/errorcodes/win32_error_codes.htm