📄 filehandler.cpp
字号:
// FileHandler.cpp : Implementation of CFileHandler
#include "stdafx.h"
#include "OpenSave.h"
#include "FileHandler.h"
#include "mshtml.h"
#define MAX_TEXT_FILE 50000
#define SAFERELEASE(x) if(x) x->Release();
#define IID_IHTMLAppBehavior __uuidof(IHTMLAppBehavior)
int main() // ATL MinDependency release won't compile without a main() - this is never called
{
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CFileHandler
//
// isValidExt()
// validates the filter passed to getFilename - a quick and dirty test which just checks for the *
BOOL isValidExt(char* szIn)
{
while(*szIn)
{
if (*szIn == '*') return true;
szIn++;
}
return false;
}
//
// isValidFilename - another quick and dirty hack which checks for a "." in the filename passed in
//
BOOL isValidFilename(char* szIn)
{
while(*szIn)
{
if (*szIn == '.') return true;
szIn++;
}
return false;
}
//
// Override of IObjectWithSite::SetSite()
// Internet Explorer QIs for IObjectWithSite, and calls this method
// with a pointer to its IOleClientSite
//
STDMETHODIMP CFileHandler::SetSite(IUnknown* pSite)
{
m_pClientSite = pSite;
return S_OK;
}
//
// getFilename - a user-friendly COM wrapper for the win API getOpenFilename
//
STDMETHODIMP CFileHandler::getFilename(BSTR bstrAction, BSTR defaultName, BSTR defaultExt,
BSTR defaultDescription, BSTR* pName)
{
USES_CONVERSION;
OPENFILENAME OpenFileName;
TCHAR szFile[MAX_PATH] = {0};
TCHAR* szFilterExt = OLE2A(defaultExt);
TCHAR* szFilter = OLE2A(defaultDescription);
char* szAllDesc = "All Files (*.*)";
char* szAllFilt = "*.*";
char* ploc = "";
if (!isSafeClientSite())
return S_FALSE;
// Set up a buffer with filter information for the FileOpen dialog
if (isValidExt(szFilterExt) && strlen(szFilter) > 1)
{
ploc = (char*)memcpy(szFilter + strlen(szFilter)+1,szFilterExt,strlen(szFilterExt)+ 1) + strlen(szFilterExt)+1;
ploc = (char*)memcpy(ploc,szAllDesc,strlen(szAllDesc)+1) + strlen(szAllDesc)+1;
ploc = (char*)memcpy(ploc,szAllFilt,strlen(szAllFilt)+1) + strlen(szAllFilt);
memset((char*)ploc,'\0',5);
}
else
{
strcpy(szFilter,szAllDesc);
ploc = (char*)memcpy(szFilter + strlen(szAllDesc)+1, szAllFilt,strlen(szAllFilt)+1) + strlen(szAllFilt);
memset((char*)ploc,'\0',5);
}
strcpy(szFile, OLE2A(defaultName));
// Fill in the OPENFILENAME structure
OpenFileName.lStructSize = sizeof(OPENFILENAME);
OpenFileName.hwndOwner = NULL;
OpenFileName.hInstance = NULL;
OpenFileName.lpstrFilter = szFilter;
OpenFileName.lpstrCustomFilter = NULL;
OpenFileName.nMaxCustFilter = 0;
OpenFileName.nFilterIndex = 0;
OpenFileName.lpstrFile = szFile;
OpenFileName.nMaxFile = sizeof(szFile);
OpenFileName.lpstrFileTitle = NULL;
OpenFileName.nMaxFileTitle = 0;
OpenFileName.lpstrInitialDir = NULL;
OpenFileName.lpstrTitle = "Select File";
OpenFileName.nFileOffset = 0;
OpenFileName.nFileExtension = 0;
OpenFileName.lpstrDefExt = NULL;
OpenFileName.lCustData = NULL;
OpenFileName.lpTemplateName = NULL;
OpenFileName.Flags = OFN_SHOWHELP | OFN_EXPLORER;
// Call the common dialog function.
if (!_stricmp(OLE2A(bstrAction), "save"))
{
if (GetSaveFileName(&OpenFileName))
{
//
// NOTE: freeing the pName BSTR is the responsibility of the client.
//
*pName = SysAllocString(A2OLE(OpenFileName.lpstrFile));
return S_OK;
}
}
else
{
if (GetOpenFileName(&OpenFileName))
{
*pName = SysAllocString(A2OLE(OpenFileName.lpstrFile));
return S_OK;
}
}
return S_FALSE;
}
//
// Loadfile() loads a text file and returns the text in the file
//
STDMETHODIMP CFileHandler::loadFile(BSTR bstrPath, BSTR *bstrFileText)
{
if (!isSafeClientSite())
return S_FALSE;
USES_CONVERSION;
TCHAR strFileText[MAX_TEXT_FILE];
BOOL bReadSuccess = TRUE;
unsigned long numRead = 0;
HANDLE hFile = CreateFile(OLE2A(bstrPath), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hFile != INVALID_HANDLE_VALUE)
{
memset((void*)&strFileText, '\0', MAX_TEXT_FILE);
bReadSuccess = ReadFile(hFile, (void*)&strFileText, MAX_TEXT_FILE, &numRead, NULL);
if (bReadSuccess)
{
*bstrFileText = SysAllocString(A2OLE(strFileText));
CloseHandle(hFile);
return S_OK;
}
CloseHandle(hFile);
}
*bstrFileText = SysAllocString(OLESTR("ERROR: File Not Found"));
return S_FALSE;
}
//
// saveFile() saves supplied text to a specified file
//
STDMETHODIMP CFileHandler::saveFile(BSTR bstrPath, BSTR bstrFileText, long *bSuccess)
{
if (!isSafeClientSite()) return S_FALSE;
USES_CONVERSION;
unsigned long numWritten = 0;
TCHAR* fileText = OLE2A(bstrFileText);
unsigned long fileLength = strlen(fileText);
HANDLE hFile = CreateFile( OLE2A(bstrPath), GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile != INVALID_HANDLE_VALUE )
{
*bSuccess = WriteFile(hFile, fileText, fileLength, &numWritten, NULL);
}
else *bSuccess = 0;
CloseHandle(hFile);
return S_OK;
}
//
// isSafeClientSite() ascertains that the object is not being hosted in a remote web page,
// and makes sure the file functions are not run if it is.
//
BOOL CFileHandler::isSafeClientSite()
{
IServiceProvider* pISP = NULL;
IWebBrowserApp* pApp = NULL;
IHTMLAppBehavior* pHTA = NULL;
IWebBrowser* pBrowser = NULL;
IDispatch* pDisp = NULL;
IHTMLDocument2* pDoc = NULL;
BSTR bstrProt;
BOOL bRetVal = false;
//HRESULT hr = S_OK;
USES_CONVERSION;
if(m_pClientSite)
{
if(SUCCEEDED(m_pClientSite->QueryInterface(IID_IServiceProvider,(void**)&pISP)))
{
if(SUCCEEDED(pISP->QueryService(IID_IWebBrowserApp, IID_IWebBrowserApp, (void**)&pApp)))
{
if(SUCCEEDED(pApp->QueryInterface(IID_IWebBrowser,(void**) &pBrowser)))
{
if(SUCCEEDED(pBrowser->get_Document(&pDisp)))
{
if(SUCCEEDED(pDisp->QueryInterface(IID_IHTMLDocument, (void**)&pDoc)))
{
if(SUCCEEDED(pDoc->get_protocol(&bstrProt)))
{
if(!stricmp(OLE2A(bstrProt),"file protocol"))
{
bRetVal = true;
goto cleanup;
}
}
}
}
}
}
else
{
bRetVal = true;
goto cleanup;
}
}
else
{
bRetVal = false;
goto cleanup;
}
}
cleanup:
SAFERELEASE(pISP);
SAFERELEASE(pApp);
SAFERELEASE(pBrowser);
SAFERELEASE(pDoc);
SAFERELEASE(pHTA);
return bRetVal;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -