* registered HandlerRoutine.
*/
-#define DBG 1
+/* #define DBG 1
+ */
#include <windows.h>
static HMODULE hmodHook = NULL;
static HHOOK hhkGetMessage;
-//static HHOOK hhkCallWndProc;
+static HHOOK hhkCallWndProc;
static LPCTSTR origwndprop = NULL;
static LPCTSTR hookwndprop = NULL;
+static HWND monitor_hwnd = NULL;
+
#ifdef DBG
static VOID DbgPrintf(LPTSTR fmt, ...);
#endif
static BOOL CALLBACK EnumttyWindow(HWND wnd, LPARAM retwnd);
+LRESULT __declspec(dllexport) WINAPI RegisterWindows9xService(BOOL is_service)
+{
+ static BOOL is_registered = FALSE;
+ HINSTANCE hkernel;
+ DWORD (WINAPI *register_service_process)(DWORD, DWORD);
+ BOOL rv;
+
+ if (is_service == is_registered)
+ return 1;
+
+ /* Obtain a handle to the kernel library */
+ hkernel = LoadLibrary("KERNEL32.DLL");
+ if (!hkernel)
+ return 0;
+
+ /* Find the RegisterServiceProcess function */
+ register_service_process = (DWORD (WINAPI *)(DWORD, DWORD))
+ GetProcAddress(hkernel, "RegisterServiceProcess");
+ if (register_service_process == NULL) {
+ FreeLibrary(hkernel);
+ return 0;
+ }
+
+ /* Register this process as a service */
+ rv = register_service_process(0, is_service != FALSE);
+
+ /* Unload the kernel library */
+ FreeLibrary(hkernel);
+ return rv;
+}
BOOL __declspec(dllexport) APIENTRY DllMain(PVOID hModule, ULONG ulReason, PCONTEXT pctx)
{
SetWindowLong(hwtty, GWL_WNDPROC, (LONG)origproc);
RemoveProp(hwtty, origwndprop);
}
+ RegisterWindows9xService(FALSE);
}
EnumWindows(EnumttyWindow, (LPARAM)&parent);
if (parent) {
UnhookWindowsHookEx(hhkGetMessage);
hhkGetMessage = NULL;
}
+ if (hhkCallWndProc) {
+ UnhookWindowsHookEx(hhkCallWndProc);
+ hhkCallWndProc = NULL;
+ }
FreeLibrary(hmodHook);
hmodHook = NULL;
}
PHANDLER_ROUTINE phandler;
HINSTANCE instance;
HWND parent;
+ char *name;
} tty_info;
#ifdef DBG
DbgPrintf("S Created ttyConHookChild:%8.8x\r\n", hwnd);
#endif
- SetProp(((tty_info*)tty)->parent, hookwndprop, hwnd);
+ if (((tty_info*)tty)->parent)
+ SetProp(((tty_info*)tty)->parent, hookwndprop, hwnd);
return 0;
}
else if (msg == WM_DESTROY)
{
HWND parent = (HWND)GetWindowLong(hwnd, gwltty_ttywnd);
- RemoveProp(parent, hookwndprop);
+ if (parent)
+ RemoveProp(parent, hookwndprop);
}
else if (msg == WM_CLOSE)
{
* and the logoff message is ignored.
*/
WNDCLASS wc;
- HWND hwnd;
MSG msg;
wc.style = CS_GLOBALCLASS;
wc.lpfnWndProc = ttyConsoleCtrlWndProc;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
- wc.lpszClassName = "ttyConHookChild";
-
+ if (((tty_info*)tty)->parent)
+ wc.lpszClassName = "ttyConHookChild";
+ else
+ wc.lpszClassName = "ApacheWin95ServiceMonitor";
+
if (!RegisterClass(&wc)) {
#ifdef DBG
DbgPrintf("S Created ttyConHookChild class\r\n");
}
/* Create an invisible window */
- hwnd = CreateWindow(wc.lpszClassName, "",
- WS_OVERLAPPED & ~WS_VISIBLE,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL,
- ((tty_info*)tty)->instance, tty);
-
- if (!hwnd) {
+ monitor_hwnd = CreateWindow(wc.lpszClassName, ((tty_info*)tty)->name,
+ WS_OVERLAPPED & ~WS_VISIBLE,
+ CW_USEDEFAULT, CW_USEDEFAULT,
+ CW_USEDEFAULT, CW_USEDEFAULT,
+ NULL, NULL,
+ ((tty_info*)tty)->instance, tty);
+
+ if (!monitor_hwnd) {
#ifdef DBG
DbgPrintf("S Error Creating ttyConHookChild:%d\r\n", GetLastError());
#endif
{
TranslateMessage(&msg);
DispatchMessage(&msg);
- if (msg.message == WM_DESTROY)
- DestroyWindow(hwnd);
}
return 0;
}
}
+/*
+ * Exported function that sets up the fixup child window
+ */
+BOOL __declspec(dllexport) WINAPI Windows9xServiceCtrlHandler(
+ PHANDLER_ROUTINE phandler,
+ BOOL add)
+{
+ /* If we have not yet done so */
+ FreeConsole();
+
+ if (add)
+ {
+ DWORD tid;
+ HANDLE hThread;
+ /* NOTE: this is static so the module can continue to
+ * access these args while we go on to other things
+ */
+ static tty_info tty;
+ tty.instance = GetModuleHandle(NULL);
+ tty.phandler = phandler;
+ tty.parent = NULL;
+ tty.name = "Apache Service";
+ RegisterWindows9xService(TRUE);
+ hThread = CreateThread(NULL, 0, ttyConsoleCtrlThread,
+ (LPVOID)&tty, 0, &tid);
+ if (hThread)
+ {
+ CloseHandle(hThread);
+ return TRUE;
+ }
+ }
+ else /* remove */
+ {
+ HWND child = FindWindowEx(NULL, NULL, "ttyConHookChild", NULL);
+ if (monitor_hwnd)
+ SendMessage(monitor_hwnd, WM_DESTROY, 0, 0);
+ RegisterWindows9xService(FALSE);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+
/*
* Exported function that sets up the fixup child window and dispatch
*/
HANDLE hThread;
DWORD tid;
static tty_info tty;
+ tty.instance = GetModuleHandle(NULL);
tty.phandler = phandler;
tty.parent = parent;
- tty.instance = GetModuleHandle(NULL);
-
+ tty.name = "ttyListener";
hmodHook = LoadLibrary("Win9xConHook.dll");
if (hmodHook)
{
hhkGetMessage = SetWindowsHookEx(WH_GETMESSAGE,
(HOOKPROC)GetProcAddress(hmodHook, "GetMsgProc"), hmodHook, 0);
- //hhkCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC,
- // (HOOKPROC)GetProcAddress(hmodHook, "CallWndProc"), hmodHook, 0);
+ hhkCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC,
+ (HOOKPROC)GetProcAddress(hmodHook, "CallWndProc"), hmodHook, 0);
}
hThread = CreateThread(NULL, 0, ttyConsoleCtrlThread,
UnhookWindowsHookEx(hhkGetMessage);
hhkGetMessage = NULL;
}
+ if (hhkCallWndProc) {
+ UnhookWindowsHookEx(hhkCallWndProc);
+ hhkCallWndProc = NULL;
+ }
FreeLibrary(hmodHook);
hmodHook = NULL;
}
GetCurrentProcessId(), hwnd);
#endif
is_subclassed = FALSE;
+ RegisterWindows9xService(FALSE);
SetWindowLong(hwnd, GWL_WNDPROC, (LONG)origproc);
RemoveProp(hwnd, origwndprop);
break;
DbgPrintf("W Proc %08x hwnd:%08x Subclassed\r\n",
GetCurrentProcessId(), hwtty);
#endif
+ RegisterWindows9xService(TRUE);
}
#ifdef DBG
DbgPrintf("H Proc %08x %s %08x\r\n", GetCurrentProcessId(),