?
Solved

Launch default web browser to open HTML file

Posted on 2002-06-06
5
Medium Priority
?
316 Views
Last Modified: 2013-11-20
I want to programmatically launch the default web browser to open an HTML file, but I can't get it to work.  I am trying to do it by calling CreateProcess and passing it the command:

start "" /I "File Path.html"

This works when I type it on the command line, but not when I use CreateProcess.  CreateProcess() returns FALSE and GetLastError() returns 123 (The filename, directory name, or volume label syntax is incorrect.)

I see that start is not a real executable, so I tried it with cmd.exe like this:

cmd.exe /c start "" /I "File Path.html"

with the same results, so I tried it with the full path, like this:

c:\windows\system32\cmd.exe /c start "" /I "File Path.html"

but still I get the same result.  Here is my code snippet, what should I do to accomplish this task?

--------------------------


//CString sFilePath is defined before this point
//it is an output file of another operation
//I am confident that the file path is correct and complete
//open the output file using the default web browser
//by executing:
//cmd.exe /C start "" /I "FilePath"
CString sFullCommandLine;
//TODOLATER: read the appropriate path from the registry
CString sCmdExePath = "c:\\windows\\system32\\cmd.exe";
sFullCommandLine.Format("%s /C start \"\" /I \"%s\"", sCmdExePath, sFilePath);

PROCESS_INFORMATION ProcessInfo = {0,0,0,0}; //receives info about the launched process

STARTUPINFO StartupInfo; //specifies window creation info for CreateProcess()
StartupInfo.cb          = sizeof(STARTUPINFO);
StartupInfo.lpReserved  = NULL;
StartupInfo.lpDesktop   = NULL;
StartupInfo.lpTitle     = NULL;
StartupInfo.cbReserved2 = NULL;
StartupInfo.lpReserved2 = NULL;
StartupInfo.dwFlags     = NULL;

BOOL bSuccess = CreateProcess(
    sFullCommandLine,   //App Name
    NULL,               //command line
    NULL,               //security attributes
    NULL,               //thread attributes
    TRUE,               //inherit handles,
    NULL,               //creation flags
    NULL,               //environment
    NULL,               //working directory
    &StartupInfo,       //startup info
    &ProcessInfo);      //lpProcessInformation
0
Comment
Question by:ris
  • 4
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 7060430
Why don't you just use

ShellExecute ( NULL, "open", "c:\\mypath\\myfile.htm", NULL, NULL, SW_SHOWNORMAL);

?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7060433
BTW, "ShellExecute()" always launches the application that is registered for the file type, just as if you would double-click on it.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7063228
Is anything unclear about this?
0
 
LVL 1

Author Comment

by:ris
ID: 7063311
Thanks, that's exactly the solution I was looking for.  I haven't had a chance to test it yet, but I expect it to work.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7063760
Oh, I am sure that it will work :o)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question