aaa.exe
并传入字符串参数。
一、启动 aaa.exe 并传递参数:
#include <windows.h>
#include <tchar.h>
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// 要传递给 aaa.exe 的参数
LPCTSTR param = _T("hello world");
// 使用 ShellExecute 启动程序并传参
ShellExecute(NULL, _T("open"), _T("aaa.exe"), param, NULL, SW_SHOWNORMAL);
return 0;
}
上面代码中,ShellExecute
的第四个参数即为传递给目标程序的字符串参数。可以是一个,也可以是多个参数(用空格分隔)。
二、aaa.exe 中如何获取传入的参数:
#include <windows.h>
#include <tchar.h>
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// lpCmdLine 就是 ShellExecute 传来的参数
MessageBox(NULL, lpCmdLine, _T("Received Parameters"), MB_OK);
return 0;
}
在被调用的程序中,lpCmdLine
参数即为 ShellExecute 所传入的命令行字符串。开发者可以自行解析该字符串,以实现功能上的自定义控制。
通过本教程,可以快速掌握在 Windows 环境下如何通过 ShellExecute 实现程序间参数传递。该方法适用于启动本地辅助程序、插件工具、脚本封装器等场景,是 Windows 原生 API 中非常实用的一种调用方式。
Copyright © 2021 - 2031 liandianshu.com All Rights Reserved.