简体   繁体   English

VSE(Visual Studio Enterprise)2015上的C ++ ShellExecute不起作用

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

When i'm trying to run my code, writed in c++ on visual studio, ShellExecute doesen't show me the ipconfig and also doesn't start chrome. 当我尝试运行在Visual Studio上用C ++编写的代码时,ShellExecute不会向我显示ipconfig,也不会启动chrome。 I have not any warning or error.The problem it's about the cmd called by ShallExecute, it starts but it stop immediatly, I had the same problem with VS but i added the stdafx.h header.Also Chrome neither starts. 我没有任何警告或错误。问题与ShallExecute调用的cmd有关,它开始但立即停止,我在VS中遇到了同样的问题,但我添加了stdafx.h标头.Chrome也没有启动。 I write the same code on a file and I run it, compiled by g++, on mingw, and it works. 我在文件上编写了相同的代码,然后在mingw上运行了由g ++编译的文件,并且可以正常工作。 So this is the code on visual studio : 这是Visual Studio上的代码:

VS PROJECT 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;
}

This is the code on the file(I dont add the part of ipconfig) 这是文件上的代码(我没有添加ipconfig的一部分)

first.cpp 第一.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;
}

For any question or uncleard concept ask me . 有任何问题或不清楚的概念问我。

UPDATE : Here a solution , it seems to work 更新:这是一个解决方案 ,似乎有效

#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");
}

Well there are several issues. 好,有几个问题。

First, in Windows Vista and Windows 7 ipconfig required privilege elevation. 首先,在Windows Vista和Windows 7中, ipconfig需要特权提升。 To do that via ShellExecute you can use the runas verb. 要做到这一点通过ShellExecute可以使用runas动词。 Then if UAC, User Access Control, is enabled, you'll get a silly warning box asking you to confirm that you really want to run the dangerous ipconfig program. 然后,如果启用了UAC(用户访问控制),则会出现一个愚蠢的警告框,要求您确认您确实要运行危险的ipconfig程序。

That is, if you run it directly, and not via cmd , as your current code needlessly attempts. 也就是说,如果您直接运行而不是通过cmd运行它,因为当前代码会不必要地尝试。

But, third, the result of running a console program directly is usually a console window that pops up and disappears when the console program finishes. 但是,第三,直接运行控制台程序的结果通常是一个控制台窗口,该控制台窗口在控制台程序完成时会弹出并消失。 And for this involving cmd can do the job. 为此,涉及cmd的工作可以完成。 Just use the /k option to cmd , which tells it to keep after executing the specified command. 只需对cmd使用/k选项,它告诉它在执行指定命令后保留

Fourth, there's a lot of Microsoft _T stuff in that code. 第四,该代码中包含许多Microsoft _T内容。 It's been obsolete since the year 2000. For modern Windows just use wide character strings, eg L"Hello!" 自2000年以来已经过时了。对于现代Windows而言,只需使用宽字符串,例如L"Hello!" .

Fifth, the C cast in 第五,C铸入

(LPCWSTR)google.c_str()

Never do that . 千万不要那样做

You're telling the compiler to shut up because, you're saying, you know better. 您之所以告诉编译器关闭,是因为您知道的更多。 But you absolutely don't. 但是你绝对不会。 google is a simple std::string , and you're casting to wide character string, wchar_t const* . google是一个简单的std::string ,您正在转换为宽字符串wchar_t const* That will never work. 那永远都行不通。

Again, in modern Windows just use wide character text. 同样,在现代Windows中,只需使用宽字符文本。 That means wide literals like L"Hello!" 这意味着像L"Hello!"这样的宽文字L"Hello!" , and wide strings like wstring . 以及诸如wstring类的宽字符串。 Define the macro symbol UNICODE before including <windows.h> (apparently it is defined in your VS project, since the call with the cast in it compiles, but still). 在包含<windows.h>之前定义宏符号UNICODE (显然,它是在VS项目中定义的,因为带有强制类型转换的调用可以编译,但仍然可以)。


In other news, not about the code's correctness, but just advice: 在其他消息中,与代码的正确性无关,而仅与建议有关:

  • For a Windows-specific program #ifdef _WIN32 doesn't make sense: _WIN32 is always defined, also in 64-bit Windows. 对于Windows特定程序#ifdef _WIN32没有意义: _WIN32始终被定义,在64位Windows中也是如此。 So you can simplify that. 因此,您可以简化它。

  • The "stdafx.h" header is a convention used by Visual Studio for a precompiled header. "stdafx.h"标头是Visual Studio用于预编译标头的约定。 Visual C++ precompiled headers change the preprocessing semantics, in particular that that header must be included first of all in every translation unit, which has tripped up many beginners. Visual C ++预先编译的标头更改了预处理语义,尤其是必须在每个翻译单元中首先包含该标头,这使许多初学者感到头疼。 I strongly advice you to just turn off precompiled headers in the project settings, and remove the include (in a much larger project it can be worth checking if precompiled headers reduce build times enough to take the penalty of non-standard rules). 我强烈建议您仅在项目设置中关闭预编译的头文件,然后删除包含文件(在更大的项目中,如果预编译的头文件减少了构建时间以承受非标准规则的代价,则值得检查)。

  • Instead of declaring variables that are modified you can often declare const -ants that are combined in expressions. 代替声明被修改的变量,您通常可以声明在表达式中组合的const That makes it easier to understand or prove the code, because there's less that can change. 这样可以更轻松地理解或证明代码,因为可以更改的内容更少。 Just sprinkle const generously just about everywhere you can. 只要在可能的任何地方都慷慨地洒const

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM