添加获取IE的可信站点和受限站点 -- VC

其他代码 blackfeather

 

网上找到的这份代码,添加IE的可信站点和受限站点有两种方法,一种是调用IE COM的某某,没细看,再了就是这个方法,直接写注册表。。。

不多说了 ,直接上代码:

 

 

/************************************************************************/

/* 添加站点                                                             

/* DWORD dwType 2--受信任站点  4--受限制站点                                                                            

/************************************************************************/

BOOL SetTrustfulUrl(HKEY hKey, char *szUrl, DWORD dwType)

{

    HKEY hkResult;

    int rc = 0;

    char *p = NULL;

    char szProtocol[MAX_PATH] = {0};

    char szData[MAX_PATH] = {0};

    char szTemp[MAX_PATH] = {0};

    char szRegPath[MAX_PATH] = {0};

    strcpy(szTemp, szUrl);

    //获取协议

    p = strchr(szTemp, ':');

    if (p != NULL)

    {

        *p = '\0';

        strcpy(szProtocol, szTemp);

        p += 3;

        strcpy(szTemp, p);

    }

    else

    {

        strcpy(szProtocol, "*");

    }

    //去除多余的url

    p = strrchr(szTemp, '/');

    if (p != NULL)

    {

        *p = '\0';

    }

    

    //判断是IP还是域名

    if (IsIP(szTemp))            //IP类站点添加

    {

        DWORD dwKeys = 0;

        sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges");

        

        rc = RegCreateKey(hKey, szRegPath, &hkResult);

        //先获取该key下有多少个项

        rc = RegQueryInfoKey(hkResult, NULL, NULL, NULL, &dwKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

        

        RegCloseKey(hkResult);

        hkResult = NULL;

        if (rc != ERROR_SUCCESS)

        {

            return FALSE;

        }

        else

        {            

            sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges\\Range%d", dwKeys+10);

            

            rc = RegCreateKey(hKey, szRegPath, &hkResult);

            //必须要先创建DWORD值,再创建字符串值,否则不能在Internet选项中信任站点列表中显示

            RegSetValueEx(hkResult, szProtocol, NULL, REG_DWORD, (BYTE *)&dwType, sizeof(DWORD));

            RegSetValueEx(hkResult, ":Range", NULL, REG_SZ, (BYTE *)&szTemp, strlen(szTemp));

            

            RegCloseKey(hkResult);

            hkResult = NULL;

        }

    }

    else                //域名类站点添加

    {

        p = strrchr(szTemp, '.');

        

        if (p == NULL)

        {

            sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\%s", szTemp);

        } 

        else

        {

            char szTempStr[MAX_PATH] = {0};

            strcpy(szTempStr, p);

            *p = '\0';

            p = strrchr(szTemp, '.');

            if (p == NULL)

            {

                sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\%s%s", szTemp, szTempStr);

            }

            else

            {

                *p = '\0';

                p++;

                sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\%s%s\\%s", p, szTempStr, szTemp);

            }

        }

        rc = RegCreateKey(hKey, szRegPath, &hkResult);

        RegSetValueEx(hkResult, szProtocol, NULL, REG_DWORD, (BYTE*)&dwType, sizeof(DWORD));

        

        RegCloseKey(hkResult);

        hkResult = NULL;

    }

    return TRUE;

}


/************************************************************************/

/* 获取站点                                                         

/* DWORD dwType 2--受信任站点   4--受限制站点                            

/************************************************************************/

BOOL GetTrustfunUrl(HKEY hKey, char *szUrl, DWORD dwType)

{

    int rc = 0;

    int rc2 = 0;

    int rc3 = 0;

    int index = 0;

    int subIndex = 0;

    int protocolIndex = 0;

    HKEY hkResult;

    HKEY hkSubKey;

    HKEY hkHost;

    char szKeyName[MAX_PATH] = {0};

    char szHost[MAX_PATH] = {0};

    char szTemp[MAX_PATH] = {0};

    char szProtocol[MAX_PATH] = {0};

//    char szUrl[MAX_PATH] = {0};

    DWORD dwProtocol = 0;

    DWORD dwKeys = 0;

    DWORD dwData = 0;

    DWORD dwLen = sizeof(DWORD);

    //获取域名形式站点

    rc = RegCreateKey(hKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains", &hkResult);

    while (RegEnumKey(hkResult, index, szKeyName, MAX_PATH) == ERROR_SUCCESS)

    {

        if (RegOpenKey(hkResult, szKeyName, &hkSubKey) == ERROR_SUCCESS)

        {

            rc = RegQueryInfoKey(hkSubKey, NULL, NULL, NULL, &dwKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

            

            //如果该项下没有子项,即没有主机名,表示为 *.xx.xxx

            if (dwKeys < 1)

            {

                dwProtocol = sizeof(szProtocol);

                protocolIndex = 0;

                while (RegEnumValue(hkSubKey, protocolIndex, szProtocol, &dwProtocol, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)

                {    

                    if (strcmp(szProtocol, "*") == 0)

                    {

                        sprintf(szTemp, "*.%s", szKeyName);

                    }

                    else

                    {

                        sprintf(szTemp, "%s://*.%s", szProtocol, szKeyName);

                    }

                    strcat(szUrl, szTemp);

                    strcat(szUrl, ",");

                    memset(szTemp, 0, MAX_PATH);

                    

                    if (strlen(szUrl) > 200)        //此处做了长度限制,可根据需要自行修改

                    {

                        *strrchr(szUrl, ',') = '\0';

                        return TRUE;

                    }

                    

                    protocolIndex ++;

                    memset(szProtocol, 0, MAX_PATH);

                } 

            }

            else

            {

                subIndex = 0;

                while (RegEnumKey(hkSubKey, subIndex, szHost, MAX_PATH) == ERROR_SUCCESS) 

                {    

                    if (RegOpenKey(hkSubKey, szHost, &hkHost) == ERROR_SUCCESS)

                    {    

                        dwProtocol = sizeof(szProtocol);

                        protocolIndex = 0;

                        while (RegEnumValue(hkHost, protocolIndex, szProtocol, &dwProtocol, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)

                        {                                        

                            RegQueryValueEx(hkHost, szProtocol, NULL, NULL, (LPBYTE)&dwData, &dwLen);

                            if (dwData == dwType)

                            {    

                                if (strcmp(szProtocol, "*") == 0)

                                {

                                    sprintf(szTemp, "%s.%s", szHost, szKeyName);

                                }

                                else

                                {

                                    sprintf(szTemp, "%s://%s.%s", szProtocol, szHost, szKeyName);

                                }

                                strcat(szUrl, szTemp);

                                strcat(szUrl, ",");

                                memset(szTemp, 0, MAX_PATH);

                                if (strlen(szUrl) > 200)

                                {

                                    RegCloseKey(hkHost);

                                    RegCloseKey(hkSubKey);

                                    RegCloseKey(hkResult);

                                    hkHost = NULL;

                                    hkSubKey = NULL;

                                    hkResult = NULL;

                                    *strrchr(szUrl, ',') = '\0';

                                    return TRUE;

                                }

                            }

                            protocolIndex ++;

                            memset(szProtocol, 0, MAX_PATH);

                        } 

                        

                        RegCloseKey(hkHost);

                        hkHost = NULL;

                    }

                    subIndex ++;

                    memset(szHost, 0, MAX_PATH);

                } 

            }

            RegCloseKey(hkSubKey);

            hkSubKey = NULL;

        }

        index ++;

        memset(szKeyName, 0, MAX_PATH);

    } 

    

    RegCloseKey(hkResult);

    hkResult = NULL;

    //获取IP形式站点

    rc = RegCreateKey(hKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges", &hkResult);

    index = 0;

    while (RegEnumKey(hkResult, index, szKeyName, MAX_PATH) == ERROR_SUCCESS)

    {

        if (RegOpenKey(hkResult, szKeyName, &hkSubKey) == ERROR_SUCCESS)

        {

            DWORD dwHost = MAX_PATH;

            RegQueryValueEx(hkSubKey, ":Range", NULL, NULL, (LPBYTE)szHost, &dwHost);

            subIndex = 0;

            dwProtocol = sizeof(szProtocol);

            while (RegEnumValue(hkSubKey, subIndex, szProtocol, &dwProtocol, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)

            {

                if (strcmp(szProtocol, ":Range") != 0)

                {

                    RegQueryValueEx(hkSubKey, szProtocol, NULL, NULL, (LPBYTE)&dwData, &dwLen);

                    if (dwData == dwType)

                    {

                        if (strcmp(szProtocol, "*") == 0)

                        {

                            sprintf(szTemp, "%s", szHost);

                        }

                        else

                        {                                            

                            sprintf(szTemp, "%s://%s", szProtocol, szHost);

                        }

                        strcat(szUrl, szTemp);

                        strcat(szUrl, ",");

                    }                    

                    memset(szTemp, 0, MAX_PATH);

                    if (strlen(szUrl) > 200)

                    {

                        RegCloseKey(hkSubKey);

                        RegCloseKey(hkResult);

                        hkSubKey = NULL;

                        hkResult = NULL;

                        *strrchr(szUrl, ',') = '\0';

                        return TRUE;

                    }

                }

                subIndex++;

                memset(szProtocol, 0, MAX_PATH);

            }

            RegCloseKey(hkSubKey);

            hkSubKey = NULL;

        }

        index ++;

        memset(szKeyName, 0, MAX_PATH);

    }

    RegCloseKey(hkResult);

    hkResult = NULL;

    *strrchr(szUrl, ',') = '\0';

    return TRUE;

}


//判断是否是IP

BOOL IsIP(char *szIP)

{

    if (strlen(szIP) == 0)

    {

        return FALSE;

    }

    

    int ip_part1 = 0;

    int ip_part2 = 0;

    int ip_part3 = 0;

    int ip_part4 = 0;

    

    if (sscanf(szIP, "%d.%d.%d.%d", &ip_part1, &ip_part2, &ip_part3, &ip_part4) != 4)

    {

        return FALSE;

    }

    

    if (!((ip_part1 >= 0 && ip_part1 <= 255) && (ip_part2 >= 0 && ip_part2 <= 255) && (ip_part3 >= 0 && ip_part3 <= 255) && (ip_part4 >= 0 && ip_part4 <= 255)))

    {

        return FALSE;

    }    

    return TRUE;

}

 
调用方法:
 
SetTrustfulUrl(HKEY_CURRENT_USER,"http://192.168.0.150",2);  //测试用  添加一个可信站点
 
实际测试效果不错,收藏!

 

评论列表:

发表评论: