Solved

Launch default web browser to open HTML file

Posted on 2002-06-06
5
290 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 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now