簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM