home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2001 August
/
PCWorld_2001-08_cd.bin
/
Komunikace
/
sambar
/
_setup.1
/
ntview.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-06-03
|
10KB
|
418 lines
/*
** WINCTRL
**
** This is the NT service controller the Sambar Server.
**
** Confidential Property of Tod Sambar
** (c) Copyright Tod Sambar 2001
** All rights reserved.
**
**
** Syntax:
**
** controller
*/
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <share.h>
#include <process.h>
#include <sambar.h>
#include <resource.h>
/*
** Local Defines
*/
#define SERVER_ID 1001
#define SERVER_MSG WM_USER + 69
#define NAME "Sambar Server"
static int LogCount = 0;
static int MainX = 20;
static int MainY = 80;
static HWND MainWindow = NULL;
static HWND MainList = NULL;
static HWND ClearButton = NULL;
static HINSTANCE MainInstance;
static BOOL ShellTray = FALSE;
static BOOL Showing = FALSE;
static BOOL CheckExisting = TRUE;
static char ConfigDir[256];
static NOTIFYICONDATA IconData;
static HANDLE hPipe = NULL;
/*
** Local Prototypes
*/
long __stdcall WndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
);
void WndShutdown(
void
);
void DisplayMenu(
HWND hWnd
);
BOOL DisplayLoglines(
HWND hWnd
);
void ShutdownServer(
HWND hWnd
);
void Logger(
void *argp
);
int __stdcall
WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CmdLine, int CmdShow)
{
unsigned long thread;
MSG message;
HWND hWnd;
WNDCLASS MainClass;
HICON ServerIcon;
/* Save the application Instance */
MainInstance = Instance;
/* If a Server is running, show it */
if (CheckExisting)
{
hWnd = FindWindow(NAME, NULL);
if (hWnd)
{
if (IsIconic(hWnd))
ShowWindow(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
return (0);
}
}
SetErrorMode(SEM_FAILCRITICALERRORS |
SEM_NOGPFAULTERRORBOX |
SEM_NOOPENFILEERRORBOX);
/* Create the tray Icon resource */
ServerIcon = LoadIcon(Instance, "SERVER_ICON");
/* Create a window class */
MainClass.style = CS_HREDRAW | CS_VREDRAW;
MainClass.lpfnWndProc = WndProc;
MainClass.cbClsExtra = 0;
MainClass.cbWndExtra = 0;
MainClass.hInstance = Instance;
MainClass.hIcon = ServerIcon;
MainClass.hCursor = LoadCursor(NULL, IDC_ARROW);
MainClass.hbrBackground = GetStockObject(LTGRAY_BRUSH);
MainClass.lpszMenuName = NULL;
MainClass.lpszClassName = NAME;
if (!RegisterClass(&MainClass))
return (0);
if (!WaitNamedPipe("\\\\.\\pipe\\SambarServer", NMPWAIT_USE_DEFAULT_WAIT))
{
MessageBox(NULL, "Failure connecting to Sambar Server NT Service.",
NAME, MB_OK | MB_ICONSTOP);
return 0;
}
hPipe = CreateFile("\\\\.\\pipe\\SambarServer", GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Failure connecting to Sambar Server NT Service.",
NAME, MB_OK | MB_ICONSTOP);
return 0;
}
/* Create the main window */
MainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, NAME, NAME,
(WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX),
CW_USEDEFAULT, 0, 600, 360, NULL, NULL, Instance, NULL);
if (!MainWindow)
{
CloseHandle(hPipe);
return (0);
}
/*
** Display the header.
*/
hWnd = CreateWindow("STATIC", "Header1", WS_CHILD | WS_VISIBLE,
20, 15, 300, 20, MainWindow, NULL, Instance, NULL);
SetWindowText(hWnd, "Sambar Server NT Monitor");
/*
** Put the buttons in.
*/
ClearButton = CreateWindow("BUTTON", "Clear", WS_CHILD | WS_VISIBLE,
MainX, MainY - 30, 50, 25, MainWindow, NULL, Instance, NULL);
SetWindowText(ClearButton, "Clear");
if (!ClearButton)
{
MessageBox(NULL, "Failure creating clear button.",
NAME, MB_OK | MB_ICONSTOP );
CloseHandle(hPipe);
return (0);
}
/* Create a list box for the log display */
MainList = CreateWindow("LISTBOX", NULL,
WS_CHILD|WS_BORDER|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL,
MainX, MainY, 550, 280, MainWindow, (HMENU)1000,
Instance, NULL);
if (!MainList)
{
MessageBox(NULL, "Failure creating list box.",
NAME, MB_OK | MB_ICONSTOP );
CloseHandle(hPipe);
return (0);
}
SendMessage(MainList, LB_SETHORIZONTALEXTENT, 2048, 0L);
/* Start the log thread. */
thread = _beginthread(Logger, 0, (void *)NULL);
if (thread == -1)
return (0);
/* Add tray icon to system tray */
IconData.cbSize = sizeof(NOTIFYICONDATA);
IconData.hWnd = MainWindow;
IconData.uID = SERVER_ID;
IconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
IconData.uCallbackMessage = SERVER_MSG;
IconData.hIcon = ServerIcon;
strcpy(IconData.szTip, "Sambar Server Controller is Active");
/* Win95 and NT4.0 only */
if (!Shell_NotifyIcon(NIM_ADD, &IconData))
{
ShowWindow(MainWindow, CmdShow);
UpdateWindow(MainWindow);
}
else
{
ShellTray = TRUE;
ShowWindow(MainWindow, Showing ? SW_SHOW : SW_HIDE);
}
atexit(WndShutdown);
/* Message loop */
while (GetMessage(&message, NULL, 0, 0))
DispatchMessage(&message);
Shell_NotifyIcon(NIM_DELETE, &IconData);
UnregisterClass(NAME, Instance);
if (hPipe != NULL)
CloseHandle(hPipe);
return (message.wParam);
}
void WndShutdown()
{
Shell_NotifyIcon(NIM_DELETE, &IconData);
}
long __stdcall
WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int hi;
int lo;
RECT rect;
switch (message)
{
case SERVER_MSG:
if ((wParam == SERVER_ID) && (lParam == WM_RBUTTONDOWN))
{
DisplayMenu(hWnd);
}
else if ((wParam == SERVER_ID) && (lParam == WM_LBUTTONDBLCLK))
{
Showing = TRUE;
ShowWindow(hWnd, SW_SHOW);
}
break;
case WM_CLOSE:
if (ShellTray)
{
ShowWindow(MainWindow, SW_HIDE);
Showing = FALSE;
}
else
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_COMMAND:
if ((HWND)lParam == ClearButton)
{
/* Clear the list box */
LogCount = 1;
SendMessage(MainList, LB_RESETCONTENT, 0, 0);
}
else
{
/* Unhandled message */
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_SIZE:
hi = HIWORD(lParam);
lo = LOWORD(lParam);
GetClientRect(MainList, &rect);
if ((LOWORD(lParam) - 40 == rect.right) &&
(HIWORD(lParam) - 90 == rect.bottom))
{
return 0;
}
/* Adjust the log box's window */
MoveWindow(MainList, MainX, MainY, LOWORD(lParam) - 40,
HIWORD(lParam) - 90, TRUE);
break;
case WM_DESTROY:
/* Shutdown the main window */
PostQuitMessage(0);
break;
default:
/* Unhandled Messages end up here (DefWindowProc) */
return DefWindowProc(hWnd, message, wParam, lParam);
}
return(0);
}
void
DisplayMenu(HWND hWnd)
{
HMENU MenuHnd;
POINT MousePos;
int ScreenWidth;
int ScreenHeight;
int SelItem;
MenuHnd = CreatePopupMenu();
AppendMenu(MenuHnd, MF_ENABLED, 1, "Open");
AppendMenu(MenuHnd, MF_SEPARATOR, 0, NULL);
AppendMenu(MenuHnd, MF_ENABLED, 2, "Shutdown");
//Get Mouse Pos
GetCursorPos(&MousePos);
//Get Screen Metrics
ScreenWidth = GetSystemMetrics(SM_CXSCREEN);
ScreenHeight = GetSystemMetrics(SM_CYSCREEN);
SetForegroundWindow(MainWindow);
//Handle the different possible task bar locations
if ((MousePos.x >= (ScreenWidth / 2)) && (MousePos.y >= (ScreenHeight / 2)))
{
//Bottom or Right
SelItem = TrackPopupMenu(MenuHnd,
TPM_BOTTOMALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
MousePos.x, ScreenHeight, 0, MainWindow, NULL);
}
else if (MousePos.y < (ScreenHeight / 2))
{
//Top
SelItem = TrackPopupMenu(MenuHnd,
TPM_TOPALIGN | TPM_RIGHTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
MousePos.x, MousePos.y, 0, MainWindow, NULL);
}
else
{
//Left
SelItem = TrackPopupMenu(MenuHnd,
TPM_BOTTOMALIGN | TPM_LEFTALIGN | TPM_RETURNCMD | TPM_LEFTBUTTON,
MousePos.x, ScreenHeight, 0, MainWindow, NULL);
}
SetForegroundWindow(MainWindow);
DestroyMenu(MenuHnd);
switch (SelItem)
{
case 1:
Showing = TRUE;
ShowWindow(MainWindow, SW_SHOW);
break;
case 2:
ShutdownServer(MainWindow);
break;
default:
break;
}
}
void
ShutdownServer(HWND MainWindow)
{
int Answer;
Answer = MessageBox(MainWindow,
"Are you sure you want to shutdown the Sambar Server Controller?",
NAME, MB_YESNO | MB_ICONQUESTION);
// If they do destroy the main window and let the the rest fall in place...
if (Answer == IDYES)
DestroyWindow(MainWindow);
}
void Logger(void *argp)
{
int n;
int read;
int done;
char buffer[1024];
done = 0;
while (!done)
{
/* Read failure, close/shutdown */
if (!ReadFile(hPipe, buffer, 1000, &read, NULL))
{
// NT Server shutdown, quit now.
DestroyWindow(MainWindow);
PostQuitMessage(0);
CloseHandle(hPipe);
hPipe = NULL;
exit(0);
}
LogCount++;
if (LogCount > 200)
{
LogCount = 1;
SendMessage(MainList, LB_RESETCONTENT, 0, 0);
}
buffer[read] = '\0';
SendMessage(MainList, LB_ADDSTRING, 0, (LPARAM)(LPSTR)buffer);
n = SendMessage(MainList, LB_GETCOUNT, 0, 0);
SendMessage(MainList, LB_SETCURSEL, (unsigned int)n - 1, 0);
}
}