forked from caiorss/C-Cpp-Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcurrentProcess.cpp
More file actions
62 lines (54 loc) · 1.92 KB
/
Copy pathcurrentProcess.cpp
File metadata and controls
62 lines (54 loc) · 1.92 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
// Brief: Get information about current process
// Author: Caio Rodrigues
//------------------------------------------------------
#include <iostream>
#include <string>
#include <cassert>
#include <windows.h>
// Return true if program was launched by clicking on it,
// return false if this program was launched from command line.
auto isInOwnConsole() -> bool {
DWORD procIDs[2];
DWORD maxCount = 2;
DWORD result = GetConsoleProcessList((LPDWORD)procIDs, maxCount);
return result != 1;
}
auto ExecutablePath = [](int pid){
HANDLE hProc = OpenProcess( PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ, FALSE, pid );
// Get process file path
std::string process_path(MAX_PATH, 0);
DWORD size = MAX_PATH;
QueryFullProcessImageNameA(hProc, 0, &process_path[0], &size);
process_path.resize(size);
CloseHandle(hProc);
return process_path;
};
auto CurrentDirectory = []{
DWORD size = ::GetCurrentDirectory(0, nullptr);
assert(size != 0);
std::string path(size + 1, 0x00);
size = ::GetCurrentDirectory(path.size(), &path[0]);
path.resize(size);
return path;
};
int main(){
// Get current process ID
DWORD pid = GetCurrentProcessId();
// Return module handle of current process
HMODULE hProc = GetModuleHandleA(nullptr);
// Get process module
std::cout << "=========== Current Process Info ========" << std::endl;
std::cout << "PID = " << pid << std::endl;
std::cout << "hProc = 0x" << hProc << std::endl;
std::cout << "Executable path = " << ExecutablePath(pid) << std::endl;
std::cout << "Current path = " << CurrentDirectory() << std::endl;
// Copy itself to Desktop
CopyFile(ExecutablePath(pid).c_str(), "C:\\Users\\archbox\\Desktop\\appinfo.exe", false) ;
// Stop program from exiting if it was launched by clicking on it.
if(!isInOwnConsole()){
std::cout << "\n ==> Type RETURN to exit." << std::endl;
std::cin.get();
}
return EXIT_SUCCESS;
}