C++ ShellExecute a URL - c++

Tried googling for a few hours, testing different solutions for hours but still just cannot get this to work.
I need a const url base (Ex. http://www.google.com)
Then I need a string input from the user (ex. Mountain Dew) and then combine them.
I've tried making the URL a LPCWSTR, wstring, wchar_t, doing a function to convert them and combine them but I cannot get it to work at all.
std::string baseUrl = "http://www.google.com/";
std::string userAdd;
getline(std::cin, userAdd)
ShellExecute(NULL, TEXT("open"), baseUrl + userAdd, NULL, NULL, SW_SHOWNORMAL);

There's no automatic conversion from std::string to const char*.
Try this: (baseUrl + userAdd).c_str()
and try using ShellExecuteA

Related

changing wallpaper c++ ive tried multiple ways

string s = "C:\\ok.bmp";
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void*)s.c_str(), SPIF_SENDCHANGE);
This doesnt work ^^ and the ok.bmp is inside of the folder!
Most builds are Unicode nowadays, so try with a Unicode string:
wstring ws = L"C:\\ok.bmp";
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, ws.data(), SPIF_SENDCHANGE);
(by using std::wstring::data(), you don't need the cast).

How do I get username and appname for file path

How am I able to define a path like "C:/Users/<USER>/AppData/Local/<APPNAME>", for different username and app? How do I set this to automatically get the user and the appname? Thank you.
You can use SHGetKnownFolderPath to get the full path of App Local:
...
#include <KnownFolders.h>
#include <ShlObj.h>
...
SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_SIMPLE_IDLIST, NULL, &path); // NULL for current user
...
To get the Local AppData path for a given user, use SHGetFolderPath() specifying CSIDL_LOCAL_APPDATA, or SHGetKnownFolderPath() specifying FOLDERID_LocalAppData. Both take an optional user token for the desired user account to query. If you don't provide a token, the user associated with the calling thread is used.
To get the username:
char username[MAX_PATH];
DWORD size = MAX_PATH;
GetUserName(username,&size);
To get the appname(Executable File Name without ".exe"):
char appname[MAX_PATH];
char buffer[MAX_PATH];
GetModuleFileName(NULL, appname,MAX_PATH); //get the string: "PATH\\appname.exe"
char *szExe = NULL;
//Remove prefix
GetFullPathName(appname, MAX_PATH, buffer, &szExe);
//Remove suffix
strncpy_s(appname, szExe, strlen(szExe) - strlen(".exe"));

How can I download a file in c++ with parameters not using libcurl

so I have a question on how to download a file off my site using parameters in c++ example: "test.com/test.php?user=name&token=sgdashg"
I can't seem to figure out how I tried UrlDownloadFileA just got the main path without the parameters.
URLDownloadToFile(NULL, "localhost:8080/test.php?", "username=" + UserNameBuffer, "&token=" TokentBuf, "C:\\", 0, NULL);
You need to build the full URL as a string before you pass it to the second argument of the function.
So something like this:
std::string url = "localhost:8080/test.php?username=";
url = url + UserNameBuffer + "&token=" + TokentBuf;
URLDownloadToFile(NULL, url.c_str() , "C:\\test.txt", 0, NULL);
The concept of HTTP parameters has nothing to do with the concept of function parameters for the URLDownloadToFile function.

ShellExecute (and ShellExecuteEx) do nothing with my URL

I'm creating a project with C++Builder XE7, in which a user can click on a button to open a web link, e.g. to open a support page, or to share his experience on a social media. For that, I use the ShellExecute() function, and it works well, except for one button.
When I click on this button, simply nothing happens. The ShellExecute() function returns without error (the returned value is 42), but my default browser does not open, and the web page isn't shown at all.
Here is my ShellExecute() implementation
const HINSTANCE result = ::ShellExecute(handle, "open", url.c_str(), NULL, NULL, SW_SHOWDEFAULT);
I also tried the ShellExecuteEx() function:
::SHELLEXECUTEINFO info;
std::memset(&info, 0, sizeof(info));
info.cbSize = sizeof(info);
info.hwnd = handle;
info.lpVerb = "open";
info.lpFile = url.c_str();
info.nShow = SW_SHOWDEFAULT;
if (!::ShellExecuteEx(&info))
The url parameter contains the website link I am trying to open. For security reasons, I cannot post it here as a sample, however I tested it in my browser (FireFox) and it works well. On the other hand, if I execute my code by just replacing the url content with Google's website, all works as expected.
The handle is just the Handle parameter of the parent frame.
I also tried to tweak the ShellExecute/Ex() parameters, like the hwnd and nShow fields, but no change.
Can anybody point me to what is wrong?
The url is formatted like this: http:// www.
mysite.xxx/selectPage.php?arg1=xxx&arg2=yyy&...&argX=a text containing
"quotes" and some %26amp%3b special chars!
In this example you have to URL-encode the double quotes and the spaces because these are not valid characters for the query part of an URL.
Fix it by replacing double quotes with %22 and spaces with %20. I suggest to use a function like UrlEscape() to properly encode the URL.
Although most browsers have an error tolerance for user input and will also accept an URL which does not have valid encoding, it's better to strictly follow the spec because you are not guaranteed that tolerance.
In addition, the double quotes will make problems when the URL is passed as a command-line argument to the command associated with the URL protocol. This is because double quotes are reserved characters for defining command-line parameters that contain spaces.
For instance, on my machine the http protocol is associated with the following command (see HKEY_CLASSES_ROOT\http\shell\open\command):
"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -osint -url "%1"
You can see that the double quotes are already used to enclose the last parameter, which obviously gets messed up if you have unencoded double quotes within the URL.
Example for correctly encoding an URL for use with ShellExecuteEx():
#include <windows.h>
#include <Shellapi.h>
int main()
{
::CoInitialize(nullptr);
SHELLEXECUTEINFOW info{ sizeof(info) };
info.fMask = SEE_MASK_NOASYNC; // because we exit process after ShellExecuteEx()
info.lpVerb = L"open";
info.lpFile = L"http://www.google.de/search?q=ShellExecuteEx%20URL%20%22double%20quotes%22";
info.nShow = SW_SHOWDEFAULT;
if( !::ShellExecuteExW( &info ) )
{
DWORD err = ::GetLastError();
printf("ShellExecuteEx failed with error %d\n", err );
}
::CoUninitialize();
}

unable to print string value using sprintf in c++

i have function that takes two parameters of type LPTSTR, and i am trying print both the values using sprintf like below and i am not able to print the exact values.
int __stdcall Logon(LPTSTR UserName, LPTSTR Password)
{
char Buffer[2048];
sprintf(Buffer,"UserName: %s\n m_Password: %s\n",UserName,Password);
FILE *Ls=fopen("lo.log",a);
fprintf(Ls,Buffer);
fclose(Ls);
}
Either fix "Use Unicode strings" in you project settings or use the
_stprintf(Buffer, _T("UserName: %S\n m_Password: %S\n"), UserName, Password);
and
#include <tchar.h>
If you use Unicode (and you do), use the '%S' format.
Assuming that UserName and Password are stored as wide strings (wchar_t) and you are trying to put them into a char[] buffer, then visual c++ uses %S to accomplish this.
sprintf(Buffer,"UserName: %S\n m_Password: %S\n",UserName,Password);