Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
dump_own_process.cpp
//Title of this code //Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86 #include <iostream> #include <cstdlib> #include <string> #include <stdio.h> #include <windows.h> #include <Psapi.h> //#include <dirent.h> #include <tchar.h> #define BUFSIZE MAX_PATH int listdir(const char *path) { HANDLE hFind; WIN32_FIND_DATA data; char p[BUFSIZE]; sprintf(p, "%s\\*.*", path); hFind = FindFirstFile(p, &data); if (hFind != INVALID_HANDLE_VALUE) { do { printf("%s\n", data.cFileName); } while (FindNextFile(hFind, &data)); FindClose(hFind); } return 0; } void hexdump(void *mem, unsigned int len, int cols_count) { unsigned int i, j; for(i = 0; i < len + ((len % cols_count) ? (cols_count - len % cols_count) : 0); i++) { /* print offset */ if(i % cols_count == 0) { printf("0x%06x: ", i); } /* print hex data */ if(i < len) { printf("%02x ", 0xFF & ((char*)mem)[i]); } else /* end of block, just aligning for ASCII dump */ { printf(" "); } /* print ASCII dump */ if(i % cols_count == (cols_count - 1)) { for(j = i - (cols_count - 1); j <= i; j++) { if(j >= len) /* end of block, not really printing */ { putchar(' '); } else if(isprint(((char*)mem)[j])) /* printable char */ { putchar(0xFF & ((char*)mem)[j]); } else /* other char */ { putchar('.'); } } putchar('\n'); } } } int main(int argc, char **argv) { HANDLE processHandle = NULL; TCHAR filename[MAX_PATH]; processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()); printf("pH = %d\n", processHandle); if (processHandle != NULL) { if (GetModuleFileNameEx(processHandle, NULL, filename, MAX_PATH) == 0) { std::wcerr << "Failed to get module filename." << std::endl; } else { std::cout << "Module filename is: " << filename << std::endl; } CloseHandle(processHandle); } else { std::wcerr << "Failed to open process." << std::endl; } if ( processHandle ) { char fn[MAX_PATH]; fn[0] = 0; GetProcessImageFileNameA(NULL,fn, MAX_PATH); printf("fn = %s\n", fn); } char fn1[MAX_PATH]; fn1[0] = 0; fn1[1] = 0; DWORD len = MAX_PATH; QueryFullProcessImageName(NULL, PROCESS_NAME_NATIVE, fn1, &len); printf("fn1 = %s\n", fn1); const char quote = '"' ; const char space = ' ' ; const std::string root_path = "C:/" ; // favour using '/' as the directory separator (back-slash requires escape) const std::string executable_name = "Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/dumpbin.exe" ; const std::string program = root_path + quote + executable_name + quote ; std::cout << "program: " << program << '\n' ; const std::string arg1 = "-dependents" ; std::cout << " arg1: " << arg1 << '\n' ; const std::string arg_file_name = "Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe" ; const std::string arg2 = root_path + quote + arg_file_name + quote ; std::cout << " arg2: " << arg2 << '\n' ; const std::string cmd = program + space + arg1 + space + arg2 ; std::cout << "command: " << cmd << "\n---------------\n\n" << std::flush ; std::system( cmd.c_str() ) ; TCHAR Buffer[BUFSIZE]; DWORD dwRet; dwRet = GetCurrentDirectory(BUFSIZE, Buffer); if( dwRet == 0 ) { printf("GetCurrentDirectory failed (%d)\n", GetLastError()); return 1; } printf("cwd = %s\n", Buffer); listdir(Buffer); FILE *f = fopen(argv[0], "rb"); printf("f = %p\n", (void*)f); if ( f ) { printf("a.exe exists!\n"); int ret; size_t size; fseek(f, 0, SEEK_END); // seek to end of file size = ftell(f); // get current file pointer fseek(f, 0, SEEK_SET); // seek back to beginning of fi char *buf = (char *)malloc(size); if ( buf ) { fread(buf, 1, size, f); } fclose(f); if ( buf ) { hexdump(buf, size, 16); } } return 0; }
run
|
edit
|
history
|
help
0
hyy
C++ move/copy constructor and assignment demo
Unicode_write2file
Visual Studio expand modified
Removing __unaligned specifier partial solution
c++functor
move swap
hangman
Attribute of qi parsers
regex select, find