Last Updated 2005/07/29
Programming Tips Windows  索 引 
標準ブラウザで表示
2005/07/29

HTML ファイルを標準のブラウザで表示するには,ShellExecute() を使用すればよい.
ShellExecute() に HTML を渡せば標準のブラウザで表示を行う.

  ShellExecute(NULL, "open", "http:/hogehoge/", NULL, ".", SW_SHOW);


標準ブラウザがなにかを知るためには FindExecutable() を使用する.
レジストリの状態によってパスに空白があるのにダブルコーテーションがない場合があり注意が必要である.


ShellExecute() を使用しない場合は以下の方法で可能である.

pszURL には "http://dss.o.oo7.jp/" や "ftp:..." や "mailto:..." を渡す.

以下のコードをコンパイルするには,
#include <windows.h>
#include <ole2.h>
#include <intshcut.h>
#include <shlobj.h>
をインクルードする.

unresolved のエラーが出る場合は,isguids.h を追加する.



/**********************************************************************/
/* InvokeCommand for an Internet shortcut                             */
/*--------------------------------------------------------------------*/
/* Parameters:    hwnd       - Parent WIndow handle                   */
/*                pszURL     - URL to Internet target(http:, ftp:,etc)*/
/*                                                                    */
/*                nCmdShow   - Window mode at start of Browser        */
/* Return value : TRUE  - Command invoked                             */
/*                FALSE - Error                                       */
/**********************************************************************/

BOOL InvokeInternetShortcut( HWND hwnd,  LPSTR pszUrl,  int   nCmdShow )
{
    HRESULT hrCoInit, hr;
    PIUniformResourceLocator pUrl;     // URL interface
    URLINVOKECOMMANDINFO urlcmd;
  
    hrCoInit = CoInitialize( NULL );         // Initialize OLE
 
    // Get URL interface. If Internet environment has
    // not been correctly set up,
    // this call will result in a failure!
    hr = CoCreateInstance( &CLSID_InternetShortcut, 
                           NULL, 
                           CLSCTX_INPROC_SERVER,
                           &IID_IUniformResourceLocator,
                           &pUrl );
    if( SUCCEEDED( hr ) ) {
        // Set URL -----------------------------------------------------
        hr = pUrl->lpVtbl->SetURL( pUrl, pszUrl, IURL_SETURL_FL_GUESS_PROTOCOL  );
        
        // Invoke command for an URL!!
        if( SUCCEEDED( hr ) ) {
 
            urlcmd.dwcbSize = sizeof(URLINVOKECOMMANDINFO);
            urlcmd.dwFlags = IURL_INVOKECOMMAND_FL_USE_DEFAULT_VERB ;
            urlcmd.hwndParent = NULL; //hwnd; (just in case)
            urlcmd.pcszVerb = NULL;   //"open";
 
            hr = pUrl->lpVtbl->InvokeCommand( pUrl,&urlcmd);
        } 
        pUrl->lpVtbl->Release( pUrl );
    }
 
    if( SUCCEEDED( hrCoInit ) ) CoUninitialize();
  
    return SUCCEEDED( hr );
}


参照
前後のTips
標準ブラウザで表示

DSS ProgrammingTipsCGI Ver2.02