VC驿站黑客编程(关机,重新启动,注销)
Posted jhcelue
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC驿站黑客编程(关机,重新启动,注销)相关的知识,希望对你有一定的参考价值。
此程序在VS2013下编译通过,假设换到编译器,大家能够稍作改动使用
#include<Windows.h> #include<tchar.h> #include"resource1.h" #pragma warning(disable:4996) BOOL EnableShutDownPriv() { HANDLE hToken = NULL; TOKEN_PRIVILEGES tkp = {0}; //打开当前程序的权限令牌 if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) { return FALSE; } if (!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid)) { CloseHandle(hToken); return FALSE; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken,FALSE,&tkp,sizeof(TOKEN_PRIVILEGES),NULL,NULL)) { CloseHandle(hToken); return FALSE; } return true; } //winMain是一个win32程序的入口点, //程序入口点: 1.console程序的入口点函数 main // 2.win32程序的入口点函数 WinMain // 3.dll32程序的入口点函数 DllMain // 4.服务程序的入口点函数 ServiceMain // 5.驱动程序入口点函数 Driver //參数1,HINSTANCE hInstance 应用程序实例句柄 //參数2。HINSTANCE hPrevInstance前一个程序实例 //參数3。LPSTR lpCmdLine 是一个用户运行应用程序时传入的參数 //參数4,int nShowCmd假设有窗体以什么形态显示窗体,最大化,最小化 //reset window status 又一次设置window状态 /* C4996以前不是警告么?如今升级啦? 尝试这个 1.Project Properties > Configuration Properties > C/C++ > General > SDL checks关掉 其它方法: 2.#pragma warning(disable: 4996) 3。/wd 4996 */ BOOL ReSetWindow(DWORD dwFlag, BOOL bForce) { //推断又一次设置的标志 switch (dwFlag) { case EWX_LOGOFF: case EWX_REBOOT: case EWX_SHUTDOWN: case EWX_POWEROFF: break; default: return FALSE; } //获取电脑系统版本号 OSVERSIONINFO osvi = { 0 }; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (!GetVersionEx(&osvi)) { return FALSE; } DWORD highVersion; DWORD lowVersion; //获取主版本号号 highVersion = osvi.dwMajorVersion; //获取次版本号号 lowVersion = osvi.dwMinorVersion; DWORD plat = osvi.dwPlatformId; if (plat == VER_PLATFORM_WIN32_NT) { EnableShutDownPriv(); } //在window中通常是占领不同的位数表示不同的值,一般通过|来获取两个或两个以上的效果 dwFlag = bForce ?
dwFlag | EWX_FORCE : dwFlag|EWX_FORCEIFHUNG; ExitWindowsEx(dwFlag, 0); } INT_PTR CALLBACK DialogProc( _In_ HWND hwndDlg, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam ) { TCHAR tStr[20] = { 0 }; switch (uMsg) { case WM_INITDIALOG: { //获取窗体句柄 HWND hComBox = GetDlgItem(hwndDlg, IDC_COMBO1); //往combobox中插入值 SendMessage(hComBox, CB_INSERTSTRING, 0, (LPARAM)_T("注销")); SendMessage(hComBox, CB_INSERTSTRING, 1, (LPARAM)_T("重新启动")); SendMessage(hComBox, CB_INSERTSTRING, 2, (LPARAM)_T("关机")); //选择button SendMessage(hComBox, CB_SELECTSTRING, 0, (LPARAM)_T("注销")); //获取屏幕大小 int witdh = GetSystemMetrics(SM_CXSCREEN); int height = GetSystemMetrics(SM_CYSCREEN); RECT rect; GetWindowRect(hwndDlg, &rect); //获取对话框的大小 int dlgW = rect.right - rect.left; int dlgH = rect.top = rect.bottom; //设置窗体位置 SetWindowPos(hwndDlg, 0, witdh / 2 - dlgW / 2, height / 2 - dlgH / 2, dlgW, dlgH, SWP_SHOWWINDOW); } break; case WM_COMMAND: { switch (wParam) { case IDOK: GetDlgItemText(hwndDlg, IDC_COMBO1, tStr, 20); if (wcscmp(tStr, _T("注销"))) { ReSetWindow(EWX_LOGOFF, FALSE); } else if (wcscmp(tStr, _T("重新启动"))) { ReSetWindow(EWX_REBOOT, FALSE); } else if (wcscmp(tStr, _T("关机"))) { ReSetWindow(EWX_SHUTDOWN, FALSE); } break; case IDCANCEL: if (MessageBox(NULL,_T("是否关闭"),_T("提示"),MB_OKCANCEL)==IDOK) { EndDialog(hwndDlg, IDCANCEL); } break; default: break; } } break; default: break; } return 0; } int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { if (strcmp(lpCmdLine, "/r") == 0) { ReSetWindow(EWX_REBOOT, FALSE); } else if (strcmp(lpCmdLine,"/f")==0) { ReSetWindow(EWX_LOGOFF, FALSE); } else if (strcmp(lpCmdLine,"/s")==0) { ReSetWindow(EWX_SHUTDOWN, FALSE); } else { DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc); } return 0; }
以上是关于VC驿站黑客编程(关机,重新启动,注销)的主要内容,如果未能解决你的问题,请参考以下文章