Productivity Tools : HotKey Program
HotKey.cpp
#include "MyUtil.h"
#include "Windows.h"
#include "Shlobj.h"
#include <stdlib.h>
#include <locale.h>
#include <vector>
#include <string>
using namespace std;
struct RegInfo{
char key;
char path[256];
int id;
bool admin;
};
#define ID_KEY_E 100
#define ID_KEY_R 101
#define ID_KEY_C 102
#define ID_KEY_T 103
#define ID_KEY_V 104
#define ID_KEY_L 105
#define ID_KEY_G 106
#define ID_KEY_S 107
#define ID_KEY_I 108
#define ID_KEY_O 109
#define ID_KEY_P 110
#define ID_KEY_D 111
#define ID_KEY_Z 112
#define ID_KEY_Y 113
RegInfo info1 = {'E', "c:\\windows\\explorer.exe", ID_KEY_E, false};
RegInfo info2 = {'R', "c:\\windows\\regedit.exe", ID_KEY_R, true};
RegInfo info3 = {'C', "c:\\windows\\system32\\cmd.exe", ID_KEY_C, true};
RegInfo info4 = {'T', "c:\\windows\\system32\\taskmgr.exe", ID_KEY_T, true};
RegInfo info5 = {'V', "c:\\Program Files\\Microsoft VS Code\\code.exe", ID_KEY_V, false};
RegInfo info6 = {'L', "W:\\work\\program\\eclipse-php-neon-2-win32\\eclipse\\eclipse.exe", ID_KEY_L, false};
RegInfo info7 = {'G', "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", ID_KEY_G, false};
RegInfo info8 = {'S', "W:\\work\\program\\stirling\\Stirling.exe", ID_KEY_S, false};
RegInfo info9 = {'I', "C:\\Program Files\\Internet Explorer\\iexplore.exe", ID_KEY_I, false};
RegInfo info10= {'O', "C:\\Program Files\\Microsoft Office\\Office16\\OUTLOOK.EXE", ID_KEY_O, false};
RegInfo info11= {'P', "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", ID_KEY_P, true};
RegInfo info12= {'D', "W:\\work\\program\\odbg108b\\OLLYDBG.EXE", ID_KEY_D, true};
RegInfo info13= {'Z', "W:\\work\\program\\pz117\\PikaZip.exe", ID_KEY_Z, false};
RegInfo RegList[] = {
info1,
info2,
info3,
info4,
info5,
info6,
info7,
info8,
info9,
info10,
info11,
info12,
info13,
info14
};
vector<HANDLE> threadHandle;
HANDLE GetAlternativeToken(){
HANDLE hToken = NULL;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
return NULL;
}
HWND hwnd = GetShellWindow();
DWORD dwPid = 0;
GetWindowThreadProcessId(hwnd, &dwPid);
HANDLE hProc = OpenProcess(MAXIMUM_ALLOWED, false, dwPid);
if(!hProc){
CloseHandle(hToken);
return NULL;
}
HANDLE hTokenNormal = NULL;
if(!OpenProcessToken(hProc, TOKEN_ALL_ACCESS, &hTokenNormal)){
CloseHandle(hToken);
CloseHandle(hProc);
return NULL;
}
HANDLE hTokenDuplicate = NULL;
DuplicateTokenEx(hTokenNormal, MAXIMUM_ALLOWED, NULL,SecurityDelegation, TokenPrimary, &hTokenDuplicate);
CloseHandle(hToken);
CloseHandle(hProc);
CloseHandle(hTokenNormal);
return hTokenDuplicate;
}
DWORD WINAPI ThreadProc(LPVOID lpArg) {
PROCESS_INFORMATION pi;
STARTUPINFOW si;
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
wchar_t lpExe_w[256] = {0};
setlocale( LC_ALL, "japanese" );
mbstowcs( lpExe_w, (LPCTSTR)((RegInfo*)lpArg)->path, 256);
if( IsUserAnAdmin()== true && ((RegInfo*)lpArg)->admin == false ){
//Create new process with non-administrative token
HANDLE hTokenDuplicate = GetAlternativeToken();
if(hTokenDuplicate == NULL){
return -1;
}
CreateProcessWithTokenW(hTokenDuplicate,0,lpExe_w,NULL,0,0,NULL,&si,&pi);
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess,INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(hTokenDuplicate);
} else if(IsUserAnAdmin()== false && ((RegInfo*)lpArg)->admin == true) {
//Create new process with administrative token
ShellExecute(NULL, TEXT("runas"), (LPCTSTR)((RegInfo*)lpArg)->path, NULL, NULL, SW_SHOWNORMAL);
} else {
//Create new process with user's token
CreateProcessW(NULL,lpExe_w,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,
NULL,NULL,&si,&pi);
CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess,INFINITE);
CloseHandle(pi.hProcess);
}
ExitThread(0);
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_CREATE:
for(int i = 0; i < (int)(sizeof(RegList)/sizeof(RegList[0])); i++){
RegisterHotKey(hwnd, RegList[i].id, MOD_ALT, RegList[i].key);
}
return 0;
case WM_HOTKEY:
for(int i = 0; i < (int)(sizeof(RegList)/sizeof(RegList[0])); i++){
if(RegList[i].id == (int)wParam){
DWORD threadId = 0;
HANDLE hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)ThreadProc,
(void*)&(RegList[i]), 0, &threadId);
if(hThread != NULL){
SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
threadHandle.push_back(hThread);
}
break;
}
}
break;
case WM_DESTROY:
for(int i = 0; i < (int)(sizeof(RegList)/sizeof(RegList[0])); i++){
UnregisterHotKey(hwnd, RegList[i].id);
}
for(vector<HANDLE>::iterator itr = threadHandle.begin();
itr != threadHandle.end(); itr++){
WaitForSingleObject(*itr, INFINITE);
CloseHandle(*itr);
}
PostQuitMessage(0);
return 0;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int showCmd)
{
MyWindow window(hInst, (LPSTR)"HotKey", WindowProc);
int err = window.create();
if( err != 0 ){
return err;
}
HWND hWnd = window.getWnd();
ShowWindow(hWnd, SW_HIDE);
MSG msg = {0};
while (1) {
//if (PeekMessage (&msg,NULL,0,0,PM_NOREMOVE)) {
if (!GetMessage (&msg,NULL,0,0)){
return msg.wParam ;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
//}
}
return 0;
}
Profile
I have technical job experience in enbedded software development and server side infrastructure/application engineering.
I'm interested in programming and computer security.
Objective
To write down my technical knowledge in the place where I can access from anywhere.
To share my program source code.
To train my writing skill.
New entries