SpiecsEngine
 
Loading...
Searching...
No Matches
ProcessLibrary.cpp
Go to the documentation of this file.
1/**
2* @file ProcessLibrary.cpp
3* @brief The ProcessLibrary Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
10
11#include <psapi.h>
12#include <stdio.h>
13
14namespace Spices {
15
16 bool ProcessLibrary::OpenProcess(const char* processPath, const char* commandLine)
17 {
19
20 STARTUPINFO StartInfo;
21 PROCESS_INFORMATION info;
22
23 ZeroMemory(&StartInfo, sizeof(StartInfo));
24 StartInfo.cb = sizeof(STARTUPINFO);
25 ZeroMemory(&info, sizeof(PROCESS_INFORMATION));
26
27 const std::wstring path = StringLibrary::CharToWChar(processPath);
28 const std::wstring command = StringLibrary::CharToWChar(commandLine);
29
30 if (!CreateProcess(
31 path.c_str(), // Process Name
32 const_cast<wchar_t*>(command.c_str()), // Command line
33 nullptr, // Process handle not inheritable
34 nullptr, // Thread handle not inheritable
35 FALSE, // Set handle inheritance to FALSE
36 NORMAL_PRIORITY_CLASS, // No creation flags
37 nullptr, // Use parent's environment block
38 nullptr, // Use parent's starting directory
39 &StartInfo, // Pointer to STARTUP INFO structure
40 &info // Pointer to PROCESS_INFORMATION structure
41 ))
42 {
43 std::stringstream ss;
44 ss << "Process: " << processPath << " Open Failed";
45
46 SPICES_CORE_WARN(ss.str());
47 return false;
48 }
49
50 return true;
51 }
52
53 bool ProcessLibrary::CloseProcess(const char* processName)
54 {
56
57 const std::string temp = std::string("C:/Windows/System32/TASKKILL.exe /F /IM ") + processName;
58 if(system(temp.c_str()) != 0)
59 {
60 SPICES_CORE_WARN("Process: " + std::string(processName) + " Close Failed")
61 return false;
62 }
63
64 return true;
65 }
66
68 {
70
71 PROCESS_MEMORY_COUNTERS pmc;
72 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
73 {
74 return pmc.WorkingSetSize / 1024.0f / 1024.0f / 1024.0f; // GB.
75 }
76
77 return 0;
78 }
79}
#define SPICES_PROFILE_ZONE
static bool CloseProcess(const char *processName)
Close a Process with command.
static bool OpenProcess(const char *processPath, const char *commandLine="")
Open a Process with command.
static float ProcessMemoryInUsed()
Get this Process Memory used( GB ).
Process Static Function Library.
static std::wstring CharToWChar(const char *c)
Transform char to wide char.
String Static Function Library.