VSE(visual studio 企业)2015 上的 C++ 中的 ShellExecute 不起作用

ShellExecute in C++ on VSE (visual studio enterprise) 2015 doesn't works

当我尝试 运行 我的代码时,在 visual studio 上用 c++ 编写,ShellExecute 没有向我显示 ipconfig,也没有启动 chrome。我没有任何警告或 error.The 问题,它与 ShallExecute 调用的 cmd 有关,它启动但立即停止,我在 VS 中遇到了同样的问题,但我添加了 stdafx.h header.Also Chrome 既不启动。我在一个文件上写了相同的代码,我 运行 它,由 g++ 编译,在 mingw 上,它可以工作。所以这是 visual studio 上的代码:

对比项目[​​=30=]

#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 的部分)

first.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,因为您当前的代码是不必要的尝试。

但是,第三,直接 运行 运行控制台程序的结果通常是控制台程序 window 弹出并在控制台程序完成时消失。为此,涉及 cmd 可以完成这项工作。只需使用 cmd/k 选项,它告诉它在执行指定命令后 保持

第四,该代码中有很多 Microsoft _T 的东西。它自 2000 年以来就已过时。对于现代 Windows 只需使用宽字符串,例如L"Hello!".

五、C cast in

(LPCWSTR)google.c_str()

永远不要那样做.

你是在告诉编译器闭嘴,因为你是在说,你知道得更多。但你绝对不会。 google 是一个简单的 std::string,您正在转换为宽字符串 wchar_t const*。那永远行不通。

同样,在现代 Windows 中只需使用宽字符文本。这意味着像 L"Hello!" 这样的宽文字和像 wstring 这样的宽字符串。在包含 <windows.h> 之前定义宏符号 UNICODE(显然它是在您的 VS 项目中定义的,因为其中包含强制转换的调用会编译,但仍然会编译)。


在其他新闻中,与代码的正确性无关,只是建议:

  • 对于 Windows-specific 程序 #ifdef _WIN32 没有意义:_WIN32 总是被定义,也在 64 位 Windows 中。所以你可以简化它。

  • "stdafx.h" header 是 Visual Studio 用于预编译 header 的约定。 Visual C++ 预编译的 headers 改变了预处理语义,特别是 header 必须首先包含在每个翻译单元中,这让许多初学者感到困惑。我强烈建议你在项目设置中关闭预编译 headers,并删除包含(在一个更大的项目中,值得检查预编译 headers 是否减少构建时间足以采取non-standard 规则的惩罚)。

  • 您通常可以声明组合在表达式中的 const-ants 而不是声明被修改的变量。这使得代码更容易理解或证明,因为可以更改的东西更少。随手洒 const 就可以了。