| 1 | #pragma once |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | typedef BOOL (WINAPI *PFGlobalMemoryStatusEx)(LPMEMORYSTATUSEX lpBuffer); |
|---|
| 5 | typedef BOOL (WINAPI *PFGlobalMemoryStatus)(LPMEMORYSTATUS lpBuffer); |
|---|
| 6 | |
|---|
| 7 | namespace CheckMemMethod |
|---|
| 8 | { |
|---|
| 9 | const int None = 0x0; |
|---|
| 10 | const int Extended = 0x1; |
|---|
| 11 | const int Normal = 0x2; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | class CheckMemoryException { |
|---|
| 15 | private: |
|---|
| 16 | std::string name_; |
|---|
| 17 | std::string msg_; |
|---|
| 18 | unsigned int error_; |
|---|
| 19 | public: |
|---|
| 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 | |
|---|
| 28 | class CheckMemory { |
|---|
| 29 | private: |
|---|
| 30 | typedef struct { |
|---|
| 31 | unsigned long long total; |
|---|
| 32 | unsigned long long avail; |
|---|
| 33 | } memType; |
|---|
| 34 | public: |
|---|
| 35 | typedef struct { |
|---|
| 36 | unsigned long dwMemoryLoad; |
|---|
| 37 | memType phys; |
|---|
| 38 | memType pageFile; |
|---|
| 39 | memType virtualMem; |
|---|
| 40 | double availExtendedVirtual; |
|---|
| 41 | } memData; |
|---|
| 42 | |
|---|
| 43 | public: |
|---|
| 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 | } |
|---|
| 104 | private: |
|---|
| 105 | int method_; |
|---|
| 106 | HMODULE hKernel32; |
|---|
| 107 | PFGlobalMemoryStatusEx FEGlobalMemoryStatusEx; |
|---|
| 108 | PFGlobalMemoryStatus FEGlobalMemoryStatus; |
|---|
| 109 | }; |
|---|