Code:
// Admin2.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "Admin2.h"
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <string>
#define MAX_LOADSTRING 100
#define TOGGABLE_KEYS 10002
#define IDT_TIMER WM_USER + 200
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK myProc(HWND, UINT, WPARAM, LPARAM);
HWND hWnd = CreateWindow(WC_DIALOG,(LPCWSTR)L"", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 400,100,100,80,NULL,NULL,NULL,NULL);
HWND static_label = CreateWindow((LPCWSTR)L"Static", (LPCWSTR)L"", WS_CHILD | WS_VISIBLE, 0, 0, 200, 25, hWnd,(HMENU)-1, NULL, NULL );
HWND myButton = CreateWindow((LPCWSTR)L"BUTTON", (LPCWSTR)L"OK", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 0, 0 , 105, 50, hWnd, (HMENU)ID_BUTTON1, NULL, NULL );
void CloseExplorer()
{
char *cmd = "C:\\Windows\\System32\\taskkill.exe -f -im explorer.exe";
system(cmd);
}
void RunAsAdmin()
{
char *runa = "runas /user:%computername%\\admin C:\\Windows\\explorer.exe";
system(runa);
}
void ExitAdmin()
{
HWND con = FindWindowA("ConsoleWindowClass",NULL);
char *cmd2 = "C:\\Windows\\System32\\taskkill.exe -f -im explorer.exe";
char *cmd3 = "C:\\Windows\\explorer.exe";
system(cmd2);
system(cmd3);
ShowWindow(con,SW_HIDE);
FreeConsole();
}
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
FreeConsole();
// UNREFERENCED_PARAMETER(hPrevInstance);
// UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
// LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_ADMIN2, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ADMIN2));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ADMIN2));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_ADMIN2);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
SetWindowLong(hWnd, DWL_DLGPROC, (long) myProc);
if (!hWnd)
{
return FALSE;
}
//Close the user's explorer window
CloseExplorer();
//Run a new Explorer.exe shell under admin
RunAsAdmin();
ShowWindow(hWnd, SW_MINIMIZE);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId;
int wmEvent;
HWND con = FindWindowA("ConsoleWindowClass",NULL);
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_INITDIALOG:
{
//** Set timers
SetTimer(hwnd, TOGGABLE_KEYS, 1, NULL);
}
break;
case WM_COMMAND:
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_BUTTON1:
{
ExitAdmin();
ShowWindow(con,SW_HIDE);
FreeConsole();
DestroyWindow(hwnd);
}
break;
}
}
break;
case WM_TIMER:
{
}
break;
//default:
// return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_INITDIALOG:
{
}
break;
case WM_COMMAND:
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
case WM_TIMER:
{
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Should work with Win7, but for some reason some pc's are glitching (works fine for my Win7 x64 machine though heh).