学习vb有一年了,遗憾的是很肤浅,我是学生,寝室里每晚11点熄灯,又是忘关机是,不小心停电了,我就做了个小程序,只是能够到时跳出一个窗体提示,去不能自动关机,请老鸟高手们给出点代码或指点,把这点遗憾不上去,就是能在一定时间里把winxp的关机程序shoutdown调出来,或是自动关机。我想这需要.dll,但我不会!谢谢!!!谢谢!!!
一鞠躬。二鞠躬!……※※

解决方案 »

  1.   

    用这个API函数.ExitWindowsEx VB声明 
    Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long 
    说明 
    退出windows,并用特定的选项重新启动 
    返回值 
    Long,非零表示成功,零表示失败。会设置GetLastError 
    参数表 
    参数 类型及说明 
    uFlags Long,指定下述一个或多个标志(用OR运算符合并到一起) 
    EWX_FORCE 强迫中止没有响应的进程 
    EWX_LOGOFF 中止进程,然后注销 
    EWX_SHUTDOWN 关掉系统电源(如果可能的话,ATX电源就可以) 
    EWX_REBOOT 重新引导系统 
    EWX_SHUTDOWN 关闭系统 
    dwReserved Long,保留,设为零 
    注解 
    这个函数调用后会立刻返回,系统关闭过程是在后台进行的。注意先中止自己的应用程序,使关闭过程更显平顺。当然,您的进程必须有足够的优先权,否则也不能执行这种操作 
      

  2.   

    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=166814http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=18622
      

  3.   

    调用windows的shoutdown用
    shell  "shoutdown.exe" '当然应该加上路径
      

  4.   

    主要就是取得相应的权限后,才能关机:Option  Explicit  
    Private  Const  TOKEN_ADJUST_PRIVILEGES  As  Long  =  &H20  
    Private  Const  TOKEN_QUERY  As  Long  =  &H8  
    Private  Const  SE_PRIVILEGE_ENABLED  As  Long  =  &H2   
    Private  Const  EWX_LOGOFF  As  Long  =  &H0  
    Private  Const  EWX_SHUTDOWN  As  Long  =  &H1  
    Private  Const  EWX_REBOOT  As  Long  =  &H2  
    Private  Const  EWX_FORCE  As  Long  =  &H4  
    Private  Const  EWX_POWEROFF  As  Long  =  &H8  
    Private  Const  EWX_FORCEIFHUNG  As  Long  =  &H10  '2000/XP  only  
     
    Private  Const  VER_PLATFORM_WIN32_NT  As  Long  =  2  
     
    Private  Type  OSVERSIONINFO  
       OSVSize                  As  Long  
       dwVerMajor            As  Long  
       dwVerMinor            As  Long  
       dwBuildNumber      As  Long  
       PlatformID            As  Long  
       szCSDVersion        As  String  *  128  
    End  Type  
     
    Private  Type  LUID  
         dwLowPart  As  Long  
         dwHighPart  As  Long  
    End  Type  
     
    Private  Type  LUID_AND_ATTRIBUTES  
         udtLUID  As  LUID  
         dwAttributes  As  Long  
    End  Type  
     
    Private  Type  TOKEN_PRIVILEGES  
         PrivilegeCount  As  Long  
         laa  As  LUID_AND_ATTRIBUTES  
    End  Type  
                 
    Private  Declare  Function  ExitWindowsEx  Lib  "user32"  _  
         (ByVal  dwOptions  As  Long,  _  
         ByVal  dwReserved  As  Long)  As  Long  
     
    Private  Declare  Function  GetCurrentProcess  Lib  "kernel32"  ()  As  Long  
     
    Private  Declare  Function  OpenProcessToken  Lib  "advapi32"  _  
       (ByVal  ProcessHandle  As  Long,  _  
         ByVal  DesiredAccess  As  Long,  _  
         TokenHandle  As  Long)  As  Long  
     
    Private  Declare  Function  LookupPrivilegeValue  Lib  "advapi32"  _  
         Alias  "LookupPrivilegeValueA"  _  
       (ByVal  lpSystemName  As  String,  _  
         ByVal  lpName  As  String,  _  
         lpLuid  As  LUID)  As  Long  
     
    Private  Declare  Function  AdjustTokenPrivileges  Lib  "advapi32"  _  
       (ByVal  TokenHandle  As  Long,  _  
         ByVal  DisableAllPrivileges  As  Long,  _  
         NewState  As  TOKEN_PRIVILEGES,  _  
         ByVal  BufferLength  As  Long,  _  
         PreviousState  As  Any,  _  
         ReturnLength  As  Long)  As  Long  
     
    Private  Declare  Function  GetVersionEx  Lib  "kernel32"  _  
         Alias  "GetVersionExA"  _  
       (lpVersionInformation  As  OSVERSIONINFO)  As  Long  
         
         
         
    Private  Sub  Command1_Click()  
     
         Dim  uflags  As  Long  
         Dim  success  As  Long  
           
         If  Option1.Value  =  True  Then  uflags  =  EWX_LOGOFF  
         If  Option2.Value  =  True  Then  uflags  =  EWX_SHUTDOWN  
         If  Option3.Value  =  True  Then  uflags  =  EWX_REBOOT  
         If  Option4.Value  =  True  Then  uflags  =  EWX_POWEROFF  
           
         If  Check1.Value  =  vbChecked  Then  uflags  =  uflags  Or  EWX_FORCE  
         If  Check2.Value  =  vbChecked  Then  uflags  =  uflags  Or  EWX_FORCEIFHUNG  
           
       'assume  success  
         success  =  True  
         
       'if  running  under  NT  or  better,  
       'the  shutdown  privledges  need  to  
       'be  adjusted  to  allow  the  ExitWindowsEx  
       'call.  If  the  adjust  call  fails  on  a  NT+  
       'system,  success  holds  False,  preventing  shutdown.  
         If  IsWinNTPlus  Then  
               success  =  EnableShutdownPrivledges()  
         End  If  
     
         If  success  Then  Call  ExitWindowsEx(uflags,  0&)  
       
    End  Sub  
         
     
    Private  Function  IsWinNTPlus()  As  Boolean  
     
       'returns  True  if  running  Windows  NT,  
       'Windows  2000,  Windows  XP,  or  .net  server  
         #If  Win32  Then  
         
               Dim  OSV  As  OSVERSIONINFO  
           
               OSV.OSVSize  =  Len(OSV)  
           
               If  GetVersionEx(OSV)  =  1  Then  
     
                     IsWinNTPlus  =  (OSV.PlatformID  =  VER_PLATFORM_WIN32_NT)  And  _  
                                               (OSV.dwVerMajor  >=  4)  
               End  If  
     
         #End  If  
     
    End  Function  
     
     
    Private  Function  EnableShutdownPrivledges()  As  Boolean  
     
         Dim  hProcessHandle  As  Long  
         Dim  hTokenHandle  As  Long  
         Dim  lpv_la  As  LUID  
         Dim  token  As  TOKEN_PRIVILEGES  
           
         hProcessHandle  =  GetCurrentProcess()  
           
         If  hProcessHandle  <>  0  Then  
           
             'open  the  access  token  associated  
             'with  the  current  process.  hTokenHandle  
             'returns  a  handle  identifying  the  
             'newly-opened  access  token  
               If  OpenProcessToken(hProcessHandle,  _  
                                                   (TOKEN_ADJUST_PRIVILEGES  Or  TOKEN_QUERY),  _  
                                                     hTokenHandle)  <>  0  Then  
           
                   'obtain  the  locally  unique  identifier  
                   '(LUID)  used  on  the  specified  system  
                   'to  locally  represent  the  specified  
                   'privilege  name.  Passing  vbNullString  
                   'causes  the  api  to  attempt  to  find  
                   'the  privilege  name  on  the  local  system.  
                     If  LookupPrivilegeValue(vbNullString,  _  
                                                                     "SeShutdownPrivilege",  _  
                                                                     lpv_la)  <>  0  Then  
                       
                         'TOKEN_PRIVILEGES  contains  info  about  
                         'a  set  of  privileges  for  an  access  token.  
                         'Prepare  the  TOKEN_PRIVILEGES  structure  
                         'by  enabling  one  privilege.  
                           With  token  
                                 .PrivilegeCount  =  1  
                                 .laa.udtLUID  =  lpv_la  
                                 .laa.dwAttributes  =  SE_PRIVILEGE_ENABLED  
                           End  With  
           
                         'Enable  the  shutdown  privilege  in  
                         'the  access  token  of  this  process.  
                         'hTokenHandle:  access  token  containing  the  
                         '    privileges  to  be  modified  
                         'DisableAllPrivileges:  if  True  the  function  
                         '    disables  all  privileges  and  ignores  the  
                         '    NewState  parameter.  If  FALSE,  the  
                         '    function  modifies  privileges  based  on  
                         '    the  information  pointed  to  by  NewState.  
                         'token:    TOKEN_PRIVILEGES  structure  specifying  
                         '    an  array  of  privileges  and  their  attributes.  
                         '  
                         'Since  were  just  adjusting  to  shut  down,  
                         'BufferLength,  PreviousState  and  ReturnLength  
                         'can  be  passed  as  null.  
                           If  AdjustTokenPrivileges(hTokenHandle,  _  
                                                                             False,  _  
                                                                             token,  _  
                                                                             ByVal  0&,  _  
                                                                             ByVal  0&,  _  
                                                                             ByVal  0&)  <>  0  Then  
                                                                               
                               'success,  so  return  True  
                                 EnableShutdownPrivledges  =  True  
           
                           End  If    'AdjustTokenPrivileges  
                     End  If    'LookupPrivilegeValue  
               End  If    'OpenProcessToken  
         End  If    'hProcessHandle  
     
    End  Function