⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 win32.c

📁 系统任务管理器
💻 C
📖 第 1 页 / 共 5 页
字号:
 /* GKrellM
|  Copyright (C) 1999-2006 Bill Wilson
|
|  Author:  Bill Wilson    billw@gkrellm.net
|  Latest versions might be found at:  http://gkrellm.net
|
|  Windows code: Bill Nalen bill@nalens.com
|
|  This program is free software which I release under the GNU General Public
|  License. You may redistribute and/or modify this program under the terms
|  of that 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.  Version 2 is in the
|  COPYRIGHT file in the top level directory of this distribution.
| 
|  To get a copy of the GNU General Puplic License, write to the Free Software
|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#if defined(WIN32_CLIENT)
#include "../gkrellm.h"
#include "../gkrellm-sysdeps.h"
#include "../gkrellm-private.h"
#include "../win32-plugin.h"
#else
#include "../../server/gkrellmd.h"
#define	GKRELLM_DIR				".gkrellm2"
#endif

#include "../inet.h"

#include <limits.h>
#include <errno.h>
#include <largeint.h>
#include <WINIOCTL.H>
#include <TCHAR.H>

#if defined(USE_PDH)
// windows NT/2k/XP
#include <pdh.h>
#include <PDHMSG.H>
#include <LMCONS.H>
#include <lmwksta.h>
#include <lmapibuf.h>
#else
// windows 95/98/Me
#include <tlhelp32.h>
#endif

// *****************************************************************

#define MAX_NET_NAME 6
#define MAX_NET_ADAPTERS 10
#define MAX_DISK_NAME 6
#define MAX_DISKS 10
#define MAX_CPU 6

#if defined(USE_PDH)
    // windows NT/2k/XP
    #define numPerfKeys 12
#else
    // windows 95/98/Me
    #define numPerfKeys 6
#endif

//******************************************************************

static gint		numCPUs;
static gulong	swapin, swapout;
static TCHAR netName[MAX_NET_ADAPTERS + 1][MAX_NET_NAME + 1];
static TCHAR diskName[MAX_DISKS + 1][MAX_DISK_NAME + 1];
static gint numAdapters = 0;
static int rx[MAX_NET_ADAPTERS + 1], tx[MAX_NET_ADAPTERS + 1];
static int initDone = 0;
static OSVERSIONINFO info;
static int fswin95 = 0;
static gchar* sname;
static gchar* hostname;

#if defined(WIN32_CLIENT)
    static win32_plugin_callbacks plugin_callbacks;
    win32_plugin_callbacks* callbacks;
#endif
static TCHAR* perfKeys[numPerfKeys];

#if defined(USE_PDH)  // windows NT/2k/XP
    static HQUERY query = 0;
    static HCOUNTER cpuUserCounter[MAX_CPU + 1];
    static HCOUNTER cpuSysCounter[MAX_CPU + 1];
    static HCOUNTER processCounter;
    static HCOUNTER threadCounter;
    static HCOUNTER uptimeCounter;
    static HCOUNTER diskReadCounter[MAX_DISKS + 1];
    static HCOUNTER diskWriteCounter[MAX_DISKS + 1];
    static HCOUNTER netRecCounter[MAX_NET_ADAPTERS + 1];
    static HCOUNTER netSendCounter[MAX_NET_ADAPTERS + 1];
    static PDH_STATUS status;
    static int winVer = 1;
#else  // windows 95/98/Me
    HKEY cpuperfKey;
    HKEY threadPerfKey;
    HKEY diskreadPerfKey;
    HKEY diskwritePerfKey;
    HKEY netPerfKey[MAX_NET_ADAPTERS + 1];
    DWORD dataType,dataLen=8192;
    char data[8192];	// Buffer to communicate with registry. (way bigger than neeeded)
    static int winVer = 0;  // used for make Pdh_ calls for performance data under NT and higher
#endif

// *****************************************************************

// local function protos
void win32_sys_cpu_stop(void);
void win32_sys_disk_stop(void);
void win32_init_callbacks(void);
void win32_sys_process_stop(void);
void win32_sys_net_stop(void);
void readPerfKeyFile(void);
void placePerfKey(const char* key, const char* item);

#if !defined(WIN32_CLIENT)
gchar *gkrellm_homedir(void)
{
	gchar	*homedir;

	homedir = (gchar *) g_get_home_dir();
	if (!homedir)
		homedir = ".";
	return homedir;
}
#endif

#if defined(WIN32) && defined(_WINDOWS) && !defined(WIN32_CLIENT)
UINT WM_GKRELLMCALLBACK;
int notDone;

LRESULT CALLBACK wndProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
    if (uMsg == WM_GKRELLMCALLBACK && lParam == WM_RBUTTONUP) {
        PostQuitMessage(0);
        notDone = 1;
        return 0;
    }
    else {
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

void setupServerWindow(HINSTANCE hInstance)
{
    NOTIFYICONDATA nid;
    HWND hwnd;
    WNDCLASSEX wndClass;
    char* CLASS_NAME = "GKrellMServer";

    notDone = 0;
        
    wndClass.cbSize = sizeof(WNDCLASSEX);
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = wndProc;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hInstance = hInstance;
    wndClass.hIcon = NULL;
    wndClass.hCursor = NULL;
    wndClass.hbrBackground = 0;
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = CLASS_NAME;
    wndClass.hIconSm = NULL;
    RegisterClassEx(&wndClass);
    
    hwnd = CreateWindow(CLASS_NAME, "GKrellMServer", SW_HIDE, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);

    // Create System Tray Icon
    WM_GKRELLMCALLBACK = RegisterWindowMessage(TEXT("GKrellMCallback"));

    nid.cbSize = sizeof(NOTIFYICONDATA);                
    nid.hWnd = hwnd;
    nid.uID = 1;   
    nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
    nid.uCallbackMessage = WM_GKRELLMCALLBACK;                                                                               
    strcpy(nid.szTip,"GKrellM Server for Windows");
    nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON3));
    Shell_NotifyIcon(NIM_ADD, &nid);
}
#endif


//***************************************************************************

void gkrellm_sys_main_init(void)
{
    WSADATA wsdata;
    int err;
    HMODULE mod = 0;
    FARPROC proc = 0;

    // initialize winsock
    if (_GK.debug_level & DEBUG_SYSDEP)
        printf("Starting Winsock\n");
    err = WSAStartup(MAKEWORD(1,1), &wsdata);
    if (err != 0 && _GK.debug_level & DEBUG_SYSDEP) 
        printf("Starting Winsock failed with error code %i\n", err);

#if defined(USE_PDH)
    if (query == 0 && winVer > 0) {
        if (_GK.debug_level & DEBUG_SYSDEP)
            printf("Opening Pdh");
        status = PdhOpenQuery(NULL, 0, &query);
        if (status != ERROR_SUCCESS) {
            if (_GK.debug_level & DEBUG_SYSDEP)
                printf("Opening Pdh failed with error code %i\n", status);
        }
    }
#endif

    // do this once
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&info);

    // see if we can use the GetDiskFreeSpaceEx function
    mod = GetModuleHandle("KERNEL32.DLL");
    proc = GetProcAddress(mod, "GetDiskFreeSpaceExA");
    if (proc != NULL) {
        fswin95 = 1;
    }

    // we don't have local mail on Windows (yet?)
    gkrellm_mail_local_unsupported();
    
    // try to make nice
    //SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);

    // initialize call back structure for plugins
    if (_GK.debug_level & DEBUG_SYSDEP)
        printf("Initializing callbacks\n");
    win32_init_callbacks();

    // get registry key names
    readPerfKeyFile();
}

void gkrellm_sys_main_cleanup(void)
{
    int i;
#if defined(WIN32_CLIENT)
    NOTIFYICONDATA nid;

    // remove system tray icon
    nid.cbSize = sizeof(NOTIFYICONDATA);                
    nid.hWnd = GDK_WINDOW_HWND(gkrellm_get_top_window()->window);
    nid.uID = 1;   
    nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
    nid.uCallbackMessage = WM_CLOSE;                                                                               
    strcpy(nid.szTip,"GKrellM for Windows");
    nid.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    Shell_NotifyIcon(NIM_DELETE, &nid);

    if (_GK.withdrawn) {
        // remove from bluebox slit
		HWND hwnd = FindWindowEx(NULL, NULL, "BControl", "BSlitWindow");
    	SendMessage(hwnd, BM_SLITMESSAGE, BSM_DELETEWINDOW, (LPARAM) GDK_WINDOW_HWND(gkrellm_get_top_window()->window));
		RemoveProp(GDK_WINDOW_HWND(gkrellm_get_top_window()->window), "BSlitControl");
		SetParent(GDK_WINDOW_HWND(gkrellm_get_top_window()->window), NULL);
        if (_GK.debug_level & DEBUG_SYSDEP)
            printf("Removing from slit\n");
		//SetWindowPos(0, m_nLeft, m_nTop, m_nWidth, m_nHeight, SWP_NOZORDER);
    }

#endif

    if (_GK.debug_level & DEBUG_SYSDEP)
        printf("Waiting for mail checking thread to end.\n");
    while (gkrellm_mail_get_active_thread() != NULL) {
        // wait here till it finishes
        // in case we are trying to get mail info
        Sleep(500);
    }

    // stop performance gathering
    if (_GK.debug_level & DEBUG_SYSDEP)
        printf("Stopping perfomance monitoring.\n");
    win32_sys_cpu_stop();
    win32_sys_disk_stop();
    win32_sys_process_stop();
    win32_sys_net_stop();

    // close keys
#if defined(USE_PDH)
    if (_GK.debug_level & DEBUG_SYSDEP)
        printf("Closing Pdh\n");
    PdhCloseQuery(query);
#else
    if (_GK.debug_level & DEBUG_SYSDEP)
        printf("Closing registry keys.\n");
    if (cpuperfKey != 0)
        RegCloseKey(cpuperfKey);	
    if (threadPerfKey != 0)
        RegCloseKey(threadPerfKey);
    if (diskreadPerfKey != 0)
        RegCloseKey(diskreadPerfKey);	
    if (diskwritePerfKey != 0)
        RegCloseKey(diskwritePerfKey);	
    for (i = 0; i < numAdapters; i++)
        RegCloseKey(netPerfKey[i]);
#endif

    // free up these strings
    for (i = 0; i < numPerfKeys; i++)
        free(perfKeys[i]);

    g_free(sname);
    g_free(hostname);

    // stop winsock
    if (_GK.debug_level & DEBUG_SYSDEP)
        printf("Closing Winsock\n");
    WSACleanup();
}

// only need to collect query data once for all those monitors that use it
static void win32_read_proc_stat(void)
	{
	static gint	data_read_tick	= -1;

	if (data_read_tick == gkrellm_get_timer_ticks())	/* One read per tick */
		return;

	data_read_tick = gkrellm_get_timer_ticks();

#if defined(USE_PDH)
    if (winVer > 0 && query != 0) {
        status = PdhCollectQueryData(query);
    }
#endif

	}


/* ===================================================================== */
/* Sensor interface */
/* ===================================================================== */

// interface to work with shared memory for MBM5

// ---------------------------------------------------------------------------
// --------------------------------------- Copyright 2001 A@majland.org ------
// --------------------------------------- Alteration for use in Visual C ----
// --------------------------------------- By Chris Zahrt techn0@iastate.edu -
// ---------------------------------------------------------------------------
//
//  Version     : 0.1
//  Date        : 02-27-2002
//
//  MBM         : version 5.1
//
//  Author      : Chris Zahrt techn0@iastate.edu (visual c alterations)
//                http://techn0.dhs.org/programming/vcmbmsm.html
//                Anders@Majland.org (author of original c code)
//                http://www.majland.org/sw/mbmcaf
//
//  Licence     : Cardware. (Send me a note/email if you find it usefull.)
//                Basically you may use it as you see fit as long as the origin
//                of the code remains clear
//
//  History     :
//		  0.1 02-27-2002 conversion of 0.3 borland to this version

// Update for MBM 5.1.9 by Bill Nalen bill@nalens.com

// ---------------------------------------------------------------------------

#define NrTemperature 32
#define NrVoltage 16
#define NrFan 16
#define NrCPU 4

static double temperatures[NrTemperature];
static double voltages[NrVoltage];
static double fans[NrFan];

//    enum Bus
#define BusType     char
#define ISA         0
#define SMBus       1
#define VIA686Bus   2
#define DirectIO    3

//    enum SMB
#define SMBType         char
#define smtSMBIntel     0
#define smtSMBAMD       1
#define smtSMBALi       2
#define smtSMBNForce    3
#define smtSMBSIS       4

// enum Sensor Types
#define SensorType      char
#define stUnknown       0
#define stTemperature   1
#define stVoltage       2
#define stFan           3
#define stMhz           4
#define stPercentage    5    

typedef struct {
    SensorType  iType;          // type of sensor
    int         Count;          // number of sensor for that type
} SharedIndex;

typedef struct {
    SensorType ssType;          // type of sensor
    unsigned char ssName[12];   // name of sensor
    char sspadding1[3];         // padding of 3 byte
    double ssCurrent;           // current value
    double ssLow;               // lowest readout
    double ssHigh;              // highest readout
    long ssCount;               // total number of readout
    char sspadding2[4];         // padding of 4 byte
    long double ssTotal;        // total amout of all readouts
    char sspadding3[6];         // padding of 6 byte
    double ssAlarm1;            // temp & fan: high alarm; voltage: % off;
    double ssAlarm2;            // temp: low alarm

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -