IE安全設置下有4個區域 對應的設置在不同的注冊表中。
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1] 是Local intranet相關操作,如果要修改受Local intranet 下的保護模式設置,請修改該項下的鍵2500
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\2] 是受信任的站點相關操作,如果要修改受信任站點 下的保護模式設置,請修改該項下的鍵2500
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3] 是Intranet相關操作,如果要修改Intranet下的保護模式設置,請修改該項下的鍵2500
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\4] 是受限制的站點相關操作,如果要修改受限制的站點下的保護模式設置,請修改該項下的鍵2500
IE保護模式通過修改下面這個注冊表項即可,[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3]
2500 = 0x0 (開啟),0x3(關閉)。
這里介紹下操作注冊表的 幾個Windows API函數
打開注冊表
https://docs.microsoft.com/zh-cn/windows/desktop/api/winreg/nf-winreg-regopenkeyexa
LONG WINAPI RegOpenKeyEx(
_In_ HKEY hKey,
_In_opt_ LPCTSTR lpSubKey,
_In_ DWORD ulOptions,
_In_ REGSAM samDesired,
_Out_ PHKEY phkResult
);
查找注冊表項的值
https://docs.microsoft.com/zh-cn/windows/desktop/api/winreg/nf-winreg-regqueryvalueexa
LONG WINAPI RegQueryValueEx(
_In_ HKEY hKey,
_In_opt_ LPCTSTR lpValueName,
_Reserved_ LPDWORD lpReserved,
_Out_opt_ LPDWORD lpType,
_Out_opt_ LPBYTE lpData,
_Inout_opt_ LPDWORD lpcbData
);
設置注冊表項的值
https://docs.microsoft.com/zh-cn/windows/desktop/api/winreg/nf-winreg-regsetvalueexa
LONG WINAPI RegSetValueEx(
_In_ HKEY hKey,
_In_opt_ LPCTSTR lpValueName,
_Reserved_ DWORD Reserved,
_In_ DWORD dwType,
_In_ const BYTE *lpData,
_In_ DWORD cbData
);
代碼實現
主要就是上面3個Windows API 函數的實現,下面直接看代碼實現吧。直接用devc++ 編譯運行即可。
#include <Windows.h>
#include <stdio.h>
bool checkIEProtectMode()
{
HKEY hKEY;
if ( ERROR_SUCCESS == ::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", 0, KEY_QUERY_VALUE, &hKEY ) )
{
DWORD PerfData = 0;
DWORD m_type = REG_DWORD;
DWORD BufferSize = sizeof(DWORD);
if ( ERROR_SUCCESS == RegQueryValueEx( hKEY, "2500", NULL, &m_type, (LPBYTE)&PerfData, &BufferSize ) )
{
if ( PerfData == 3 )
{
printf("IE保護模式當前狀態:關閉\n");
RegCloseKey( hKEY );
return false;
}
}
}
RegCloseKey( hKEY );
printf("IE保護模式當前狀態:打開\n");
return true;
}
bool openIEProtectMode()
{
HKEY hKEY;
if ( ERROR_SUCCESS == ::RegOpenKeyEx( HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", 0, KEY_ALL_ACCESS, &hKEY ) )
{
DWORD m_Dword = 0;
if( ERROR_SUCCESS == ::RegSetValueEx( hKEY,"2500",0,REG_DWORD,(PBYTE)&m_Dword,sizeof(DWORD)) )
{
printf("打開IE保護模式成功!\n");
return true;
}
}
RegCloseKey( hKEY );
printf("打開IE保護模式失敗!\n");
return false;
}
bool closeIEProtectMode()
{
HKEY hKEY;
if ( ERROR_SUCCESS == ::RegOpenKeyEx( HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", 0, KEY_ALL_ACCESS, &hKEY ) )
{
DWORD m_Dword = 3;
if( ERROR_SUCCESS == ::RegSetValueEx( hKEY,"2500",0,REG_DWORD,(PBYTE)&m_Dword,sizeof(DWORD)) )
{
printf("關閉IE保護模式成功!\n");
return true;
}
}
RegCloseKey( hKEY );
printf("關閉IE保護模式失敗!\n");
return false;
}
int main(int args, char** argv)
{
if (checkIEProtectMode() )
{
closeIEProtectMode();
checkIEProtectMode();
} else
{
openIEProtectMode();
checkIEProtectMode();
}
return 0;
}
運行效果
剛開始是關閉的
運行程序,已經通過程序打開了。