求指点,我新手!

a63461612 2011-09-28 09:53:57
我新手,刚学,代码编译有点简单,大家别笑我想弄个定时关机,下面是代码,可是当时间到的时候没反映,怎么回事
#include "stdafx.h"
#include "time.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"


BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
}

return FALSE;
}
void CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
TCHAR str1[256];
wsprintf(str1,TEXT("%u"),stLocal.wHour);
SetDlgItemText(hwnd,IDC_EDIT1,str1);
TCHAR str2[256];
wsprintf(str2,TEXT("%u"),stLocal.wMinute);
SetDlgItemText(hwnd,IDC_EDIT2,str2);
TCHAR str3[256];
wsprintf(str3,TEXT("%u"),stLocal.wSecond);
SetDlgItemText(hwnd,IDC_EDIT3,str3);

}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
SetTimer(hwnd,0,1000,TimerProc);

return TRUE;
}

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDC_OK:
{
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
TCHAR str1[256];
wsprintf(str1,TEXT("%u"),stLocal.wHour);
TCHAR str2[256];
wsprintf(str2,TEXT("%u"),stLocal.wMinute);
TCHAR str3[256];
wsprintf(str3,TEXT("%u"),stLocal.wSecond);
TCHAR str6[256];
GetDlgItemText(hwnd,IDC_EDIT6,str6,sizeof(str1));
TCHAR str7[256];
GetDlgItemText(hwnd,IDC_EDIT6,str7,sizeof(str7));
TCHAR str8[256];
GetDlgItemText(hwnd,IDC_EDIT8,str8,sizeof(str8));
if(str6==str1,str7==str2,str3==str8)
ExitWindowsEx(EWX_SHUTDOWN,4);






}
break;
default:
break;
}
}

void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}
...全文
40 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯本 2011-09-28
  • 打赏
  • 举报
回复
VC的程序?
在windows2000以上关机需要提升权限的
(当然前提是要超级用户权限)

HANDLE hToken;
TOKEN_PRIVILEGES tkp,tkpNew;
LUID int64LpUid;

OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)
LookupPrivilegeValue(NULL,"SeShutdownPrivilege",&int64LpUid);
tkp.PrivilegeCount=1;
tkp.Privileges[0].Liud=int64LpUid;
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&tkp,sizeof(tkp),&tkpNew,&dwSize);

Windows NT/2000 关机函数
BOOL MySystemShutdown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return( FALSE );

// Get the LUID for the shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);

if (GetLastError() != ERROR_SUCCESS)
return FALSE;

// Shut down the system and force all applications to close.

if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return FALSE;

return TRUE;
}

13,826

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧