C++实现Windows的运行(Win+R)

本文章学习:
1、system函数调用
2、#include <windows.h>的添加
3、主程序

1、& 2、了解system函数的运用
system函数可以调用windows的程序,但必须先导入windows.h模块
示例代码

#include <bits/stdc++.h>
#include <windows.h>
int main()
{
    system ("notepad");
    return 0;
}

学过cmd的朋友都知道,在命令提示符中输入notepad可以打开记事本,在C++中,同样可以用system函数做到这点。
因此,只要我们把用户输入的各种可能用if语句写出来,就可以实现C++的运行程序了(Win+R打开运行窗口)

3、程序优化
目前,我们的程序代码还是这样的:

#include <bits/stdc++.h>
#include <windows.h>
#include <cmath>
using namespace std;

int main(void)
{
	cout << "此程序为C++编写的Windows运行程序,与原版运行有着相似的功能. . ." <<endl; 
	cout << "作者:Jerry"<<endl;
	cout << "请输入指令: ";
	string input;
	cin >> input;
	if(input == "cmd")
	{
		cout << "打开系统cmd"<<endl;
		system ("cmd.exe"); 
		cout << "请输入指令: ";
	}
	if(input == "notepad")
	{
		cout << "打开记事本"<<endl;
		system ("notepad.exe");
		cout << "请输入指令: ";
	}
	if(input == "mspaint")
	{
		cout << "打开画图"<<endl;
		system ("mspaint.exe"); 
		cout << "请输入指令: ";
	}
	if(input == "shutdown")
	{
		cout << "警告: 系统即将关机, 是否继续, 继续按 Enter ,否则直接退出"<<endl;
		system ("pause");
		system ("shutdown -s -t 60");
		cout << "请输入指令: ";
	}
	if(input == "shutd_false")
	{
		cout << "此命令可取消定时关机"<<endl;
		system ("shutdown -a");
		cout << "已取消关机"<<endl; 
		cout << "请输入指令: ";
	}
	if(input == "winver")
	{
		cout << "查看系统版本"<<endl;
		system ("winver");
		cout << "请输入指令: ";
	}
	if(input == "slmgr.vbs")
	{
		cout << "查看系统激活状态"<<endl;
		system ("slmgr.vbs -dlv");
		cout << "请输入指令: ";
	}
	if(input == "calc")
	{
		cout << "打开计算器"<<endl;
		system ("calc");
		cout << "请输入指令: ";
	}
	if(input == string("exit")) {
			cout << "感谢您的使用..." << endl;
			cout << "未经作者允许, 禁止转载"<<endl;
			cout << "2019 - 2022 Jerry. All Rights Reserved."<<endl;
			break; 
		}
	if(input == "chkdsk")
	{
		cout << "磁盘扫描工具"<<endl;
		cout << "默认扫描C盘"<<endl;
		system ("chkdsk C:");
		cout << "请输入指令: ";
	}
	if(input == "tree")
	{
		cout << "显示树形目录"<<endl;
		cout << "默认为C盘"<<endl;
		system ("tree C:");
		cout << "请输入指令: ";
	}
	if(input == "dir")
	{
		cout << "显示所有文件"<<endl;
		cout << "默认参数为: dir /s /a"<<endl;
		system ("dir /s /a");
		cout << "请输入指令: ";
	}
	if(input == "taskmgr")
	{
		cout << "打开任务管理器"<<endl;
		system ("taskmgr.exe");
		cout << "请输入指令: ";
	}
	if(input == "explorer")
	{
		cout << "打开桌面资源管理器";
		system ("explorer.exe");
		cout << "请输入指令: ";
	}
	if(input == "systeminfo")
	{
		cout << "系统配置信息"<<endl;
		system ("systeminfo");
		cout << "请输入指令: ";
	}
	if(input == "ping")
	{
		cout << "对IP地址发送数据包"<<endl;
		cout << "默认参数: ping 127.0.0.1 -t -l 65500"<<endl;
		system ("ping 127.0.0.1 -t -l 65500");
		cout << "请输入指令: "; 
	}
    
	return true; 
}

运行后可以发现,程序只让用户输入了一次,执行命令后就关闭了。。。

这怎么行呢?难道用户想执行多个程序还要重开吗?

我们只需要在主程序的适当位置加上

while(true)
{
...   //中间的程序(省略)
}

让用户重复输入命令
最终代码

#include <bits/stdc++.h>
#include <windows.h>
#include <cmath>
using namespace std;

int main(void)
{
	system ("title 命令提示符"); 
	cout << "此程序为C++编写的Windows运行程序,与原版运行有着相似的功能. . ." <<endl; 
	cout << "作者:Jerry"<<endl;
	cout << "请输入指令: ";
	while(true){
	string input;
	cin >> input;
	if(input == "cmd")
	{
		cout << "打开系统cmd"<<endl;
		system ("cmd.exe"); 
		cout << "请输入指令: ";
	}
	if(input == "notepad")
	{
		cout << "打开记事本"<<endl;
		system ("notepad.exe");
		cout << "请输入指令: ";
	}
	if(input == "mspaint")
	{
		cout << "打开画图"<<endl;
		system ("mspaint.exe"); 
		cout << "请输入指令: ";
	}
	if(input == "shutdown")
	{
		cout << "警告: 系统即将关机, 是否继续, 继续按 Enter ,否则直接退出"<<endl;
		system ("pause");
		system ("shutdown -s -t 60");
		cout << "请输入指令: ";
	}
	if(input == "shutd_false")
	{
		cout << "此命令可取消定时关机"<<endl;
		system ("shutdown -a");
		cout << "已取消关机"<<endl; 
		cout << "请输入指令: ";
	}
	if(input == "winver")
	{
		cout << "查看系统版本"<<endl;
		system ("winver");
		cout << "请输入指令: ";
	}
	if(input == "slmgr.vbs")
	{
		cout << "查看系统激活状态"<<endl;
		system ("slmgr.vbs -dlv");
		cout << "请输入指令: ";
	}
	if(input == "calc")
	{
		cout << "打开计算器"<<endl;
		system ("calc");
		cout << "请输入指令: ";
	}
	if(input == string("exit")) {
			cout << "感谢您的使用..." << endl;
			cout << "未经作者允许, 禁止转载"<<endl;
			cout << "2019 - 2022 Jerry. All Rights Reserved."<<endl;
			break; 
		}
	if(input == "chkdsk")
	{
		cout << "磁盘扫描工具"<<endl;
		cout << "默认扫描C盘"<<endl;
		system ("chkdsk C:");
		cout << "请输入指令: ";
	}
	if(input == "tree")
	{
		cout << "显示树形目录"<<endl;
		cout << "默认为C盘"<<endl;
		system ("tree C:");
		cout << "请输入指令: ";
	}
	if(input == "dir")
	{
		cout << "显示所有文件"<<endl;
		cout << "默认参数为: dir /s /a"<<endl;
		system ("dir /s /a");
		cout << "请输入指令: ";
	}
	if(input == "taskmgr")
	{
		cout << "打开任务管理器"<<endl;
		system ("taskmgr.exe");
		cout << "请输入指令: ";
	}
	if(input == "explorer")
	{
		cout << "打开桌面资源管理器";
		system ("explorer.exe");
		cout << "请输入指令: ";
	}
	if(input == "systeminfo")
	{
		cout << "系统配置信息"<<endl;
		system ("systeminfo");
		cout << "请输入指令: ";
	}
	if(input == "ping")
	{
		cout << "对IP地址发送数据包"<<endl;
		cout << "默认参数: ping 127.0.0.1 -t -l 65500"<<endl;
		system ("ping 127.0.0.1 -t -l 65500");
		cout << "请输入指令: "; 
	}
    }
	return true; 
}

本文章原创,如需转载请备注作者名与原文地址!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要了解一下什么是hook。Hook即为“钩子”,是指截获特定事件的机制。在Windows系统中,有许多事件可以被hook,比如键盘、鼠标、窗口等。对于记事本程序,我们可以hook它的关闭事件,使其在关闭时弹出自定义的弹窗。 以下是实现hook记事本的C++代码: ```cpp #include <Windows.h> HHOOK g_hHook = NULL; LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION && wParam == WM_CLOSE) // 截获关闭事件 { MessageBox(NULL, "Are you sure to close?", "Warning", MB_OKCANCEL | MB_ICONWARNING); } return CallNextHookEx(g_hHook, nCode, wParam, lParam); } int main() { HINSTANCE hInstance = GetModuleHandle(NULL); g_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, hInstance, 0); MSG msg; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } UnhookWindowsHookEx(g_hHook); return 0; } ``` 这段代码通过调用`SetWindowsHookEx`函数注册了一个全局的键盘hook,当截获到键盘事件时,会调用`KeyboardProc`函数进行处理。在`KeyboardProc`函数中,我们判断是否截获到了记事本的关闭事件,如果是,则弹出自定义的弹窗。最后,在程序退出时,我们需要调用`UnhookWindowsHookEx`函数取消hook。 接下来,我们需要将这个hook程序部署在Windows 10上。我们可以将代码编译成exe文件,并将其放在一个独立的文件夹中。接着,我们需要将这个文件夹添加到系统环境变量中,这样就可以在任意目录下执行这个程序了。 具体操作如下: 1. 编译代码,生成exe文件。 2. 创建一个空文件夹,比如命名为“myhook”。 3. 将生成的exe文件放到“myhook”文件夹中。 4. 按Win+R键打开“运行”对话框,输入“sysdm.cpl”并回车,打开“系统属性”窗口。 5. 点击“高级”选项卡,然后点击“环境变量”按钮。 6. 在“系统变量”区域中找到“Path”变量,双击打开编辑窗口。 7. 在编辑窗口的最后面加上“;D:\myhook”(注意:这里的路径需要替换成你自己的路径),然后点击“确定”保存修改。 8. 关闭所有窗口,重新打开一个记事本程序,当你关闭它时,就会看到自定义的弹窗了。 希望这篇文章能帮助你完成hook记事本程序并在Windows 10上部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值