c++ - 如何检查用户是否打开了应用程序
问题描述
我创建了这段代码,它允许用户打开诸如记事本++、excel等文件。我想检查他是否打开了一个存在的文件。如果不只是给出错误消息。如果它是文件打印“它可以工作”或类似的东西。
您给我的答案完全不相关,是关于其他功能不同的东西,太笼统了。
我试图检查文件是否为真,但它不起作用。
if (qorn == "notreal" || qorn == "4") {
if (fileopen("ex") == true) { // all ways true ex is not a real file
cout << "The file is good";
}
else {
cout << "you don't have this file";
}
}
bool fileopen(string url) {
bool f = false;
// .exe etc
// what to do with the file - open is default
// what is the name of the file you want to do something with
ShellExecuteA(NULL,NULL/*null- open it as a deffualt*/, url.c_str(), NULL, NULL, SW_SHOWNORMAL);
if (ShellExecuteA(NULL, NULL, url.c_str(), NULL, NULL, SW_SHOWNORMAL)) {
f = true;
}
return f;
}
> You adviced me to this this as well which returns 000002 at the end if
> i print it
HINSTANCE fileopen(string url) {
HINSTANCE num = 0;
// .exe etc
// what to do with the file - open is deffualt
// what is the name of the file you want to do wsomething with
num =ShellExecuteA(NULL,NULL/*null- open it as a deffualt*/, url.c_str(), NULL, NULL, SW_SHOWNORMAL);
return num;
}
解决方案
正如文档所说,将返回值与 32 进行比较,否则显示错误消息。
(并且您仅标记了 C++,但在您使用ShellExecute时它也是 Windows/Winapi
像这样(在 Windows 上总是 Unicode)=>
(如果你用一个不存在的文件替换 Excel,它会返回 2 (File Not Found))
int hInst = (int)ShellExecute(NULL, NULL, L"Excel", NULL, NULL, SW_SHOWNORMAL);
if (hInst <= 32)
{
LPVOID lpMsgBuf = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hInst /* = GetLastError() */, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL);
if (lpMsgBuf != NULL)
{
MessageBox(NULL, (LPCTSTR)lpMsgBuf, L"ShellExecute Error", MB_OK | MB_ICONSTOP);
LocalFree(lpMsgBuf);
}
}
推荐阅读
- java - 使用 YouTube Data API v3 确定 YouTube 频道的上传速率
- python - 值错误:x、y 和格式字符串不能为 None
- visual-studio - Visual Studio 灯泡不再显示“修复格式”
- android - 使用 CollapsingToolbarLayout 在 CoordinatorLayout 中的当前滚动位置
- java - groovy MissingMethodException:没有方法签名:java.lang.String.getFileSystem()
- html - SVG
- python - click() 确实执行了一次点击,但以错误结束
- scala - 在 Scala/Spark 中获取键/值对的键
- python - pyodbc 中的用户密码与 pyinstaller 无关?
- python - 如何根据熊猫中的特定列值识别数据框中的“子”数据框