面对IE保护模式的开发者生存之道
以下是在保存日志到文件的演示项目中的代码。我们首先调用IEShowSaveFileDialog()来提示用户选择文件路径:
void CBandDialog::OnSaveLog(UINT uCode, int nID, HWND hwndCtrl)接下来,我们使用IEGetWriteableFolderPath()来获得缓冲区目录的位置。
{
HRESULT hr;
HANDLE hState;
LPWSTR pwszSelectedFilename = NULL;
const DWORD dwSaveFlags =
OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
OFN_OVERWRITEPROMPT;
// Get a filename from the user.
hr = IEShowSaveFileDialog (
m_hWnd, L"Saved log.txt", NULL,
L"Text files|*.txt|All files|*.*|",
L"txt", 1, dwSaveFlags, &pwszSelectedFilename,
&hState );
if ( S_OK != hr )
return;
LPWSTR pwszCacheDir = NULL;如果一起都顺利的话,我们调用另一个保护模式API,IESaveFile()。IESaveFile()获得IEShowSaveFileDialog()返回的状态句柄,以及我们的临时文件的路径。注意这个HANDLE不是一个标准的句柄,不需要被关闭;在IESaveFile()调用完后,这个HANDLE会被自动释放。
TCHAR szTempFile[MAX_PATH] = {0};
// Get the path to the IE cache dir, which is a dir that we're allowed
// to write to in protected mode.
hr = IEGetWriteableFolderPath ( FOLDERID_InternetCache, &pwszCacheDir );
if ( SUCCEEDED(hr) )
{
// Get a temp file name in that dir.
GetTempFileName ( CW2CT(pwszCacheDir), _T("bob"), 0, szTempFile );
CoTaskMemFree ( pwszCacheDir );
// Write our data to that temp file.
hr = WriteLogFile ( szTempFile );
}
由于某些原因,我们没有结束调用IESaveFile(),举个例子来说,如果当写临时文件的时候出现一个错误,我们需要清除这个HANDLE和任何IEShowSaveFileDialog()分配的任何内部数据。我们通过调用IECancelSaveFile()来实现:
if ( SUCCEEDED(hr) )
{
// If we wrote the file successfully, have IE save that data to
// the path that the user chose.
hr = IESaveFile ( hState, T2CW(szTempFile) );
// Clean up our temp file.
DeleteFile ( szTempFile );
}
else
{
// We couldn't complete the save operation, so cancel it.
IECancelSaveFile ( hState );
}
0
相关文章