Solved

Launch default web browser to open HTML file

Posted on 2002-06-06
5
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replacement selected text 2 67
Shell script to login to remote m/c and execute commands 10 79
WinWaitActive parameters 12 31
substring method in java 1 137
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.
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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