Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
GeneralSearch MyFileName*.* file Pin
wk_vigorous30-Apr-04 1:21
wk_vigorous30-Apr-04 1:21 
GeneralRe: Search MyFileName*.* file Pin
Colin Angus Mackay30-Apr-04 1:33
Colin Angus Mackay30-Apr-04 1:33 
GeneralRe: Search MyFileName*.* file Pin
Stefan Troschuetz30-Apr-04 1:57
Stefan Troschuetz30-Apr-04 1:57 
GeneralThread problem Pin
azusakt30-Apr-04 0:44
azusakt30-Apr-04 0:44 
GeneralRe: Thread problem Pin
Corinna John30-Apr-04 1:41
Corinna John30-Apr-04 1:41 
GeneralRe: Thread problem Pin
Jan Vercauteren30-Apr-04 2:49
Jan Vercauteren30-Apr-04 2:49 
GeneralRe: Thread problem Pin
Heath Stewart30-Apr-04 3:53
protectorHeath Stewart30-Apr-04 3:53 
Generalupdating system time in c# Pin
bennyrascal30-Apr-04 0:37
bennyrascal30-Apr-04 0:37 
Am trying to update the system time using c#. Have a bit of code written in VB.NET that I’ve plundered ( from Alastair Dallas) , which works. The VB code is:

<structlayout(layoutkind.sequential)> _
Public Structure SYSTEMTIME
Public wYear As Int16
Public wMonth As Int16
Public wDayOfWeek As Int16
Public wDay As Int16
Public wHour As Int16
Public wMinute As Int16
Public wSecond As Int16
Public wMilliseconds As Int16
End Structure

Private Declare Function GetSystemTime Lib "kernel32.dll" (ByRef stru As SYSTEMTIME) As Int32
Private Declare Function SetSystemTime Lib "kernel32.dll" (ByRef stru As SYSTEMTIME) As Int32

Public Shared Sub SetWindowsClock(ByVal dt As DateTime)
'Sets system time. Note: Use UTC time; Windows will apply time zone

Dim timeStru As SYSTEMTIME
Dim result As Int32

timeStru.wYear = CType(dt.Year, Int16)
timeStru.wMonth = CType(dt.Month, Int16)
timeStru.wDay = CType(dt.Day, Int16)
timeStru.wDayOfWeek = CType(dt.DayOfWeek, Int16)
timeStru.wHour = CType(dt.Hour + 2, Int16) ' used to verify system time is actually changed
timeStru.wMinute = CType(dt.Minute, Int16)
timeStru.wSecond = CType(dt.Second, Int16)
timeStru.wMilliseconds = CType(dt.Millisecond, Int16)

result = SetSystemTime(timeStru)

End Sub
**************************************************
Have converted this to C# as follows:
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public Int16 wYear;
public Int16 wMonth;
public Int16 wDayOfWeek;
public Int16 wDay;
public Int16 wHour;
public Int16 wMinute;
public Int16 wSecond;
public Int16 wMilliseconds;
}
[DllImport("kernel32.dll")]
public static extern int SetSystemTime(SYSTEMTIME lpSystemTime);


public static void SetWindowsClock(DateTime dt)
{

SYSTEMTIME timeStru;
int result;
timeStru.wYear = ((Int16)dt.Year);
timeStru.wMonth = ((Int16)dt.Month);
timeStru.wDay = ((Int16)dt.Day);
timeStru.wDayOfWeek = ((Int16)dt.DayOfWeek);
timeStru.wHour = ((Int16)dt.Hour);
timeStru.wMinute = ((Int16)dt.Minute);
timeStru.wSecond = ((Int16)dt.Second);
timeStru.wMilliseconds = ((Int16)dt.Millisecond);
result = SetSystemTime(timeStru);
}

The C# code crashes on the SetSystemTime(timeStru). While tracing, the only thing I noticed which was different between the 2 was the way C# handles structures is different to VB.
Anyone have a clue?

benny
GeneralRe: updating system time in c# Pin
John Baird30-Apr-04 2:47
John Baird30-Apr-04 2:47 
GeneralRe: updating system time in c# Pin
bennyrascal30-Apr-04 3:21
bennyrascal30-Apr-04 3:21 
GeneralRe: updating system time in c# Pin
Heath Stewart30-Apr-04 3:59
protectorHeath Stewart30-Apr-04 3:59 
GeneralRe: updating system time in c# Pin
bennyrascal30-Apr-04 14:20
bennyrascal30-Apr-04 14:20 
GeneralInvoking Console Application at runtime Pin
Aryadip29-Apr-04 23:28
Aryadip29-Apr-04 23:28 
GeneralRe: Invoking Console Application at runtime Pin
leppie30-Apr-04 7:08
leppie30-Apr-04 7:08 
GeneralRe: Invoking Console Application at runtime Pin
Aryadip4-May-04 20:24
Aryadip4-May-04 20:24 
GeneralArchitectural question (crisis) Pin
Jan Vercauteren29-Apr-04 21:48
Jan Vercauteren29-Apr-04 21:48 
GeneralRe: Architectural question (crisis) Pin
Anonymous29-Apr-04 23:05
Anonymous29-Apr-04 23:05 
GeneralRe: Architectural question (crisis) Pin
Anonymous29-Apr-04 23:06
Anonymous29-Apr-04 23:06 
GeneralRe: Architectural question (crisis) Pin
Jan Vercauteren30-Apr-04 0:37
Jan Vercauteren30-Apr-04 0:37 
GeneralRe: Architectural question (crisis) Pin
bjoernen30-Apr-04 1:54
bjoernen30-Apr-04 1:54 
GeneralRe: Architectural question (crisis) Pin
Jan Vercauteren30-Apr-04 2:46
Jan Vercauteren30-Apr-04 2:46 
GeneralRe: Architectural question (crisis) Pin
Heath Stewart30-Apr-04 4:05
protectorHeath Stewart30-Apr-04 4:05 
GeneralRe: Architectural question (crisis) Pin
Jan Vercauteren30-Apr-04 4:55
Jan Vercauteren30-Apr-04 4:55 
GeneralRe: Architectural question (crisis) Pin
Heath Stewart30-Apr-04 5:10
protectorHeath Stewart30-Apr-04 5:10 
GeneralRe: Architectural question (crisis) Pin
bjoernen30-Apr-04 11:51
bjoernen30-Apr-04 11:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.