Когда я пытаюсь запустить свой код, выписанный в C++ на визуальной студии, ShellExecute не показывает мне ipconfig, а также не запускает хром , У меня нет никаких предупреждений или ошибок. Проблема в том, что cmd вызван ShallExecute, он запускается, но он останавливается немедленно, у меня была такая же проблема с VS, но я добавил заголовок stdafx.h
. Также Chrome не запускается. Я пишу тот же код в файле, и я запускаю его, скомпилированный g ++, на mingw, и он работает. Так что это код на визуальной студии:ShellExecute в C++ на VSE (предприятие Visual Studio) 2015 не работает
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)
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");
}
Для ShellExecute в [MSDN] [1] есть замечание о необходимости использования CoInitializeEx. Может быть, это поможет? [1]: https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx –