piotrlegnica / stuff
Various stuff and utilities.
Clone this repository (size: 50.9 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/piotrlegnica/stuff/
| commit 14: | 5c0fd3e2d281 |
| parent 13: | 69b9bc502315 |
| branch: | default |
| tags: | tip |
Spelling and other fun useless stuff. Refactored writeColour a bit.
stuff /
icon.cpp
| r14:5c0fd3e2d281 | 128 loc | 3.1 KB | embed / history / annotate / raw / |
|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | // compile with exceptions
// link with user32 and shell32
#ifdef _MSC_VER
# pragma comment(lib, "user32.lib")
# pragma comment(lib, "shell32.lib")
#endif
#define _WIN32_WINNT 0x0501
#define OEMRESOURCE
#include <windows.h>
#include <boost/lexical_cast.hpp>
//
// forward decls
//
LRESULT CALLBACK windowProc(HWND, UINT, WPARAM, LPARAM);
void fail(LPTSTR);
void registerClass(HINSTANCE, HICON);
void createWindow(HINSTANCE);
BOOL CALLBACK findWindow(HWND, LPARAM);
HANDLE getTargetProcess(DWORD&);
HICON getTargetIcon(void);
int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int) {
// get the icon of given running process
HICON icon = getTargetIcon();
// create our own window with that icon
registerClass(instance, icon);
createWindow(instance);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
//
// our window
//
LRESULT CALLBACK windowProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) {
if (msg == WM_DESTROY) {
PostQuitMessage(0);
return 0;
}
return DefWindowProc(wnd, msg, wp, lp);
}
void registerClass(HINSTANCE instance, HICON icon) {
WNDCLASSEX wndclass;
ZeroMemory(&wndclass, sizeof(WNDCLASSEX));
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.lpfnWndProc = windowProc;
wndclass.hInstance = instance;
wndclass.hIcon = icon;
wndclass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
wndclass.hCursor = static_cast<HCURSOR>(LoadImage(
NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED
));
wndclass.lpszClassName = TEXT("icon_stealer");
ATOM wndclassAtom = RegisterClassEx(&wndclass);
if (!wndclassAtom) fail(TEXT("registerclass"));
}
void createWindow(HINSTANCE instance) {
HWND wnd = CreateWindowEx(
WS_EX_APPWINDOW, TEXT("icon_stealer"), TEXT("Icon Stealer"), WS_CAPTION | WS_VISIBLE | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 150, 40, NULL, NULL, instance, NULL
);
if (!wnd) fail(TEXT("createwindow"));
UpdateWindow(wnd);
}
//
// their window
//
HANDLE getTargetProcess(DWORD &pid) {
int argc;
LPWSTR *argv;
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (!argv || argc < 2) fail(TEXT("argv"));
pid = boost::lexical_cast<DWORD>(argv[1]);
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (!process) fail(TEXT("openprocess"));
return process;
}
namespace globals {
HWND result;
DWORD pid;
}
BOOL CALLBACK findWindow(HWND wnd, LPARAM) {
DWORD foundPid;
GetWindowThreadProcessId(wnd, &foundPid);
if (foundPid == globals::pid) {
globals::result = wnd;
return FALSE;
}
return TRUE;
}
HICON getTargetIcon(void) {
HANDLE process = getTargetProcess(globals::pid);
EnumWindows(findWindow, 0);
return reinterpret_cast<HICON>(GetClassLong(globals::result, GCL_HICON));
}
//
// util
//
void fail(LPTSTR msg) {
MessageBox(NULL, msg, TEXT("Fail."), MB_OK | MB_ICONERROR);
ExitProcess(1);
}
|
