关于ShellExecute

关于ShellExecute

Q: 如何打开一个应用程序?

ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );

ShellExecute(this->m_hWnd,"open","notepad.exe",

    "c://MyLog.log","",SW_SHOW );

正如您所看到的,我并没有传递程序的完整路径。
Q:
如何打开一个同系统程序相关连的文档?

ShellExecute(this->m_hWnd,"open",

    "c://abc.txt","","",SW_SHOW );

Q: 如何打开一个网页?

ShellExecute(this->m_hWnd,"open",

    "http://www.google.com","","", SW_SHOW );

Q: 如何激活相关程序,发送EMAIL

ShellExecute(this->m_hWnd,"open",

    "mailto:nishinapp@yahoo.com","","", SW_SHOW );

Q: 如何用系统打印机打印文档?

ShellExecute(this->m_hWnd,"print",

    "c://abc.txt","","", SW_HIDE);

Q: 如何用系统查找功能来查找指定文件?

ShellExecute(m_hWnd,"find","d://nish",

    NULL,NULL,SW_SHOW);

Q: 如何启动一个程序,直到它运行结束?

SHELLEXECUTEINFO ShExecInfo = {0};

ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;

ShExecInfo.hwnd = NULL;

ShExecInfo.lpVerb = NULL;

ShExecInfo.lpFile = "c://MyProgram.exe";      

ShExecInfo.lpParameters = "";    

ShExecInfo.lpDirectory = NULL;

ShExecInfo.nShow = SW_SHOW;

ShExecInfo.hInstApp = NULL;      

ShellExecuteEx(&ShExecInfo);

WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

或:

PROCESS_INFORMATION ProcessInfo;

STARTUPINFO StartupInfo; //This is an [in] parameter

ZeroMemory(&StartupInfo, sizeof(StartupInfo));

StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field

if(CreateProcess("c://winnt//notepad.exe", NULL,

    NULL,NULL,FALSE,0,NULL,

    NULL,&StartupInfo,&ProcessInfo))

{

    WaitForSingleObject(ProcessInfo.hProcess,INFINITE);

    CloseHandle(ProcessInfo.hThread);

    CloseHandle(ProcessInfo.hProcess);

} 

else

{

    MessageBox("The process could not be started...");

}

Q: 如何显示文件或文件夹的属性?

SHELLEXECUTEINFO ShExecInfo ={0};

ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;

ShExecInfo.hwnd = NULL;

ShExecInfo.lpVerb = "properties";

ShExecInfo.lpFile = "c://"; //can be a file as well

ShExecInfo.lpParameters = "";

ShExecInfo.lpDirectory = NULL;

ShExecInfo.nShow = SW_SHOW;

ShExecInfo.hInstApp = NULL;

ShellExecuteEx(&ShExecInfo);

<think>好的,我现在需要解决用户的问题,即如何使用ShellExecute调用ipmitool进行IPMI管理。首先,我得回顾一下用户提供的引用内容,特别是引用2和3中关于ShellExecute的用法。用户可能已经了解ShellExecute的基本语法,但需要具体到调用ipmitool的场景。 首先,我需要确定ipmitool在系统中的路径。如果ipmitool没有添加到系统环境变量,用户可能需要指定完整路径,比如"C:\Program Files\ipmitool\ipmitool.exe"。然后,用户需要构建参数部分,比如IPMI命令如"power status"或"sensor list"。这需要将参数作为lpParameters传递。 接下来,引用3中提到,ShellExecute可以不带完整路径调用系统程序,但像ipmitool这样的第三方工具可能需要完整路径。此外,处理中文字符或路径时,应使用宽字符版本ShellExecuteW以避免乱码,如引用1所述。 还需要考虑权限问题。如果操作需要管理员权限,可能需要使用ShellExecute的"runas"动词,但根据引用中的信息,ShellExecute本身可能无法直接提升权限,可能需要其他方法,如ShellExecuteEx。 然后,示例代码部分需要展示如何正确调用ShellExecuteW,包括参数传递。例如,调用ipmitool执行power status命令,并处理可能的错误返回值。同时,要提醒用户注意字符串转换,特别是将char*转换为LPCWSTR,可以使用MultiByteToWideChar函数。 最后,生成相关问题需要考虑用户可能的后续疑问,比如错误处理、参数传递、权限提升等,确保覆盖常见的使用场景和问题。</think>### 如何使用ShellExecute调用ipmitool进行IPMI管理 #### 1. **确认ipmitool的路径** - 若`ipmitool.exe`未加入系统环境变量,需指定完整路径(例如:`"C:\Program Files\ipmitool\ipmitool.exe"`)。 - 若已配置环境变量,可直接使用`"ipmitool.exe"`[^2]。 #### 2. **构建ShellExecute参数** - **函数原型**: ```cpp HINSTANCE ShellExecute( HWND hwnd, // 父窗口句柄(可为NULL) LPCTSTR lpOperation, // 操作类型(如"open"、"runas") LPCTSTR lpFile, // 程序路径 LPCTSTR lpParameters, // 命令行参数 LPCTSTR lpDirectory, // 工作目录(可为NULL) INT nShowCmd // 窗口显示方式(如SW_HIDE/SW_SHOW) ); ``` - **关键参数说明**: - `lpOperation`: 使用`"open"`表示执行程序。 - `lpFile`: 指定`ipmitool.exe`的路径。 - `lpParameters`: IPMI命令(如`"power status"`或`"sensor list"`)。 - `nShowCmd`: 若需隐藏控制台窗口,使用`SW_HIDE`[^2][^3]。 #### 3. **示例代码(C++)** ```cpp #include <windows.h> int main() { // 调用ipmitool查询服务器电源状态 HINSTANCE result = ShellExecuteW( NULL, // 无父窗口 L"open", // 操作类型 L"C:\\ipmitool\\ipmitool.exe", // ipmitool路径 L"-H 192.168.1.100 -U admin -P password power status", // 参数 NULL, // 默认工作目录 SW_SHOW // 显示控制台窗口 ); // 检查执行结果 if ((int)result <= 32) { // 错误处理(例如文件未找到/权限不足) } return 0; } ``` #### 4. **注意事项** - **字符编码问题**:使用`ShellExecuteW`(宽字符版本)避免中文字符乱码,参数需用`L"..."`包裹[^1]。 - **权限要求**:若操作需管理员权限,需改用`ShellExecuteEx`并设置`lpVerb = L"runas"`[^3]。 - **参数格式**:复杂参数需用引号包裹(例如`"sensor list"`)。 #### 5. **常见IPMI命令参考** | 功能 | 命令示例 | |-----------------|------------------------------------------| | 电源状态查询 | `power status` | | 开机 | `power on` | | 温度传感器读取 | `sensor list` | | 系统日志导出 | `sel list -o output.txt` |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值