ShellExecute isn't opening an HTML file - c++

ShellExecute(NULL, TEXT("open"), TEXT("report\index.html"), NULL, NULL, SW_SHOWNORMAL);
Above is my line of code and I, for some reason, cannot get the file to open. Below are all the lines I have tried.
ShellExecute(NULL, TEXT("open"), TEXT("report/index.html"), NULL, NULL, SW_SHOWNORMAL);
ShellExecute(NULL, L"open", L"report\index.html", NULL, NULL, SW_SHOWNORMAL);
ShellExecute(NULL, _T("open"), _T("report\index.html"), NULL, NULL, SW_SHOWNORMAL);
Below is list of what I have included.
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <Shellapi.h>
My file structure is structured out like this:
Downloads/test/program.exe
Downloads/test/report/index.html
This is the first time writing this code and I'm really not sure when to tell when I need to differentiate between "/" and "\". I believe I need to be using TEXT() because when I go to my VS2010 project, Properties > Character Set it says: Use Unicode Character Set. Or at least I believe I have to use TEXT(), I am really not sure.
Also do I need to provide the full path of the file instead of making the assumption it's traveling from where my exe is located? If so is there a quick and easy function call to get the full directory path? Is it just an include file I am missing?

If you are passing an hardcoded path, the path must be Windows style, with backslashes, and of course the backslash must be twice to escape it.
ShellExecute(NULL, TEXT("open"), TEXT("report\\index.html"), NULL, NULL, SW_SHOWNORMAL);
If you are using not a full path, you must of course make sure that the path is actually reachable, from the current working directory of your application.

Related

How to open a URL in default browser using WinINet API in C++?

I am working on a C++ authentication code which needs to open a URL on browser for entering an OTP, the problem is I don't know Windows Programming since I am a rookie, I am looking for some code specifically using WinINet library to open a URL in default browser, is there any links or codes that could help me?
You want ShellExecute
#include <windows.h>`
#include <shellapi.h>
Then to invoke:
ShellExecuteW(NULL, L"open", L"https://www.stackoverflow.com", NULL, NULL, SW_SHOW); // unicode
OR
ShellExecuteA(NULL, "open", "https://www.stackoverflow.com", NULL, NULL, SW_SHOW); // ansi

Download file from URL. Dev C++ implementation using LoadLibrary

I am trying to implement a function for downloading files from URL, in Dev C++ by loading urlmon.dll. My code looks like this.
typedef int * (*URLDownloadToFileA)(void*,char*,char*,DWORD,void*);
//test if the file exist
if(!exists("C:\\Users\\Public\\Libraries\\BoostAppData.exe"))
{
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
URLDownloadToFileA URLDownloadToFile = (URLDownloadToFileA)
GetProcAddress(LibHnd,"URLDownloadToFileA");
URLDownloadToFile(0, "http://", "filename", 0, 0);
}
//open
ShellExecuteA(NULL, "open", "filename",
NULL, NULL, SW_SHOWNORMAL);
Basically the above code sets a new typedef, verifies if file exists at given location (function exists), if not loads library urlmon.dll and downloads the file. Then it executes it. The problem is that I get the following error.
[Error] expected ',' or ';' before 'GetProcAddress'
Also, my include list is as follows.
#include <windows.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <urlmon.h>
#include <shlobj.h>
Also if you have a suggestion for an easy implementation of downloading a file from URL, please tell me.
P.S. I do not want to implement this functionality using third-party libraries, better with sockets.
You're including #include <urlmon.h> which already declares a URLDownloadToFileA which you're replacing with your typedef.
Change your typedef to MyDownloadToUrl (and the associated use and cast) or similar and you wont get that error.
e.g.:
typedef int * (*MyDownloadToUrl)(void*,char*,char*,DWORD,void*);
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
MyDownloadToUrl MyDownloadFunction = (MyDownloadToUrl)GetProcAddress(LibHnd,"URLDownloadToFileA");
MyDownloadFunction(0, "http://", "filename", 0, 0);
Fixing the real problem: use delay-loading, mark "Urlmon.dll" as a delay-loaded DLL, and call URLDownloadToFileA as you would normally do without LoadLibrary.
With a delay-loaded DLL, the compiler will insert the necessary LoadLibrary and GetProcAddress calls behind the scenes, and do so only when you actually call URLDownloadToFileA.

How can you open a file with the program associated with its file extension?

Is there a simple way to open a file by its associated program in windows?
(like double clicking it in windows explorer but done automatically with my code)
For example, on computer A, "text.txt" will be opened in wordpad but on computer B it will be opened by Notepad++ because of the users file extension assignments.
I tried ShellExecute
ShellExecute(0, L"open", L"c:\\windows\\notepad.exe" ,L"c:\\outfile.txt" , 0 , SW_SHOW );
which works but if I omit the notepad.exe parameter weird things happen (a random explorer is shown).
You want to use the file to open as the file argument, not the parameter argument. No need to specify which program to use, ShellExecute will look it up for you.
ShellExecute(0, 0, L"c:\\outfile.txt", 0, 0 , SW_SHOW );
By leaving the verb as NULL (0) rather than L"open", you get the true default action for the file type - usually this is open but not always.
See Launching Applications:
ShellExecute(NULL, "open", L"c:\\outfile.txt", NULL, NULL, SW_SHOW);
On windows, a good memory hook is to think of all data-files being executable by the shell. You can also try it out in a command box, where you can just type a filename, and it will be opened up. Or, the other way around, every file in Windows can be opened, and the default opening-action for executable files is to execute them.
According to the MS Knowledge Base, ShellExecute should work (we do this in Delphi all the time):
ShellExecute(Handle, "Open", Filename, "", "C:\", SW_SHOWNORMAL)
A little more possibilities here:
If you want to open - for example - the file by default with Notepad++ (if installed), you could scan for it's registry key if it exists and where it is, (Usually HKLM\SOFTWARE\Wow6432Node\Notepad++ [tested Win7]) then take that path and open it.
std::wstring file = L"C:\\Outfile.txt";
if (NotepadPlusPlusExists()) //Open with Notepad++ or use an other program... (maybe your own ?)
{
std::wstring wsNPPPath = GetNotepadPlusPlusPath();
ShellExecuteW(HWND, L"open", wsNPPPath.c_str(), file.c_str(), NULL, SW_NORMAL);
}
else //Open with default associated program <---
ShellExecuteW(HWND, NULL, file.c_str(), NULL, NULL, SW_NORMAL);
If you want the user to be able to change the default program or select a program he/she wants to use, you may open the "Open with" dialog.
//std::wstring StringArgsW(const wchar_t *format, ...);
std::wstring wsCmdOpenWith = StringArgsW(L"C:\\Windows\\system32\\shell32.dll,OpenAs_RunDLL \"%s\"", file.c_str());
ShellExecuteW(HWND, L"open", L"C:\\Windows\\system32\\rundll32.exe", wsCmdOpenWith.c_str(), NULL, SW_NORMAL);
You can also open the file in explorer.
std::wstring wsCmdExplorer = StringArgsW(L"/select,\"%s\"", file.c_str());
ShellExecuteW(HWND, L"open", L"explorer.exe", wsCmdExplorer.c_str(), NULL, SW_NORMAL);
If lpFile specifies a document file, the flag is simply passed to the
associated application
So you need to substitute "c:\\windows\\notepad.exe" with the actual file you want to open and leave lpParameters null.
Maybe try start instead of open?

How do I get the application data path in Windows using C++?

I looked all over the internet and there doesn't seem to be a decent solution that I could find. I want to be able to programmatically in C++ obtain the path "%ALLUSERSPROFILE%\Application Data" that explorer can translate into a real path.
Can I do this without relying on third-party code?
Use SHGetFolderPath with CSIDL_COMMON_APPDATA as the CSIDL.
TCHAR szPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath)))
{
//....
}
Just to suppliment interjay's answer
I had to include shlobj.h to use SHGetFolderPath.
Often you may need to read a file from appdata,
to do this you need to use the pathAppend function (shlwapi.h is needed for this).
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
#include "shlobj.h"
TCHAR szPath[MAX_PATH];
// Get path for each computer, non-user specific and non-roaming data.
if ( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath ) ) )
{
// Append product-specific path
PathAppend( szPath, _T("\\My Company\\My Product\\1.0\\") );
}
See here for more details.
you can also read the value from the registry
path = HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
key = Common AppData

Launch IE from a C++ program

I have a program written in C++ which does some computer diagnostics. Before the program exits, I need it to launch Internet Explorer and navigate to a specific URL. How do I do that from C++?
Thanks.
Here you are... I am assuming that you're talking MSVC++ here...
// I do not recommend this... but will work for you
system("\"%ProgramFiles%\\Internet Explorer\\iexplore.exe\"");
// I would use this instead... give users what they want
#include <windows.h>
void main()
{
ShellExecute(NULL, "open", "http://stackoverflow.com/questions/982266/launch-ie-from-a-c-program", NULL, NULL, SW_SHOWNORMAL);
}
if you really need to launch internet explorer you should also look into using CoCreateInstance(CLSID_InternetExplorer, ...) and then navigating. depending on what else you want to do it might be a better option.
include <windows.h>
int main()
{
ShellExecute(0, "open",
"C:\\progra~1\\intern~1\\iexplore.exe",
"http://www.foo.com",
"",
SW_MAXIMIZE);
return 0;
}
Do you really need to launch IE or just some content in a browser? The ShellExecute function will launch whatever browser is configured to be the default. Call it like this:
ShellExecute(NULL, "open", szURL, NULL, NULL, SW_SHOW);
Using just standard C++, if iexplore is on the path then
#include <stdlib.h>
...
string foo ("iexplore.exe http://example.com");
system(foo.c_str());
If it's not on the path then you need to work out the path somehow and pass the whole thing to the system call.
string foo ("path\\to\\iexplore.exe http://example.com");
system(foo.c_str());
I'm with Glen and John, except I'd prefer to use CreateProcess instead. That way you have a process handle you can do something with. Examples might be Kill IE when you are done with it, or have a thread watching for IE to terminate (WaitForSingleObject with the process handle) so it could do something like restart it, or shut down your program too.
Try this
system("\"C:\Program Files\Internet Explorer\iexplore\" http://www.shail.com");
Works perfectly..