C Windows.h如何在Shellexecute中使用变量

C++ windows.h How to use variable in ShellExecute

本文关键字:变量 Shellexecute Windows      更新时间:2023-10-16

我想问如何在 ShellExecute

内使用变量

在我的情况下,我想在文件中添加路径。

#include <windows.h>
#include <iostream>
int main()
{
std::string path={"C:UsersMeCLionProjectsstoragecmake-build-debugbookshop.txt"};
ShellExecute(NULL,"edit","path",NULL,NULL, SW_SHOWNORMAL);
return 0;
}

我已经尝试了c_str(),但这无济于事。它正在编译没有错误,但TXT文件不反对。有线索吗?

您需要在字符串字面上逃脱后斜切,然后摆脱牙套:

std::string path = "C:\Users\Me\CLionProjects\storage\cmake-build-debug\bookshop.txt";

然后,您需要使用std::string::c_str()方法将字符串传递给ShellExecute()。而且由于您使用的是std::string,因此应使用基于charShellExecuteA()而不是基于TCHARShellExecute()

ShellExecuteA(NULL, "edit", path.c_str(), NULL, NULL, SW_SHOWNORMAL);