8000 ShellExecute · Issue #226 · holdyounger/ScopeBlog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
ShellExecute #226
Open
Open
@holdyounger

Description

@holdyounger

ShellExecute

> 概述: ShellExecute 用法的整理和示例
> 官方说明:shellExecuteA 函数 (shellapi.h) - Win32 apps | Microsoft Learn

一个完整的用法示例

以下示例为一个完整的用法展示,实现了调用第三方工具 devocn.exe 安装虚拟网卡(不展示安装界面)并通过获取安装进程句柄判断安装结果。

  • SEE_MASK_NOCLOSEPROCESS 参数:表示要获取进程句柄
  • 通过 GetExitCodeProcess 函数获取进程执行状态
  • SW_HIDE 参数: 不展示窗口
#define QREINSTALL_NETCARD_TOOLS_NAME   L"devcon.exe"
std::mutex g_mutex_nc_install;

bool InstallNcDrive(bool &bRes, bool &bEnd)
	{
	    std::lock_guard<std::mutex> lock(g_mutex_nc_install);
	
		bool bOptRt = false;
		//触发重新安装虚拟网卡
		TCHAR szPath[MAX_PATH];
		::GetModuleFileName(NULL, szPath, MAX_PATH);
		::PathRemoveFileSpec(szPath);
	
		QLOG_INFO() << "tools[devcon.exe]'s path is" << QString::fromWCharArray(szPath);
	
		std::string command = " install vpnvnic.inf vpnvnic";

		SHELLEXECUTEINFO  ShExecInfoInstall = { 0 };
	
		try
		{
			ShExecInfoInstall.cbSize = sizeof(SHELLEXECUTEINFO);
			ShExecInfoInstall.fMask = SEE_MASK_NOCLOSEPROCESS;
			ShExecInfoInstall.hwnd = NULL;
			if (!isProcessAdmin())
			{
				ShExecInfoInstall.lpVerb = L"runas";
			}
			else
			{
				ShExecInfoInstall.lpVerb = L"open";
			}
			ShExecInfoInstall.lpFile = QREINSTALL_NETCARD_TOOLS_NAME;
			ShExecInfoInstall.lpParameters = STDString2LPCWCH(command);
			ShExecInfoInstall.lpDirectory = szPath;
			ShExecInfoInstall.nShow = SW_HIDE;
			ShExecInfoInstall.hInstApp = NULL;
			ShExecInfoInstall.hProcess = NULL;

			bOptRt = ShellExecuteEx(&ShExecInfoInstall);

			if (bOptRt)
			{
				QLOG_INFO() << "驱动安装进程已启动!";

				// 等待进程结束
				WaitForSingleObject(ShExecInfoInstall.hProcess, INFINITE);

				// 获取退出代码
				DWORD exitCode;
				if (GetExitCodeProcess(ShExecInfoInstall.hProcess, &exitCode))
				{
					// 判断进程是否成功退出
					if (exitCode != STILL_ACTIVE)
					{
						QLOG_INFO() << "驱动安装进程已退出,退出代码: " << exitCode;
					}
					else
					{
						QLOG_INFO() << "驱动安装进程未能正常退出,退出代码: " << exitCode;
					}
				} 
				else
				{
					QLOG_INFO() << "获取驱动安装进程退出代码失败!错误代码: " << GetLastError();
				}

				// 实测 1和0 的时候安装成功
				bRes = exitCode == 1 || exitCode == 0 ? true : false;

				// 关闭句柄
				CloseHandle(ShExecInfoInstall.hProcess);
			}
			else
			{
				QLOG_ERROR() << "启动驱动安装进程失败!错误代码: " << GetLastError();
			}
		}
		catch (const std::exception& e)
		{
			QLOG_ERROR() << __FUNCTION__ << __LINE__ << e.what();

			bEnd = true;
			return bOptRt;
		}

		bEnd = true;
		return bOptRt;
}

blog link ShellExecute

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0