Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Created September 15, 2012 01:36
Show Gist options
  • Save heisvoid/3725985 to your computer and use it in GitHub Desktop.
Save heisvoid/3725985 to your computer and use it in GitHub Desktop.
Maplestory AD Killer
/*
* Maplestory AD Killer
*
* Copyright (C) 2012 heisvoid
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <tchar.h>
#include <tlhelp32.h>
#include <shlwapi.h>
#include <shellapi.h>
#include "resource.h"
enum
{
NOTIFY_ICON_ID = 1,
WM_NOTIFY_ICON = WM_APP
};
static const TCHAR * const APP_NAME = _T ("Maplestory AD Killer");
static DWORD
get_notifyicondata_size ()
{
DWORD size = 0;
HMODULE dll = LoadLibrary (_T ("shell32.dll"));
if (NULL != dll)
{
DLLGETVERSIONPROC dll_get_version = (DLLGETVERSIONPROC) GetProcAddress (
dll, "DllGetVersion");
if (NULL != dll_get_version)
{
DLLVERSIONINFO version;
ZeroMemory (&version, sizeof (DLLVERSIONINFO));
version.cbSize = sizeof (DLLVERSIONINFO);
if (S_OK == ((*dll_get_version) (&version)))
{
if (5 > version.dwMajorVersion)
{
size = NOTIFYICONDATA_V1_SIZE;
}
else if (5 == version.dwMajorVersion)
{
size = NOTIFYICONDATA_V2_SIZE;
}
else if (6 == version.dwMajorVersion
&& 0 == version.dwMinorVersion
&& 6 > version.dwBuildNumber)
{
size = NOTIFYICONDATA_V3_SIZE;
}
else
{
size = sizeof (NOTIFYICONDATA);
}
}
}
FreeLibrary (dll);
}
return size;
}
static void
notify_icon_add (HWND win)
{
NOTIFYICONDATA notify_icon;
ZeroMemory (&notify_icon, sizeof (NOTIFYICONDATA));
notify_icon.cbSize = get_notifyicondata_size ();
if (0 < notify_icon.cbSize)
{
notify_icon.uCallbackMessage = WM_NOTIFY_ICON;
notify_icon.hWnd = win;
notify_icon.uID = NOTIFY_ICON_ID;
notify_icon.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
notify_icon.hIcon = LoadIcon (GetModuleHandle (NULL),
MAKEINTRESOURCE (IDI_MAPLESTORYADKILLER));
_tcscpy_s (notify_icon.szTip, APP_NAME);
Shell_NotifyIcon (NIM_ADD, &notify_icon);
}
}
static void
notify_icon_del (HWND win)
{
NOTIFYICONDATA notify_icon;
ZeroMemory (&notify_icon, sizeof (NOTIFYICONDATA));
notify_icon.cbSize = get_notifyicondata_size ();
if (0 < notify_icon.cbSize)
{
notify_icon.hWnd = win;
notify_icon.uID = NOTIFY_ICON_ID;
Shell_NotifyIcon (NIM_DELETE, &notify_icon);
}
}
static DWORD
get_pid (const TCHAR *exe)
{
DWORD pid = 0;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE != snapshot)
{
PROCESSENTRY32 proc_info;
proc_info.dwSize = sizeof (PROCESSENTRY32);
if (TRUE == Process32First (snapshot, &proc_info))
{
do
{
if (0 == _tcscmp (exe, proc_info.szExeFile))
{
pid = proc_info.th32ProcessID;
break;
}
}
while (TRUE == Process32Next (snapshot, &proc_info));
}
CloseHandle(snapshot);
}
return pid;
}
static BOOL CALLBACK
enumerate_windows (HWND win, LPARAM lparam)
{
DWORD pid = 0;
GetWindowThreadProcessId (win, &pid);
if (*((DWORD *) lparam) == pid)
{
enum
{
MAX_SIZE = 8
};
TCHAR title[MAX_SIZE];
if (0 < GetWindowText (win, title, MAX_SIZE))
{
if (0 == _tcscmp (_T ("ad."), title))
{
PostMessage (win, WM_CLOSE, 0, 0);
return FALSE;
}
}
}
return TRUE;
}
static LRESULT CALLBACK
window_procedure (HWND win, UINT msg, WPARAM wparam, LPARAM lparam)
{
enum
{
TIMER_ID = 1
};
static UINT taskbar_created_msg = 0;
LRESULT result = 0;
switch (msg)
{
case WM_CREATE:
SetTimer (win, TIMER_ID, 100, NULL);
notify_icon_add (win);
taskbar_created_msg = RegisterWindowMessage (_T ("TaskbarCreated"));
break;
case WM_DESTROY:
KillTimer (win, TIMER_ID);
notify_icon_del (win);
PostQuitMessage (0);
break;
case WM_TIMER:
{
static const TCHAR *exe_names[] = {_T ("MapleStory.exe"),
_T ("MapleStoryT.exe"),
NULL};
size_t i = 0;
for (i = 0; NULL != exe_names[i]; i++)
{
DWORD pid = get_pid (exe_names[i]);
if (0 != pid)
{
EnumWindows (&enumerate_windows, (LPARAM) &pid);
}
}
}
break;
case WM_NOTIFY_ICON:
if (WM_RBUTTONDOWN == lparam)
{
enum
{
NOTIFY_ICON_MENU_EXIT = 1
};
static HMENU notify_icon_menu = NULL;
if (NULL == notify_icon_menu)
{
notify_icon_menu = CreatePopupMenu ();
if (NULL != notify_icon_menu)
{
AppendMenu (notify_icon_menu, MF_STRING,
NOTIFY_ICON_MENU_EXIT, _T ("E&xit"));
}
}
if (NULL != notify_icon_menu)
{
POINT pos;
ZeroMemory (&pos, sizeof (POINT));
if (FALSE != GetCursorPos (&pos))
{
SetForegroundWindow (win);
BOOL selected = TrackPopupMenu (notify_icon_menu,
TPM_NONOTIFY | TPM_RETURNCMD, pos.x, pos.y, 0, win,
NULL);
if (NOTIFY_ICON_MENU_EXIT == selected)
{
PostMessage (win, WM_CLOSE, 0, 0);
}
}
}
}
break;
default:
if (msg == taskbar_created_msg)
{
notify_icon_add (win);
}
result = DefWindowProc (win, msg, wparam, lparam);
break;
}
return result;
}
int WINAPI
WinMain (HINSTANCE inst, HINSTANCE prev_inst, LPSTR cmd_line, int cmd_show)
{
HANDLE mutex = CreateMutex (NULL, FALSE,
_T ("Global\\b5e2660f-c1f4-494a-bf5b-abfd4b8d02d8"));
if (ERROR_ALREADY_EXISTS == GetLastError ())
{
return 0;
}
WNDCLASS cls;
cls.style = CS_HREDRAW | CS_VREDRAW;
cls.lpfnWndProc = window_procedure;
cls.cbClsExtra = 0;
cls.cbWndExtra = 0;
cls.hInstance = inst;
cls.hIcon = LoadIcon (inst, MAKEINTRESOURCE(IDI_MAPLESTORYADKILLER));
cls.hCursor = NULL;
cls.hbrBackground = GetSysColorBrush (COLOR_3DFACE);
cls.lpszMenuName = NULL;
cls.lpszClassName = APP_NAME;
RegisterClass (&cls);
HWND win = CreateWindow (cls.lpszClassName, APP_NAME,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
50, 50, NULL, NULL, inst, NULL);
if (NULL != win)
{
ShowWindow (win, SW_HIDE);
if (TRUE == UpdateWindow (win))
{
while (TRUE)
{
MSG msg;
BOOL ret = GetMessage (&msg, NULL, 0, 0);
if (0 == ret || -1 == ret)
{
break;
}
DispatchMessage (&msg);
}
}
}
CloseHandle (mutex);
return 0;
}
/*
* Maplestory AD Killer
*
* Copyright (C) 2012 heisvoid
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "resource.h"
IDI_MAPLESTORYADKILLER ICON "maplestoryadkiller.ico"
/*
* Maplestory AD Killer
*
* Copyright (C) 2012 heisvoid
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RESOURCE_H
#define RESOURCE_H
#define IDI_MAPLESTORYADKILLER 101
#endif /* not RESOURCE_H */
@heisvoid
Copy link
Author

Download
https://docs.google.com/open?id=0B4LlbHJyEH47R2JuNUxyWWxTV3c

SHA1SUM
32bf5307cfd5e2b8fabdba6466e98269d1ee78f4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment