你能告诉我为什么下面的代码无效吗? [英] Can you tell me why following code not working?

查看:63
本文介绍了你能告诉我为什么下面的代码无效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Imports  System.Runtime.InteropServices 

Public Form1

' 用于传递给P / Invoke的系统时间结构......

< structlayoutattribute(layoutkind.sequential)> _
私有 结构 SYSTEMTIME

公共作为

公开作为

公开 dayOfWeek 作为

公共作为 简短

公共小时作为

公共分钟作为

公共第二作为

< span class =code-keyword>公共毫秒作为

结束 结构

' P / Invoke dec用于设置系统时间......

< dllimport( kernel32.dll)> _
私有 共享 功能 SetLocalTime( ByRef time As SYSTEMTIME) As 布尔

结束 功能

公共 功能 SetDeviceTime( ByVal p_NewDate 作为 日期

' 填充结构...

' 替换< your date =object =>通过GPRS返回日期对象...

Dim st As SYSTEMTIME

st.year = p_NewDate.Year

st.month = p_NewDate.Month

st.dayOfWeek = p_NewDate.DayOfWeek

st.day = p_NewDate.Day

st.hour = p_NewDate.Hour

st.minute = p_NewDate.Minute

st.second = p_NewDate.Second

st.milliseconds = p_NewDate.Millisecond

' 设置新时间......

SetLocalTime(st)

' 返回SetLocalTime(st)

结束 函数

私有 Sub Button1_Cl ick( ByVal sender As System。 Object ByVal e As System.EventArgs) Handles Button1.Click

SetDeviceTime( 01/01/2010

结束 Sub

结束

解决方案

您需要扩展权限才能更改系统时间,请参阅 https: //msdn.microsoft.com/en-us/library/windows/desktop/ms724936(v=vs.85).aspx [ ^ ]。

Imports System.Runtime.InteropServices

Public Class Form1

    'System time structure used to pass to P/Invoke...

    <structlayoutattribute(layoutkind.sequential)> _
    Private Structure SYSTEMTIME

        Public year As Short

        Public month As Short

        Public dayOfWeek As Short

        Public day As Short

        Public hour As Short

        Public minute As Short

        Public second As Short

        Public milliseconds As Short

    End Structure

    'P/Invoke dec for setting the system time...

    <dllimport("kernel32.dll")> _
    Private Shared Function SetLocalTime(ByRef time As SYSTEMTIME) As Boolean

    End Function

    Public Function SetDeviceTime(ByVal p_NewDate As Date)

        'Populate structure...

        'Substitute <your date="" object=""> with your date object returned via GPRS...

        Dim st As SYSTEMTIME

        st.year = p_NewDate.Year

        st.month = p_NewDate.Month

        st.dayOfWeek = p_NewDate.DayOfWeek

        st.day = p_NewDate.Day

        st.hour = p_NewDate.Hour

        st.minute = p_NewDate.Minute

        st.second = p_NewDate.Second

        st.milliseconds = p_NewDate.Millisecond

        'Set the new time...

        SetLocalTime(st)

        'Return SetLocalTime(st)

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SetDeviceTime("01/01/2010")

    End Sub

End Class

解决方案

You need extended privileges to change the system time, see https://msdn.microsoft.com/en-us/library/windows/desktop/ms724936(v=vs.85).aspx[^].


这篇关于你能告诉我为什么下面的代码无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆