Reputation: 1352
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. 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. I write the same code on a file and I run it, compiled by g++, on mingw, and it works. So this is the code on visual studio :
VS PROJECT
#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)
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;
}
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");
}
Upvotes: 1
Views: 2194
Reputation: 145419
Well there are several issues.
First, in Windows Vista and Windows 7 ipconfig
required privilege elevation. To do that via ShellExecute
you can use the runas
verb. 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.
That is, if you run it directly, and not via cmd
, as your current code needlessly attempts.
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. Just use the /k
option to cmd
, which tells it to keep after executing the specified command.
Fourth, there's a lot of Microsoft _T
stuff in that code. It's been obsolete since the year 2000. For modern Windows just use wide character strings, e.g. L"Hello!"
.
Fifth, the C cast in
(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*
. That will never work.
Again, in modern Windows just use wide character text. That means wide literals like L"Hello!"
, and wide strings like 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).
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. So you can simplify that.
The "stdafx.h"
header is a convention used by Visual Studio for a precompiled header. 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. 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. 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.
Upvotes: 1