c语言程序重启自身?

c语言程序重启自身?,第1张

可以使用exec函数来实现: #include <stdio.h>#include <unistd.h>int main(int argc, char *argv[]) { int ifor (i = 0i <10i++) { printf("do something...\n")sleep(2)} execvp(argv[0], argv)return 0}

system("shutdown

-r")就是重启命令

#include<window.h>

int

main

(){

system("shutdown

-r")

return

0

}

1、system函数:

原型:int

system(const

char

*

command);

功能:执行

dos(windows系统)

shell(Linux/Unix系统)

命令,参数字符串command为命令名

说明:在windows系统中,system函数直接在控制台调用一个command命令。在Linux/Unix系统中,system函数会调用fork函数产生子进程,由子进程来执行command命令,命令执行完后随即返回原调用的进程;

头文件:stdlib.h;

返回值:命令执行成功返回0,执行失败返回-1。

2、例程:

#include<stdio.h>

#include<stdlib.h>

int main(){

system("del C:\\123.txt")//在控制台中,执行命令del C:\\123.txt,删除C盘目录下的123.txt文件

return 0

}

Call Shell ("shutdown -l -f")'注销

Call Shell ("shutdown -s -f")'关闭

Call Shell ("shutdown -r -f")'重启

或设定状态

SetSuspendState

是否支持休眠

IsPwrHibernateAllowed

是否支持待机

IsPwrSuspendAllowed看MSDN的Power Management部分ColdMooon 1.主要过程

'在窗体内

'放置一个时间控件,两个标签控件和一个文体框控件,和两个单选按钮,两个命令按钮Option Explicit

Dim txtTime '保存输入时间

Dim nowTime '保存实时时间

Dim oldTime '保存开始定时时间Private Sub Form_Load()me.caption="定时关机"me.label1.caption="关机时间:"

me.Option1.caption="在指定时间关机(时间格式:00:00:00)"

me.Option2.caption="延迟指定的时间关机"

me.command1.caption="确定"

me.command2.caption="取消"

Me.Text1 = GetSetting("ShutDownWindows", "Options", "ShutDownTime", "1:00")

me!timer1.interval=1000End Sub

Private Sub Command1_Click()

oldTime = Time

If Not IsDate(Text1.Text) Then '用IsData函数判断输入的时间格式

MsgBox "你所输入的不是时间格式,请修改!", , "Wrong"

ElseIf Option1.Value Then

txtTime = TimeValue(Text1.Text)

Timer1.Enabled = True '启动定时器

Me.WindowState = 1 '最小化窗体

Me.Caption = "定时关机-启动"

ElseIf Option2.Value Then

Me.WindowState = 1

Me.Caption = "定时关机-暂停"

End If

End SubPrivate Sub Command2_Click()Unload MeEnd Sub

Private Sub Form_Unload(Cancel As Integer)SaveSetting "ShutDownWindows", "Options", "ShutDownTime", Me.Text1End SubPrivate Sub Timer1_Timer()

nowTime = Time

Me.label2.Caption = Time

If Option1.Value Then

If DateDiff("s", nowTime, txtTime) <= 0 And DateDiff("s", nowTime, txtTime) >= -60 Then '用DateDiff函数判断是否到时间了

ShutDownPC True

Timer1.Enabled = False

End If

End If

End Sub '在标准模块内

Private Const EWX_LOGOFF = 0

Public Const EWX_SHUTDOWN = 1

Public Const EWX_POWEROFF = 8 '此参数在VB自带API浏览器并未提供,值得注意,如果没有此参数在Win2kServer会死在关机屏幕下

Private Const EWX_REBOOT = 2

Private Const EWX_FORCE = 4

Private Const TOKEN_ADJUST_PRIVILEGES = &H20

Private Const TOKEN_QUERY = &H8

Private Const SE_PRIVILEGE_ENABLED = &H2

Private Const ANYSIZE_ARRAY = 1

Private Const VER_PLATFORM_WIN32_NT = 2Type OSVERSIONINFO

dwOSVersionInfoSize As Long

dwMajorVersion As Long

dwMinorVersion As Long

dwBuildNumber As Long

dwPlatformId As Long

szCSDVersion As String * 128

End TypeType LUID

LowPart As Long

HighPart As Long

End TypeType LUID_AND_ATTRIBUTES

pLuid As LUID

Attributes As Long

End TypeType TOKEN_PRIVILEGES

PrivilegeCount As Long

Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES

End TypePrivate 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 TOKEN_PRIVILEGES, ReturnLength As Long) As Long

Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long

'Detect if the program is running under Windows NT 检测是否为NT

Public Function IsWinNT() As Boolean

Dim myOS As OSVERSIONINFO

myOS.dwOSVersionInfoSize = Len(myOS)

GetVersionEx myOS

IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)

End Function

'set the shut down privilege for the current application 为当前应用程序设置关机权限

Private Sub EnableShutDown()

Dim hProc As Long

Dim hToken As Long

Dim mLUID As LUID

Dim mPriv As TOKEN_PRIVILEGES

Dim mNewPriv As TOKEN_PRIVILEGES

hProc = GetCurrentProcess()

OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken

LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID

mPriv.PrivilegeCount = 1

mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED

mPriv.Privileges(0).pLuid = mLUID

' enable shutdown privilege for the current application 赋予当前应用程序关机权限

AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)

End Sub

' Shut Down PC 关闭PC

Public Sub ShutDownPC(Force As Boolean)

Dim ret As Long

Dim Flags As Long

Flags = EWX_SHUTDOWN Or EWX_POWEROFF

If Force Then Flags = Flags + EWX_FORCE

If IsWinNT Then EnableShutDown

ExitWindowsEx Flags, 0

End Sub

'Restart PC 重起PC

Public Sub RebootPC(Force As Boolean)

Dim ret As Long

Dim Flags As Long

Flags = EWX_REBOOT

If Force Then Flags = Flags + EWX_FORCE

If IsWinNT Then EnableShutDown

ExitWindowsEx Flags, 0

End Sub

'Log off the current user 注消(登出)当前用户

Public Sub LogOff(Force As Boolean)

Dim ret As Long

Dim Flags As Long

Flags = EWX_LOGOFF

If Force Then Flags = Flags + EWX_FORCE

ExitWindowsEx Flags, 0

End Sub

'自动开机说明:(以CMOS Setup Utility - Copyright (C) 1984-2001 Award Software 为例)

'开机按Delete键进入CMOS Setup程序

'把选择移到Power Management Setup(电源管理设置), 按回车

'然后把选择移到Resume by Alarm,按PageDown键选择Enable

'移到下一行把Date(of Month) Alarm 设为0

'移到下一行把Time(hh:mm:ss) Alarm 设为[自己的开机时间


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8065540.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存