他人の空似自作物置場

GetProcessMemory.zip/GetProcessMemory/GetProcessMemory.cpp


#include <iostream>

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/type_traits/remove_pointer.hpp>

#include <Windows.h>
#include <tlhelp32.h>
// PSAPI_VERSIONが2だとvistaで動かないため1で使用する
#define PSAPI_VERSION 1
#include <Psapi.h>

#pragma comment(lib, "psapi.lib")

typedef boost::shared_ptr<boost::remove_pointer<HANDLE>::type> SHARED_HANDLE;
void MyCloseHandle(const HANDLE handle) {
  if (handle != NULL) {
    ::CloseHandle(handle);
  }
}
SHARED_HANDLE ToSharedPtr(const HANDLE handle) {
  return SHARED_HANDLE(handle, &MyCloseHandle);
}

bool CreateProcessList(std::vector<boost::shared_ptr<PROCESSENTRY32W> > &result) {
  result.clear();
  HANDLE snapshotImpl = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if (snapshotImpl == INVALID_HANDLE_VALUE) {
    return false;
  }
  SHARED_HANDLE snapshot = ToSharedPtr(snapshotImpl);

  while (true) {
    const boost::shared_ptr<PROCESSENTRY32W> entry(new PROCESSENTRY32W());
    entry->dwSize = sizeof(PROCESSENTRY32W);
    if (result.size() == 0) {
      if (::Process32FirstW(snapshot.get(), entry.get()) == FALSE) {
        break;
      }
    } else {
      if (::Process32NextW(snapshot.get(), entry.get()) == FALSE) {
        break;
      }
    }
    result.push_back(entry);
  }
  return true;
}

bool FindProcess(boost::shared_ptr<PROCESSENTRY32W> &result, const char * const exeName) {
  std::vector<boost::shared_ptr<PROCESSENTRY32W> > processList;
  if (!CreateProcessList(processList)) {
    return false;
  }
  const auto &it = std::find_if(processList.begin(), processList.end(),
    [exeName](const boost::shared_ptr<PROCESSENTRY32W> entry){
      return std::equal(&exeName[0], &exeName[::strlen(exeName)], &entry->szExeFile[0]);
    }
  );
  if (it == processList.end()) {
    return false;
  }
  result.swap(*it);
  return true;
}

bool Check(unsigned long long int &result, const char * const exeName) {
  boost::shared_ptr<PROCESSENTRY32W> processEntry;
  if (!FindProcess(processEntry, exeName)) {
    return false;
  }
  const HANDLE processImpl = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, processEntry->th32ProcessID);
  if (processImpl == NULL) {
    return false;
  }
  const SHARED_HANDLE process = ToSharedPtr(processImpl);
  PROCESS_MEMORY_COUNTERS info;
  info.cb = sizeof(info);
  if (::GetProcessMemoryInfo(process.get(), &info, info.cb) == FALSE) {
    return false;
  }
  result = info.PagefileUsage;
  return true;
}

//int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE hPrevInstance, const LPSTR lpCmdLine, int nCmdShow) {
int main(const unsigned int argc, const char * const * const argv) {
  if (argc != 2) {
    std::cout << "usage: GetProcessMemory.exe exe_name" << std::endl;
    return 1;
  }
  const boost::filesystem::path path = boost::filesystem::current_path() / L"memory.csv";
  boost::filesystem::ofstream ofs(path, std::ios::binary | std::ios::app);
  //std::ostream &ofs = std::cout;
  const boost::posix_time::ptime ptime = boost::posix_time::second_clock::local_time();
  ofs << std::endl << boost::posix_time::to_iso_extended_string(ptime) << std::endl;
  std::cout << std::endl << boost::posix_time::to_iso_extended_string(ptime) << std::endl;
  while (true) {
    unsigned long long int memory;
    if (!Check(memory, argv[1])) {
      break;
    }
    ofs << memory << ",";
    std::cout << memory << ",";
    ofs.flush();
    ::Sleep(5 * 60 * 1000);
  }
  return 0;
}