admin 管理员组

文章数量: 887021

//MFC  
//用默认浏览器打开网页
//如果找不到默认浏览器,则默认用IE打开

void OpenURL(const CString & openUrl)
{
    if(openUrl == "")
        return;
    HKEY  hkRoot, hSubKey;   //注册表根关键字及子关键字
    CString ValueName;
    unsigned char DataValue[MAX_PATH];
    unsigned long cbValueNAME = MAX_PATH;
    unsigned long cbDataNAME = MAX_PATH;    
<pre name="code" class="cpp"><pre name="code" class="cpp">    CString ShellChar;//定义命令行
    DWORD  dwType;
    //打开注册表根关键字
    if(RegOpenKey(HKEY_CLASSES_ROOT,NULL,&hkRoot) == ERROR_SUCCESS)
    {
        //打开子关键字
        if(RegOpenKeyEx(hkRoot,L"htmlfile\\Shell\\open\\command",
           0, KEY_ALL_ACCESS, &hSubKey) == ERROR_SUCCESS)
        {
.            RegEnumValue(hSubKey, 0, (LPWSTR)ValueName.GetBuffer(MAX_PATH), &cbValueName, NULL,
                           &dwType, DataValue, &cbDataValue);
             ValueName.ReleaseBuffer();
             //调用参数赋值
             ShellChar = (char *)DataValue;
             if(ShellChar == "\"")
             {
                 ShellExecute(NULL, L"open",(LPWSTR)openUrl.GetBuffer(MAX_PATH), NULL, NULL, SW_SHOWNORMAL);
                 openUrl.ReleaseBuffer();
             }
             else
             {
                 ShellChar = ShellChar + openUrl;
                 WinExec((LPCSTR)ShellChar.GetBuffer(MAX_PATH),SW_SHOW);
                 ShellChar.ReleaseBuffer();
             }
        }
        else
        {
             RegCloseKey(hSubKey);
             RegCloseKey(hRoot);             <pre name="code" class="cpp"><pre name="code" class="cpp">             ShellExecute(NULL, L"open",(LPWSTR)openUrl.GetBuffer(MAX_PATH), NULL, NULL, SW_SHOWNORMAL);
<pre name="code" class="cpp"><pre name="code" class="cpp"><pre name="code" class="cpp">             openUrl.ReleaseBuffer();
 
 
 
 } 

}
 
 


}
 

本文标签: 打开网页 浏览器 MFC