Hi,
I have my code where I just want to access a websearch automatically with a program. For that I have this code, that apparently works well for other people but not on my laptop.
Can someone let me know, why is that?
Here is the code
Here are the mistakes:
main.cpp:29:1: error: no matching function for call to 'ShellExecuteW'
shellapi.h:63:22: note: expanded from macro 'ShellExecute'
_mingw_unicode.h:12:32: note: expanded from macro '__MINGW_NAME_AW'
2:1: note: expanded from here
shellapi.h:76:24: note: candidate function not viable: no known conversion from 'const char [5]' to 'LPCWSTR' (aka 'const wchar_t *') for 2nd argument
I would like to open the website and then that it automatically closes.
Not possible with ShellExecute() function, I think.
You could use the ShellExecuteEx() function instead, which, in case that a new process was started, gives you a handle to that process, so that you could terminate the newly created process (after a timeout).
...or maybe, less intrusively, send a WM_CLOSE message to its top-level window.
But: Be aware that neither ShellExecute() nor ShellExecuteEx() is guaranteed to start a new process. It could open the web-site in an already running browser instance, in which case no new process is started.
A handle to the newly started application. This member is set on return and is always NULL unless fMask is set to SEE_MASK_NOCLOSEPROCESS. Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. For example, if a document to be launched is a URL and an instance of Internet Explorer is already running, it will display the document. No new process is launched, and hProcess will be NULL.
These are the mistakes I get:
main.cpp:21:1: error: no matching function for call to 'ShellExecuteExA'
shellapi.h:347:22: note: candidate function not viable: requires single argument 'pExecInfo', but 6 arguments were provided
If I solve that, I would just need some type of timer between open and close commands and program would be finished.
My browser just actualized kigar, I will answer you later and read the documentation.
I just read the documentation... I do not know how to implement it though...
I am actually on university just learning how to use templates. This code I copied from a youtube video.
I am actually willing to pay if someone does actually help me with this program. I do not care if the browser opens and then closes or if new tabs open and then close.
The search history of my internet browser should always have the websites I would command on the program.
Hope someone helps or tells me who should I contact for the person to write me that program or to tell me how to write it (of course I would pay if it is too long although I thought it should have just a couple of lines).
#define _WIN32_WINNT 0x0500
#include <Windows.h>
#include <shellapi.h>
int main()
{
SHELLEXECUTEINFOW sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFOW));
sei.cbSize = sizeof(SHELLEXECUTEINFOW);
sei.lpVerb = L"open";
sei.lpFile = L"http://www.example.com/";
sei.nShow = SW_SHOWNORMAL;
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
if (!ShellExecuteExW(&sei))
{
MessageBoxW(NULL, L"Whoops, something went wrong!", L"Err0r", MB_ICONERROR);
return 1;
}
if (sei.hProcess != NULL)
{
/* Wait for the process to exit, or terminate it, if it still hasn't exited after 5 sec */
if (WaitForSingleObject(sei.hProcess, 5000U) == WAIT_TIMEOUT)
{
TerminateProcess(sei.hProcess, 666);
}
CloseHandle(sei.hProcess); /* don't forget to close the handle! */
}
else
{
/* ShellExecuteExW succeeded but *no* new process was created! */
/* probably an existing browser instance was re-used */
}
return 0;
}
You edited it, that code is different from what you posted before.
Yes. I thought it makes sense to use the WaitForSingleObject() function to wait for the process, so if the process happens to terminate before the timeout (for whatever reason), then we don't continue to wait pointlessly. Also, this way, we don't try to kill the process, if it already has terminated by itself.
// C++ program to open a URL in browser.
// This program is written on for Microsoft
// Windows OS
#define _WIN32_WINNT 0x0500
#include <Windows.h>
#include <shellapi.h>
int main()
{
SHELLEXECUTEINFOW sei;
memset(&sei, 0, sizeof(SHELLEXECUTEINFOW));
sei.cbSize = sizeof(SHELLEXECUTEINFOW);
sei.lpVerb = L"open";
sei.lpFile = L"https://de.search.yahoo.com/search?ei=UTF-8&fr=crmas&p=Daniel+Aar%C3%B3n+C%C3%A1rdenas+Mu%C3%B1oz+es+Dios";
sei.nShow = SW_SHOWNORMAL;
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
if (!ShellExecuteExW(&sei))
{
MessageBoxW(NULL, L"Whoops, something went wrong!", L"Err0r", MB_ICONERROR);
return 1;
}
if (sei.hProcess != NULL)
{
if (WaitForSingleObject(sei.hProcess, 5000U) == WAIT_TIMEOUT)
{
TerminateProcess(sei.hProcess, 666);
}
CloseHandle(sei.hProcess);
}
else
{
/* ShellExecuteExW succeeded but *no* new process was created! */
/* probably an existing browser instance was re-used */
}
return 0;
}
The program runs good, but no other window or tab opens after the timer, and the opened tab does not close either.
It closes only if there was no other window before. Otherwise it lets it opened as another tab.
Yes, because that is how ShellExecuteEx() works :-)
The relevant part of the documentation:
Microsoft wrote:
Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. For example, if a document to be launched is a URL and an instance of Internet Explorer is already running, it will display the document. No new process is launched, and hProcess will be NULL.
BTW: This is the same with ShellExecute(), except that ShellExecute() doesn't give you the process handle, even if a new process was launched. So, ShellExecuteEx() is your best bet, I think.
The program runs good, but no other window or tab opens after the timer
If you want another URL to open after the timeout, it should be trivial to do, now that you have the basics.
I am actually on university just learning how to use templates
As an aside: The Win32 API is pure C, not C++. There is no such thing as templates in pure C, only in C++.