newbie could somone please tell me why this code does not work

  • Thread starter Thread starter darren
  • Start date Start date
D

darren

Hi i have alterd some code that changed the system time so it changes
the date could you olease tell me why it does not work

using System;
using System.Runtime.InteropServices;





public struct SystemTime
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}


public class MyTimeAdjuster
{
[DllImport("kernel32.dll")]
public static extern bool GetSystemTime(ref
SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(ref
SystemTime systemTime);

void SetNewTime(short d, short m, short y)
{
SystemTime st = new SystemTime();
GetSystemTime(ref st);
st.wDay = d;
st.wMonth = m;
st.wYear = y;
SetSystemTime(ref st);
}

public static void Main(string[] args)
{
MyTimeAdjuster myTime = new
MyTimeAdjuster();
myTime.SetNewTime(1,12,2002);
}
}
 
You don't say what error, if any, you are getting, but try adding the
StructLayout attribute to your SystemType struct.

[StructLayout( LayoutKind.Sequential)]


HTH
Brian W
 
Hi
yes sorry it does not alter the system date

sorry i dont understand this i am new to all this

StructLayout attribute to your SystemType struct.
[StructLayout( LayoutKind.Sequential)]

here is trhe original code for altering the system time works fine so
i dont understnd why i cannot alter the date this way ?


Hi,
The .NET framework doesn't have managed interfaces for all of Win32
API, so
you have to write special code to use things that are built into
Windows.
Platform Invoke (P/Invoke) is the most common way to do this. To use
P/Invoke, you write a prototype that describes how the function should
be
called, and then the runtime uses this information to make the call.
The
other way to do this is by wrapping the functions using the Managed
Extensions to C++.

Here is what you can do to change the system time: (Look at this
article
"Using Win32 and Other Libraries" by Eric Gunnerson on MSDN for more
info.)

using System;
using System.Runtime.InteropServices;

public struct SystemTime {
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}


public class MyTimeAdjuster
{
[DllImport("kernel32.dll")]
public static extern bool GetSystemTime(ref SystemTime
systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(ref SystemTime
systemTime);

void SetNewTime(short hour, short minutes)
{
SystemTime st = new SystemTime();
GetSystemTime(ref st);
st.wHour = hour;
st.wMinute = minutes;
SetSystemTime(ref st);
}

public static void Main(string[] args)
{
MyTimeAdjuster myTime = new MyTimeAdjuster();
myTime.SetNewTime( 12, 50);
}
}

Hope this helps,
Martha









You don't say what error, if any, you are getting, but try adding the
StructLayout attribute to your SystemType struct.

[StructLayout( LayoutKind.Sequential)]


HTH
Brian W

Hi i have alterd some code that changed the system time so it changes
the date could you olease tell me why it does not work

using System;
using System.Runtime.InteropServices;





public struct SystemTime
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}


public class MyTimeAdjuster
{
[DllImport("kernel32.dll")]
public static extern bool GetSystemTime(ref
SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(ref
SystemTime systemTime);

void SetNewTime(short d, short m, short y)
{
SystemTime st = new SystemTime();
GetSystemTime(ref st);
st.wDay = d;
st.wMonth = m;
st.wYear = y;
SetSystemTime(ref st);
}

public static void Main(string[] args)
{
MyTimeAdjuster myTime = new
MyTimeAdjuster();
myTime.SetNewTime(1,12,2002);
}
}
 
Change your struct to look like this: -- Note the use of StructLayout
attribute

[StructLayout( LayoutKind.Sequential)]
public struct SystemTime
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}

Hi
yes sorry it does not alter the system date

sorry i dont understand this i am new to all this

StructLayout attribute to your SystemType struct.
[StructLayout( LayoutKind.Sequential)]

here is trhe original code for altering the system time works fine so
i dont understnd why i cannot alter the date this way ?


Hi,
The .NET framework doesn't have managed interfaces for all of Win32
API, so
you have to write special code to use things that are built into
Windows.
Platform Invoke (P/Invoke) is the most common way to do this. To use
P/Invoke, you write a prototype that describes how the function should
be
called, and then the runtime uses this information to make the call.
The
other way to do this is by wrapping the functions using the Managed
Extensions to C++.

Here is what you can do to change the system time: (Look at this
article
"Using Win32 and Other Libraries" by Eric Gunnerson on MSDN for more
info.)

using System;
using System.Runtime.InteropServices;

public struct SystemTime {
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}


public class MyTimeAdjuster
{
[DllImport("kernel32.dll")]
public static extern bool GetSystemTime(ref SystemTime
systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(ref SystemTime
systemTime);

void SetNewTime(short hour, short minutes)
{
SystemTime st = new SystemTime();
GetSystemTime(ref st);
st.wHour = hour;
st.wMinute = minutes;
SetSystemTime(ref st);
}

public static void Main(string[] args)
{
MyTimeAdjuster myTime = new MyTimeAdjuster();
myTime.SetNewTime( 12, 50);
}
}

Hope this helps,
Martha









You don't say what error, if any, you are getting, but try adding the
StructLayout attribute to your SystemType struct.

[StructLayout( LayoutKind.Sequential)]


HTH
Brian W

<darren> wrote in message news:[email protected]...
Hi i have alterd some code that changed the system time so it changes
the date could you olease tell me why it does not work

using System;
using System.Runtime.InteropServices;





public struct SystemTime
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}


public class MyTimeAdjuster
{
[DllImport("kernel32.dll")]
public static extern bool GetSystemTime(ref
SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(ref
SystemTime systemTime);

void SetNewTime(short d, short m, short y)
{
SystemTime st = new SystemTime();
GetSystemTime(ref st);
st.wDay = d;
st.wMonth = m;
st.wYear = y;
SetSystemTime(ref st);
}

public static void Main(string[] args)
{
MyTimeAdjuster myTime = new
MyTimeAdjuster();
myTime.SetNewTime(1,12,2002);
}
}
 
Back
Top