Changing IE Show Picture Setting

The following sample program demonstrates how to change the Internet Explorer Show Picture setting.  This program toggles the state of this setting.  Note the usage of the SendMessageTimeoutW function with the WM_SETTINGCHANGE message that causes all the open instances of IE to update themselves.  If you use SendMessageW instead, and a window procedure for some window on the system falls into an infinite loop for some reason, then, your process would also stop responding.  Here’s the sample program.  Note that this is a Unicode program, so it doesn’t run on Win95/98/Me.  However, making this an ANSI application is a trivial task, as I’ve done it below the Unicode source code:

int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HKEY hkey;
DWORD dwType = REG_SZ;
BYTE pData[100];
DWORD cbSize = 100;
BYTE pDataToWrite[100];
DWORD cbSizeToWrite = 100;
::ZeroMemory( pData, sizeof(BYTE) * cbSize );
::ZeroMemory( pDataToWrite, sizeof(BYTE) * cbSizeToWrite );
DWORD dwResult;

WCHAR szRegKey[] = L"Software\\Microsoft\\Internet Explorer\\Main\\";

LONG lRet;

try
{
lRet = ::RegOpenKeyExW( HKEY_CURRENT_USER,
szRegKey,
0, KEY_READ | KEY_WRITE, &hkey );
if (ERROR_SUCCESS != lRet)
throw L"Failed to open the Internet Explorer registry key.";
lRet = ::RegQueryValueExW( hkey, L"Display Inline Images", NULL,
&dwType, pData, &cbSize );
if (ERROR_SUCCESS != lRet)
throw L"Failed to read the registry value.";

if (::lstrcmpiW( (LPCWSTR) pData, L"yes" ) == 0)
::lstrcpyW( (LPWSTR) pDataToWrite, L"no" );
else
::lstrcpyW( (LPWSTR) pDataToWrite, L"yes" );

cbSizeToWrite = (::lstrlenW( (LPCWSTR) pDataToWrite ) + 1) * sizeof(WCHAR);

lRet = ::RegSetValueExW( hkey, L"Display Inline Images", 0, REG_SZ,
pDataToWrite, cbSizeToWrite );
if (ERROR_SUCCESS != lRet)
throw L"Failed to set the registry key value.";

::RegCloseKey( hkey );

::SendMessageTimeoutW( HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) szRegKey,
SMTO_ABORTIFHUNG | SMTO_BLOCK, 3 * 1000, &dwResult ); // wait 3 seconds on each window
}
catch (WCHAR * pwsz)
{
::MessageBoxW( ::GetDesktopWindow(), pwsz, L"Error", MB_OK | MB_ICONSTOP );
}
return 0;
}

Here’s a version of the same program that can be compiled using both Unicode and ANSI modes.

int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HKEY hkey;
DWORD dwType = REG_SZ;
BYTE pData[100];
DWORD cbSize = 100;
BYTE pDataToWrite[100];
DWORD cbSizeToWrite = 100;
::ZeroMemory( pData, sizeof(BYTE) * cbSize );
::ZeroMemory( pDataToWrite, sizeof(BYTE) * cbSizeToWrite );
DWORD dwResult;

TCHAR szRegKey[] = _T("Software\\Microsoft\\Internet Explorer\\Main\\");

LONG lRet;

try
{
lRet = ::RegOpenKeyEx( HKEY_CURRENT_USER,
szRegKey,
0, KEY_READ | KEY_WRITE, &hkey );
if (ERROR_SUCCESS != lRet)
throw _T("Failed to open the Internet Explorer registry key.");
lRet = ::RegQueryValueEx( hkey, _T("Display Inline Images"), NULL,
&dwType, pData, &cbSize );
if (ERROR_SUCCESS != lRet)
throw _T("Failed to read the registry value.");

if (::lstrcmpi( (LPCTSTR) pData, _T("yes") ) == 0)
::lstrcpy( (LPTSTR) pDataToWrite, _T("no") );
else
::lstrcpy( (LPTSTR) pDataToWrite, _T("yes") );

cbSizeToWrite = (::lstrlen( (LPCTSTR) pDataToWrite ) + 1) * sizeof(TCHAR);

lRet = ::RegSetValueEx( hkey, _T("Display Inline Images"), 0, REG_SZ,
pDataToWrite, cbSizeToWrite );
if (ERROR_SUCCESS != lRet)
throw _T("Failed to set the registry key value.");

::RegCloseKey( hkey );

::SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) szRegKey,
SMTO_ABORTIFHUNG | SMTO_BLOCK, 3 * 1000, &dwResult ); // wait 3 seconds on each window
}
catch (TCHAR * psz)
{
::MessageBox( ::GetDesktopWindow(), psz, _T("Error"), MB_OK | MB_ICONSTOP );
}
return 0;
}

This article originally appeared on BeginThread.com. It’s been republished here, and may contain modifications from the original article.

Posted in Visual C++