source: nscp/include/CheckMemory.h @ 1b7ae3d

0.4.00.4.10.4.2stable
Last change on this file since 1b7ae3d was 1b7ae3d, checked in by Michael Medin <michael@…>, 7 years ago

* empty log message *

  • Property mode set to 100644
File size: 3.1 KB
Line 
1#pragma once
2
3
4typedef BOOL (WINAPI *PFGlobalMemoryStatusEx)(LPMEMORYSTATUSEX lpBuffer);
5typedef BOOL (WINAPI *PFGlobalMemoryStatus)(LPMEMORYSTATUS lpBuffer);
6
7namespace CheckMemMethod
8{
9        const int None          = 0x0;
10        const int Extended      = 0x1;
11        const int Normal        = 0x2;
12}
13
14class CheckMemoryException {
15private:
16        std::string name_;
17        std::string msg_;
18        unsigned int error_;
19public:
20        CheckMemoryException(std::string name,std::string msg,unsigned int error) : name_(name), error_(error), msg_(msg) {};
21
22        std::string getError() {
23                return "Service: " + name_ + " caused: " + msg_;
24        }
25        unsigned int getErrorCode() { return error_; }
26};
27
28class CheckMemory {
29private:
30        typedef struct {
31                unsigned long long total;
32                unsigned long long avail;
33        } memType;
34public:
35        typedef struct {
36                unsigned long dwMemoryLoad; 
37                memType phys;
38                memType pageFile;
39                memType virtualMem;
40                double availExtendedVirtual;
41        } memData;
42
43public:
44        CheckMemory() : FEGlobalMemoryStatusEx(NULL), FEGlobalMemoryStatus(NULL), method_(CheckMemMethod::None)
45        {
46                hKernel32 = ::LoadLibrary(_TEXT("Kernel32"));
47                if (hKernel32) 
48                {
49                        FEGlobalMemoryStatusEx = (PFGlobalMemoryStatusEx)::GetProcAddress(hKernel32, _TEXT("GlobalMemoryStatusEx"));
50                        FEGlobalMemoryStatus = (PFGlobalMemoryStatus)::GetProcAddress(hKernel32, _TEXT("GlobalMemoryStatus"));
51                }
52                int method = getAvailableMethods();
53                if ((method&CheckMemMethod::Extended)==CheckMemMethod::Extended)
54                        method_ = CheckMemMethod::Extended;
55                else
56                        method_ = method;
57        }
58        virtual ~CheckMemory() {
59                if (hKernel32) FreeLibrary(hKernel32);
60        }
61
62        int getAvailableMethods() {
63                int ret = CheckMemMethod::None;
64                if (FEGlobalMemoryStatusEx)
65                        ret |= CheckMemMethod::Extended;
66                if (FEGlobalMemoryStatus)
67                        ret |= CheckMemMethod::Normal;
68                return ret;
69        }
70        int getSuggestedMethod();
71        int setMethod(int method) {
72                if ((getAvailableMethods()&method) != 0)
73                        method_ = method;
74                return method_;
75        }
76
77        memData getMemoryStatus() {
78                memData ret;
79                if (method_ == CheckMemMethod::Extended) {
80                        MEMORYSTATUSEX buffer;
81                        buffer.dwLength = sizeof(buffer);
82                        if (!FEGlobalMemoryStatusEx(&buffer))
83                                throw CheckMemoryException("CheckMemory", "GlobalMemoryStatusEx failed", GetLastError());
84                        ret.phys.total = buffer.ullTotalPhys;
85                        ret.phys.avail = buffer.ullAvailPhys;
86                        ret.virtualMem.total = buffer.ullTotalVirtual;
87                        ret.virtualMem.avail = buffer.ullAvailVirtual;
88                        ret.pageFile.total = buffer.ullTotalPageFile;
89                        ret.pageFile.avail = buffer.ullAvailPageFile;
90                } else if (method_ == CheckMemMethod::Normal) {
91                        MEMORYSTATUS buffer;
92                        buffer.dwLength = sizeof(buffer);
93                        if (!FEGlobalMemoryStatus(&buffer))
94                                throw CheckMemoryException("CheckMemory", "GlobalMemoryStatus failed", GetLastError());
95                        ret.phys.total = buffer.dwTotalPhys;
96                        ret.phys.avail = buffer.dwAvailPhys;
97                        ret.virtualMem.total = buffer.dwTotalVirtual;
98                        ret.virtualMem.avail = buffer.dwAvailVirtual;
99                        ret.pageFile.total = buffer.dwTotalPageFile;
100                        ret.pageFile.avail = buffer.dwAvailPageFile;
101                }
102                return ret;
103        }
104private:
105        int method_;
106        HMODULE hKernel32;
107        PFGlobalMemoryStatusEx  FEGlobalMemoryStatusEx;
108        PFGlobalMemoryStatus    FEGlobalMemoryStatus;
109};
Note: See TracBrowser for help on using the repository browser.