码迷,mamicode.com
首页 > 其他好文 > 详细

VC驿站黑客编程(关机,重启,注销)

时间:2015-06-03 13:41:45      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

此程序在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("关机"));
						  //选择按钮
						  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驿站黑客编程(关机,重启,注销)

标签:

原文地址:http://blog.csdn.net/jackhall/article/details/46343961

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!