ris
asked on
Launch default web browser to open HTML file
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.ex e /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\\cm d.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
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.ex
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\\cm
sFullCommandLine.Format("%
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, "ShellExecute()" always launches the application that is registered for the file type, just as if you would double-click on it.
Is anything unclear about this?
ASKER
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.
Oh, I am sure that it will work :o)