Window读写文件操作CreateFile

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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值