![](/img/trans.png)
[英]Edit and Continue doesn't work (C++, Visual Studio Community 2015)
[英]ShellExecute in C++ on VSE (visual studio enterprise) 2015 doesn't works
当我尝试运行在Visual Studio上用C ++编写的代码时,ShellExecute不会向我显示ipconfig,也不会启动chrome。 我没有任何警告或错误。问题与ShallExecute调用的cmd有关,它开始但立即停止,我在VS中遇到了同样的问题,但我添加了stdafx.h
标头.Chrome也没有启动。 我在文件上编写了相同的代码,然后在mingw上运行了由g ++编译的文件,并且可以正常工作。 这是Visual Studio上的代码:
VS项目
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <shellapi.h>
#include <string>
#include <stdlib.h>
#ifdef _WIN32
#define WINPAUSE system("pause")
#endif
using namespace std;
int main() {
cout << "Hello World!" << endl;
for (int i = 0; i<10; i++) {
if (i % 2 == 0)
cout << "\t" << i << "\t dispari!" << endl;
else
cout << "\t" << i << "\t dispari!!!" << endl;
}
ShellExecute(NULL, _T("open"), _T("cmd"), _T(" /C ipconfig"), _T(" C:\ "), SW_SHOW);
cout << "\nLancio google.com" << endl;
string google = "https://www.google.it/search?as_q=";
string ricerca = "";
cout << "Inserisci la tua ricerca su google :" << endl;
getline(cin, ricerca);
google += ricerca;
ShellExecute(0, 0,(LPCWSTR)google.c_str(), 0, 0, SW_SHOW);
cout << "Tutto andato a buon fine" << endl;
return 0;
}
这是文件上的代码(我没有添加ipconfig的一部分)
第一.cpp
#include <iostream>
#include <windows.h>
#include <shellapi.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
cout << "Hello World!" << endl;
for (int i = 0; i<10; i++) {
if (i % 2 == 0)
cout << "\t" << i << "\t dispari!" << endl;
else
cout << "\t" << i << "\t dispari!!!" << endl;
}
cout << "\nLancio google.com" << endl;
string google = "https://www.google.it/search?as_q=";
string ricerca = "";
cout << "Inserisci la tua ricerca su google :" << endl;
getline(cin, ricerca);
google += ricerca;
ShellExecute(0, 0,(LPCSTR)google.c_str(), 0, 0, SW_SHOW);
cout << "Tutto andato a buon fine" << endl;
return 0;
}
有任何问题或不清楚的概念问我。
更新:这是一个解决方案 ,似乎有效
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int main() {
cout << "Hello World!" << endl;
ShellExecute(0, 0, L"cmd.exe", L"/k help", 0, SW_SHOW);
cout << "\nLancio google.com" << endl;
wstring google = L"https://www.google.it/search?as_q=";
wstring ricerca = L"";
cout << "Inserisci la tua ricerca su google :" << endl;
wcin >> ricerca;
google += ricerca;
LPCWSTR googlata = google.c_str();
ShellExecute(NULL , L"open", L"chrome.exe", googlata, 0, SW_SHOWDEFAULT);
return 0;
system("pause");
}
好,有几个问题。
首先,在Windows Vista和Windows 7中, ipconfig
需要特权提升。 要做到这一点通过ShellExecute
可以使用runas
动词。 然后,如果启用了UAC(用户访问控制),则会出现一个愚蠢的警告框,要求您确认您确实要运行危险的ipconfig
程序。
也就是说,如果您直接运行而不是通过cmd
运行它,因为当前代码会不必要地尝试。
但是,第三,直接运行控制台程序的结果通常是一个控制台窗口,该控制台窗口在控制台程序完成时会弹出并消失。 为此,涉及cmd
的工作可以完成。 只需对cmd
使用/k
选项,它告诉它在执行指定命令后保留 。
第四,该代码中包含许多Microsoft _T
内容。 自2000年以来已经过时了。对于现代Windows而言,只需使用宽字符串,例如L"Hello!"
。
第五,C铸入
(LPCWSTR)google.c_str()
千万不要那样做 。
您之所以告诉编译器关闭,是因为您知道的更多。 但是你绝对不会。 google
是一个简单的std::string
,您正在转换为宽字符串wchar_t const*
。 那永远都行不通。
同样,在现代Windows中,只需使用宽字符文本。 这意味着像L"Hello!"
这样的宽文字L"Hello!"
以及诸如wstring
类的宽字符串。 在包含<windows.h>
之前定义宏符号UNICODE
(显然,它是在VS项目中定义的,因为带有强制类型转换的调用可以编译,但仍然可以)。
在其他消息中,与代码的正确性无关,而仅与建议有关:
对于Windows特定程序#ifdef _WIN32
没有意义: _WIN32
始终被定义,在64位Windows中也是如此。 因此,您可以简化它。
"stdafx.h"
标头是Visual Studio用于预编译标头的约定。 Visual C ++预先编译的标头更改了预处理语义,尤其是必须在每个翻译单元中首先包含该标头,这使许多初学者感到头疼。 我强烈建议您仅在项目设置中关闭预编译的头文件,然后删除包含文件(在更大的项目中,如果预编译的头文件减少了构建时间以承受非标准规则的代价,则值得检查)。
代替声明被修改的变量,您通常可以声明在表达式中组合的const
。 这样可以更轻松地理解或证明代码,因为可以更改的内容更少。 只要在可能的任何地方都慷慨地洒const
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.