MFC入门到精通1-创建项目

开发环境搭建

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
出现错误
在这里插入图片描述
第一步,进入下列目录C:\Program Files (x86)\Windows Kits\10\bin\10.0.15063.0\x86,复制rc.exe和rcdll.dll两个文件

第二步,进入目录C:\Program Files (x86)\Windows Kits\10\bin\x86,将上述文件粘贴,即可,问题解决。

一个简单的demo

1.切换到资源状态
在这里插入图片描述
2.打开页面布局在这里插入图片描述
3.添加按钮
在这里插入图片描述
修改每个按钮的属性值

CaptionID
打开驱动设备IDC_BUTTON_CREATE
关闭驱动设备IDC_BUTTON_CLOSE
写数据IDC_BUTTON_IO_WRITE
读数据IDC_BUTTON_IO_READ
添加进程保护IDC_BUTTON_ADD
卸载进程保护IDC_BUTTON_REMOVE

在这里插入图片描述
按钮对齐快捷方式
在这里插入图片描述
双击打开驱动设备按钮进入代码行

//打开驱动设备对象
HANDLE DeviceHandle = NULL;
void CMFCApplicationDlg::OnBnClickedButtonCreate()
{
	// TODO: 在此添加控件通知处理程序代码
	DeviceHandle = CreateFileW(
		L"\\??\\MyDriver",
		GENERIC_READ | GENERIC_WRITE,
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
}

双击关闭驱动设备按钮进入代码行

void CMFCApplicationDlg::OnBnClickedButtonClose()
{
	// TODO: 在此添加控件通知处理程序代码
	CloseHandle(DeviceHandle);//IRP_MJ_CLOSE
}

双击写数据按钮进入代码行

#include <winioctl.h>
#define IOTEST CTL_CODE(FILE_DEVICE_UNKNOWN,0X800,METHOD_BUFFERED,FILE_ANY_ACCESS)

void CMFCApplicationDlg::OnBnClickedButtonIoWrite()
{
	// TODO: 在此添加控件通知处理程序代码
	DWORD dwRetSize = 0;//返回字节数
	typedef struct TINPUT_BUF
	{
		DWORD m_arg1;
		DWORD m_arg2;
		DWORD m_arg3;
		DWORD m_arg4;
		DWORD m_arg5;
		DWORD m_arg6;
	}TINPUT_BUF;
	//结构
	TINPUT_BUF inBuf = { 1,2,3,4,5,0x6ABC666 };
	//数组示例
	DWORD OutBuf[6] = { 0 };//输出缓冲区
	DeviceIoControl(
		DeviceHandle,//CreateFile打开驱动设备 返回的句柄
		IOTEST,//控制码 CTL_CODE
		&inBuf,//输入缓冲区指针
		sizeof(inBuf),//输入缓冲区大小
		&OutBuf,//返回缓冲区
		sizeof(OutBuf),//返回缓冲区大小
		&dwRetSize, //返回字节数
		NULL);

	//打印返回参数
	CString csStr;
	csStr.Format(L"EXE: 接收驱动层返回的参数(%x,%x,%x,%x,%x,%x  dwRetSize=%d", OutBuf[0], OutBuf[1], OutBuf[2], OutBuf[3], OutBuf[4], OutBuf[5], dwRetSize);
	OutputDebugStringW(csStr);
}

双击添加进程保护

	// TODO: 在此添加控件通知处理程序代码
	//添加保护
	UpdateData(TRUE);//窗口数据更新到变量
	DWORD dwRetSize = 0;//返回字节数
	char buftest[256];
	sprintf_s(buftest, "yjx:EXE R3 读写测试 控制码=%X\n", IO_READ_WRITE_TEST);
	OutputDebugStringA(buftest);
	//int 传入数据[3] = { 3,7,8 };
	UINT32  传入数据 = m_PID;// { 3, 6, 5 };
	//数组示例
	int OutBuf = 0;
	DeviceIoControl(
		DeviceHandle,//CreateFile打开驱动设备 返回的句柄
		IO_添加受保护的PID,//控制码 CTL_CODE
		&传入数据,//输入缓冲区指针
		sizeof(传入数据),//输入缓冲区大小
		&OutBuf,//输出缓冲区
		sizeof(OutBuf),//返回缓冲区大小
		&dwRetSize, //返回字节数
		NULL);

双击移除进程保护

//移出保护
	UpdateData(TRUE);//窗口数据更新到变量
	DWORD dwRetSize = 0;//返回字节数
	char buftest[256];
	sprintf_s(buftest, "yjx:EXE R3 读写测试 控制码=%X\n", IO_READ_WRITE_TEST);
	OutputDebugStringA(buftest);
	//int 传入数据[3] = { 3,7,8 };
	UINT32  传入数据 = m_PID;// { 3, 6, 5 };
	//数组示例
	int OutBuf = 0;
	DeviceIoControl(
		DeviceHandle,//CreateFile打开驱动设备 返回的句柄
		IO_删除受保护的PID,//控制码 CTL_CODE
		&传入数据,//输入缓冲区指针
		sizeof(传入数据),//输入缓冲区大小
		&OutBuf,//输出缓冲区
		sizeof(OutBuf),//返回缓冲区大小
		&dwRetSize, //返回字节数
		NULL);

静态编译会自动把MFC的库编译进去,不然的话会在目标系统上跑不起来。如果目标系统没有2017的支持库,或者是开发环境。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Forger 推荐的学习windows 编程的所有书籍都在这儿: windows 编程 windows MFC入门精通 windows 高级编程 windows 网络编程 Programming Windows Programming Windows with MFC Programming Applications for Windows Network Programming for Microsoft Windows Programming Windows by Charles Petzold. The book to get on Win32 API. If you want to write programs using just the API (which is what this tutorial covers), you need this book. 如果你想只用windows API编程,这本书是入门经典,你需要这本书。 Programming Windows with MFC by Jeff Prosise. If you want to venture into MFC (AFTER becoming fully accustomed to using the Win32 API), this is the book for you. If you don't like MFC but intend on getting a job doing windows developement, get this anyway, it's better to know than not. 如果你想用MFC这本书是你想要的 Programming Applications for Windows by Jeffrey Richter. Not for newbies, if you want to be up on managing processes and threads, dlls, windows memory management, exception handling, and hooking into the system, then this is the book for you. 如果你想了解操作系统内幕,这本书是经典,书中包含了进程,线程,动态库,windows内存管理,异常处理。。。 Visual C++ Windows Shell Programming by Dino Esposito. For anyone interested in the visual and user-friendly aspects of windows, this book covers writing extentions to the windows shell, working efficiently with files and drag and drop, customizing the taskbar and windows explorer, and numerous other tricks. Well worthwhile for anyone writing GUI apps in windows. Network Programming for Microsoft Windows Up to date information on network programming, including NetBIOS, mailslots and pipes, and of course the ever important windows sockets, complete with winsock2 and raw sockets. Also contains specific information on the various windows platforms including 2000 and CE.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值