Visual StudioでC++で書かれたコードを実行しようとしているとき、ShellExecuteはipconfigを表示せず、chromeも起動しません。私は警告もエラーもありません。ShallExecuteによって呼び出されたcmdに関する問題は始まりますが、immediatlyで止まりますが、VSと同じ問題がありましたが、stdafx.h
ヘッダーを追加しました。私はファイルに同じコードを書いて、それをg ++でコンパイルして実行し、mingwで動作します。だから、これは、Visual Studioのコードです:これは、ファイル上のコード(私はIPCONFIGの一部を追加いけないが)であるVSE(ビジュアルスタジオエンタープライズ)のC++でShellExecuteが動作しない
#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;
}
VSプロジェクトは
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;
}
質問や不明な点についてはお問い合わせください。
UPDATE:ここソリューション、
#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");
}
[MSDN] [1]のShellExecuteには、CoInitializeExを使用する必要があることがあります。多分これが助けになるでしょうか? [1]:https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx –