-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
102 lines (83 loc) · 2.67 KB
/
Utils.cpp
File metadata and controls
102 lines (83 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <sstream>
#include <fstream>
#include <windows.h>
#include <Lmcons.h>
#include <ctime>
#include <direct.h>
#include <tlhelp32.h>
#include "FileHandler.h"
#include "Utils.h"
const std::string getCurrentDateTime(bool includeHours) {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
if(includeHours)
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
else
strftime(buf, sizeof(buf), "%Y-%m-%d", &tstruct);
return buf;
}
std::string getUserName(){
TCHAR name [ UNLEN + 1 ];
DWORD size = UNLEN + 1;
GetUserName( (TCHAR*)name, &size );
std::stringstream ss;
std::string userName;
ss << name;
ss >> userName;
return userName;
}
std::wstring s2ws(const std::string& s){
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
const wchar_t* s2wct(std::string path){
std::wstring widepath;
for(int i = 0; i < path.length(); ++i)
widepath += wchar_t( path[i] );
return widepath.c_str();
}
std::string generateRandomNumber(){
srand (time(0));
int intRandomNumber = rand () % 10000+1;
char charRandomNumber[6];
std::string stringRandomNumber = std::to_string(intRandomNumber);
return stringRandomNumber;
}
void storeKey(std::string key){
std::ofstream store("C:\\ProgramData\\SecurityHelper\\systemconf.dll", std::ios::app);
store << key;
store.close();
}
std::string createId(){
mkdir("C:\\ProgramData\\SecurityHelper");
std::ofstream sysid("C:\\ProgramData\\SecurityHelper\\sysid.dat");
std::string id = generateRandomNumber();
sysid << id;
storeKey("First run!");
return id;
}
bool isProcessRunning(const char* name){
HANDLE SnapShot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if(SnapShot == INVALID_HANDLE_VALUE)
return false;
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(PROCESSENTRY32);
if( !Process32First(SnapShot, &procEntry) )
return false;
int processCount = 0;
do {
if(strcmp(procEntry.szExeFile, name) == 0 )
processCount++;
if(processCount == 2) // hack to check for duplicates since this function is used only for this process
return true;
} while( Process32Next( SnapShot, &procEntry ) );
return false;
}