-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprograma.cpp
More file actions
96 lines (78 loc) · 2.71 KB
/
programa.cpp
File metadata and controls
96 lines (78 loc) · 2.71 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
#include "programa.h"
// instala o programa inicializando a função createProcess
bool programa::instalar()
{
if (Nome.length() <= 0 || Diretorio.length() <= 0 || Argumentos.length() <= 0)
return false;
cout << "> Iniciando instalação do " << Nome.c_str() << "..." << endl;
createProcess(Diretorio, Argumentos);
return true;
}
// inicia o processo e espera ele fechar, retornando e printando um exitCode
PROCESS_INFORMATION programa::createProcess(string pszPath, string pszArguments)
{
string pszCommand;
pszCommand.append(pszPath);
pszCommand.append(" ");
pszCommand.append(pszArguments);
STARTUPINFO StartInfo;
PROCESS_INFORMATION ProcessInfo;
DWORD exitCode = { 0 };
int lErrorCode = 0;
memset(&StartInfo, 0, sizeof(StartInfo));
StartInfo.cb = sizeof(StartInfo);
StartInfo.wShowWindow = SW_HIDE;
StartInfo.dwFlags |= STARTF_USESHOWWINDOW;
//ERRO ESTÁ AQUI!!!! CONVERTER PSZCOMMAND PARA LPWSTR SEM DAR ERRO :(
//wstring stemp = wstring(pszCommand.begin(), pszCommand.end());
//LPCWSTR sw = stemp.c_str();
BOOL proc = CreateProcess(0, (char *)pszCommand.c_str(), 0, 0, TRUE, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW, 0, 0, &StartInfo, &ProcessInfo);
//cout << "CONVERSAO: " << CA2T(pszCommand.c_str()) << endl;
if (proc)
{
lErrorCode = 0;
}
else
{
lErrorCode = GetLastError();
cout << lErrorCode;
}
if (WaitForSingleObject(ProcessInfo.hProcess, INFINITE) == WAIT_FAILED)
cout << "> WaitForSingleObject() falhou! Erro: " << lErrorCode << endl;
proc = GetExitCodeProcess(ProcessInfo.hProcess, &exitCode);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
if (!proc)
cout << "> Executou o comando, mas falhou ao retornar o exitcode." << endl;
else
cout << "> Processo executado com sucesso. ExitCode: " << exitCode << endl;
return ProcessInfo;
}
string programa::ReplaceAll(string str, const string& from, const string& to) {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
wstring programa::widen(const string& str)
{
wostringstream wstm;
const ctype<wchar_t>& ctfacet =
use_facet< ctype<wchar_t> >(wstm.getloc());
for (size_t i = 0; i<str.size(); ++i)
wstm << ctfacet.widen(str[i]);
return wstm.str();
}
wstring programa::s2ws(const 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;
}