FileConfig.h
#ifndef _PROCESS_CONFIG_H_
#define _PROCESS_CONFIG_H_
#include <windows.h>
#include <stdint.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <tchar.h>
using namespace std;
#define PROCESS_PID_LEN 6
#define PROCESS_NAME "notepad++.exe"
const char config_file_name[] = "config/pid.cfg";
namespace client {
typedef struct EnumHWndsArg
{
std::vector<HWND> *vecHWnds;
DWORD dwProcessId;
}EnumHWndsArg, *LPEnumHWndsArg;
class FileConfig
{
public:
static FileConfig* GetInitance();
bool Read(string& buffer);
bool Write(const string& buffer);
void Delete();
DWORD GetProcessIDByName(const char* pName);
HANDLE GetProcessHandleByID(int nID);
void GetHWndsByProcessID(DWORD processID, std::vector<HWND> &vecHWnds);
void LogPrint(const char* str, ...);
private:
FileConfig();
~FileConfig();
static FileConfig* process_config;
};
BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam);
} //client
#endif //_PROCESS_CONFIG_H_
FileConfig.cpp
#include "FileConfig.h"
namespace client {
FileConfig* FileConfig::process_config = new FileConfig();
FileConfig* FileConfig::GetInitance()
{
if (process_config) {
return process_config;
}
return NULL;
}
FileConfig::FileConfig()
{
}
FileConfig::~FileConfig()
{
}
bool FileConfig::Read(string& buffer)
{
char buf[PROCESS_PID_LEN] = {};
HANDLE pfile = CreateFileA(config_file_name, GENERIC_READ, 0, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_WRITE_THROUGH, NULL);
if (pfile == INVALID_HANDLE_VALUE) {
printf("read config file error.\n");
CloseHandle(pfile);
return false;
}
DWORD filesize = GetFileSize(pfile, NULL);
DWORD readsize = 0;
ReadFile(pfile, buf, filesize, &readsize, NULL);
buf[filesize] = 0;
buffer = buf;
CloseHandle(pfile);
return true;
}
bool FileConfig::Write(const string& buffer)
{
HANDLE pfile = CreateFileA(config_file_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_HIDDEN | FILE_FLAG_WRITE_THROUGH, NULL);
if (pfile == INVALID_HANDLE_VALUE) {
printf("write config file error.\n");
CloseHandle(pfile);
return false;
}
DWORD writesize = 0;
WriteFile(pfile, buffer.data(), strlen(buffer.data()), &writesize, NULL);
CloseHandle(pfile);
return true;
}
void FileConfig::Delete()
{
if (!DeleteFileA(config_file_name)) {
printf("[BrowserConfig] Delete file fail.\n");
}
}
DWORD FileConfig::GetProcessIDByName(const char* pName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return NULL;
}
DWORD pid = 0;
PROCESSENTRY32 pe = { sizeof(pe) };
for (BOOL ret = Process32First(hSnapshot, &pe); ret; ret = Process32Next(hSnapshot, &pe)) {
char exeFile[256] = {};
sprintf_s(exeFile, "%ws", pe.szExeFile);
if (strcmp(exeFile, pName) == 0) {
printf("%-6d %s\n", pe.th32ProcessID, pe.szExeFile);
CloseHandle(hSnapshot);
pid = pe.th32ProcessID;
return pid;
}
//printf("%-6d %s\n", pe.th32ProcessID, pe.szExeFile);
}
CloseHandle(hSnapshot);
return pid;
}
HANDLE FileConfig::GetProcessHandleByID(int nID)
{
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, nID);
}
void FileConfig::GetHWndsByProcessID(DWORD processID, std::vector<HWND> &vecHWnds)
{
EnumHWndsArg wi;
wi.dwProcessId = processID;
wi.vecHWnds = &vecHWnds;
EnumWindows(lpEnumFunc, (LPARAM)&wi);
}
void FileConfig::LogPrint(const char* strOutputString, ...)
{
char strBuffer[4096] = { 0 };
va_list vlArgs;
va_start(vlArgs, strOutputString);
_vsnprintf_s(strBuffer, sizeof(strBuffer) - 1, strOutputString, vlArgs);
va_end(vlArgs);
OutputDebugStringA(strBuffer);
}
BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam)
{
EnumHWndsArg *pArg = (LPEnumHWndsArg)lParam;
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
if (processId == pArg->dwProcessId)
{
pArg->vecHWnds->push_back(hwnd);
printf("%p\n", hwnd);
}
return TRUE;
}
} // client
main.cpp
#include "FileConfig.h"
int main()
{
char snPid[PROCESS_PID_LEN] = {};
DWORD id = FileConfig::GetInitance()->GetProcessIDByName(PROCESS_NAME);
sprintf_s(snPid, PROCESS_PID_LEN, "%u", id);
pid = snPid;
FileConfig::GetInitance()->Write(pid);
FileConfig::GetInitance()->Read(data);
return 0;
}