N
NewBieDev2012
Guest
Hello Everyone,
I have a simple HTML file (and associated JavaScript) in an MFC resource DLL. I want to load this resource DLL and display the HTML file in CDHTMLDialog in my MFC application.
I have used two methods as follows:
Method 1:
//
// CDHtmlDialog* pWnd: Dialog on which HTML is to be shown
// int iResID : Resource ID of HTML (in resource DLL)
//
BOOLEAN
ShowHTML(
CDHtmlDialog* pWnd,
int iResID
)
{
TCHAR szResourceURL[MAX_PATH];
if (NULL == pWnd)
{
return FALSE;
}
_stprintf(szResourceURL, _T("res://%s/%d"), <path of resource dll>, iResID);
pWnd->Navigate(szResourceURL);
pWnd->ShowWindow(SW_MAXIMIZE);
return TRUE;
}
Method 2:
BOOLEAN
ShowHTML(
CDHtmlDialog* pWnd,
IWebBrowser2 *pWebBrowser,
LPDISPATCH pDisp,
UINT uiResID
)
{
HRSRC hRes;
CString str;
HMODULE hModule;
HGLOBAL hGlobal;
HRESULT hResult;
WCHAR *pwszHtml;
DWORD dwcbResource;
LPCBYTE pcbyResource;
TCHAR szResName[MAX_PATH];
IUnknown *pIUnknownBrowser;
IUnknown *pIUnknownDispatch;
CComPtr<IDispatch> pDocDispatch;
TCHAR szModuleFileName[MAX_PATH];
CComPtr<IHTMLDocument2> pHTMLDoc2;
hResult = pWebBrowser->QueryInterface(IID_IUnknown, (void**)&pIUnknownBrowser);
if (FAILED(hResult))
{
AfxMessageBox(_T("QueryInterface for pIUnknownBrowser FAILED"));
return FALSE;
}
hResult = pDisp->QueryInterface(IID_IUnknown, (void**)&pIUnknownDispatch);
if (FAILED(hResult))
{
AfxMessageBox(_T("QueryInterface for pIUnknownDispatch FAILED"));
pIUnknownBrowser->Release();
return FALSE;
}
if (pIUnknownBrowser != pIUnknownDispatch)
{
//AfxMessageBox(_T("Mismatch"));
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
StringCchCopy(szModuleFileName, ARRAY_SIZE(szModuleFileName), PATH_FSCRNRES);
StringCchCat(szModuleFileName, ARRAY_SIZE(szModuleFileName), DLL_FSCRNRES);
hModule = LoadLibraryEx(szModuleFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (NULL == hModule)
{
str.Format(_T("LoadLibraryEx FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
StringCchPrintf(szResName, ARRAY_SIZE(szResName), _T("#%u"), uiResID);
hRes = ::FindResource(hModule, szResName, RT_HTML);
if (NULL == hRes)
{
str.Format(_T("FindResource FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
hGlobal = ::LoadResource(hModule, hRes);
if (NULL == hGlobal)
{
str.Format(_T("LoadResource FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
pcbyResource = (LPCBYTE)LockResource(hGlobal);
if (NULL == pcbyResource)
{
str.Format(_T("LockResource FAILED."));
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
dwcbResource = SizeofResource(hModule, hRes);
if (0 == dwcbResource)
{
str.Format(_T("SizeofResource FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
pwszHtml = (wchar_t*)malloc(dwcbResource / sizeof(wchar_t));
if (NULL == pwszHtml)
{
AfxMessageBox(_T("malloc FAILED."));
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
WriteHTML(pWebBrowser, pcbyResource, dwcbResource);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
pWnd->ShowWindow(SW_MAXIMIZE);
return TRUE;
}
void
WriteHTML(
IWebBrowser2 *pWebBrowser,
LPCBYTE pbyHtml,
UINT uicbHtml
)
{
int iRet;
LPWSTR pwszHtml;
if (NULL == pbyHtml || 0 == uicbHtml)
{
return;
}
IDispatch* pHtmlDoc;
pWebBrowser->get_Document(&pHtmlDoc);
if (!pHtmlDoc)
{
return;
}
CComPtr<IHTMLDocument2> doc2;
doc2.Attach((IHTMLDocument2*)pHtmlDoc);
if (!doc2)
{
return;
}
//
// Creates a new one-dimensional array.
//
SAFEARRAY* psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (!psaStrings)
{
return;
}
pwszHtml = (LPWSTR)malloc(sizeof(wchar_t) * uicbHtml);
if (NULL == pwszHtml)
{
return;
}
memset(pwszHtml, 0, (sizeof(wchar_t) * uicbHtml));
for (UINT uiLoop = 0; uiLoop < uicbHtml; uiLoop++)
{
iRet = mbtowc(&(pwszHtml[uiLoop]), (const char*)&(pbyHtml[uiLoop]), MB_CUR_MAX);
if ((0 == iRet) || (-1 == iRet))
{
return;
}
}
BSTR bstr = SysAllocStringLen((OLECHAR*)pwszHtml, uicbHtml);
if (bstr)
{
VARIANT* param;
HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
if (SUCCEEDED(hr))
{
param->vt = VT_BSTR;
param->bstrVal = bstr;
hr = SafeArrayUnaccessData(psaStrings);
if (SUCCEEDED(hr))
{
doc2->write(psaStrings);
doc2->close();
}
}
}
//
// SafeArrayDestroy calls SysFreeString for each BSTR!
//
if (psaStrings)
{
SafeArrayDestroy(psaStrings);
}
}
The HTML file is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML Strict//EN">
<html>
<head>
<script src="res:/JS/IDR_JS_MIN" type="text/javascript"></script>
<script src="res:/JS/IDR_JS_MOUSEWHEEL" type="text/javascript"></script>
<script>
$(function()
{
$(".scroller").mousewheel(function(event, delta)
{
this.scrollLeft -= (delta * 30);
event.preventDefault();
});
});
</script>
<style>
body {
margin: 0
}
.box {
width: 100%;
background-color: white;
scrollbar-face-color : #EBEBEB;
scrollbar-highlight-color : #CCC;
scrollbar-3dlight-color : #CCC;
scrollbar-darkshadow-color : #CCC;
scrollbar-arrow-color : #CCC;
scrollbar-shadow-color : #CCC;
scrollbar-track-color: #FFF;
}
.scroller {
overflow: auto;
*overflow-x: auto;
*overflow-y: hidden;
*padding-bottom: 50px;
padding-top: 50px
}
.combo {
white-space: nowrap;
width: 320px;
float: left;
height: 150px;
background: #ddd
}
</style>
</head>
<body>
<div class="box">
<div class="scroller">
<table>
<tr>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
</tr>
</table>
</div>
</div>
</body>
</html>
The HTML and and JQuery are working fine using the 1st method. However for the 2nd method, I get a JQuery error. Is there something that is wrong with what I have done. Thanks in advance for any help.
Continue reading...
I have a simple HTML file (and associated JavaScript) in an MFC resource DLL. I want to load this resource DLL and display the HTML file in CDHTMLDialog in my MFC application.
I have used two methods as follows:
Method 1:
//
// CDHtmlDialog* pWnd: Dialog on which HTML is to be shown
// int iResID : Resource ID of HTML (in resource DLL)
//
BOOLEAN
ShowHTML(
CDHtmlDialog* pWnd,
int iResID
)
{
TCHAR szResourceURL[MAX_PATH];
if (NULL == pWnd)
{
return FALSE;
}
_stprintf(szResourceURL, _T("res://%s/%d"), <path of resource dll>, iResID);
pWnd->Navigate(szResourceURL);
pWnd->ShowWindow(SW_MAXIMIZE);
return TRUE;
}
Method 2:
BOOLEAN
ShowHTML(
CDHtmlDialog* pWnd,
IWebBrowser2 *pWebBrowser,
LPDISPATCH pDisp,
UINT uiResID
)
{
HRSRC hRes;
CString str;
HMODULE hModule;
HGLOBAL hGlobal;
HRESULT hResult;
WCHAR *pwszHtml;
DWORD dwcbResource;
LPCBYTE pcbyResource;
TCHAR szResName[MAX_PATH];
IUnknown *pIUnknownBrowser;
IUnknown *pIUnknownDispatch;
CComPtr<IDispatch> pDocDispatch;
TCHAR szModuleFileName[MAX_PATH];
CComPtr<IHTMLDocument2> pHTMLDoc2;
hResult = pWebBrowser->QueryInterface(IID_IUnknown, (void**)&pIUnknownBrowser);
if (FAILED(hResult))
{
AfxMessageBox(_T("QueryInterface for pIUnknownBrowser FAILED"));
return FALSE;
}
hResult = pDisp->QueryInterface(IID_IUnknown, (void**)&pIUnknownDispatch);
if (FAILED(hResult))
{
AfxMessageBox(_T("QueryInterface for pIUnknownDispatch FAILED"));
pIUnknownBrowser->Release();
return FALSE;
}
if (pIUnknownBrowser != pIUnknownDispatch)
{
//AfxMessageBox(_T("Mismatch"));
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
StringCchCopy(szModuleFileName, ARRAY_SIZE(szModuleFileName), PATH_FSCRNRES);
StringCchCat(szModuleFileName, ARRAY_SIZE(szModuleFileName), DLL_FSCRNRES);
hModule = LoadLibraryEx(szModuleFileName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (NULL == hModule)
{
str.Format(_T("LoadLibraryEx FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
StringCchPrintf(szResName, ARRAY_SIZE(szResName), _T("#%u"), uiResID);
hRes = ::FindResource(hModule, szResName, RT_HTML);
if (NULL == hRes)
{
str.Format(_T("FindResource FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
hGlobal = ::LoadResource(hModule, hRes);
if (NULL == hGlobal)
{
str.Format(_T("LoadResource FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
pcbyResource = (LPCBYTE)LockResource(hGlobal);
if (NULL == pcbyResource)
{
str.Format(_T("LockResource FAILED."));
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
dwcbResource = SizeofResource(hModule, hRes);
if (0 == dwcbResource)
{
str.Format(_T("SizeofResource FAILED. ErrCode(%u)."), GetLastError());
AfxMessageBox(str);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
pwszHtml = (wchar_t*)malloc(dwcbResource / sizeof(wchar_t));
if (NULL == pwszHtml)
{
AfxMessageBox(_T("malloc FAILED."));
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
return FALSE;
}
WriteHTML(pWebBrowser, pcbyResource, dwcbResource);
FreeLibrary(hModule);
pIUnknownBrowser->Release();
pIUnknownDispatch->Release();
pWnd->ShowWindow(SW_MAXIMIZE);
return TRUE;
}
void
WriteHTML(
IWebBrowser2 *pWebBrowser,
LPCBYTE pbyHtml,
UINT uicbHtml
)
{
int iRet;
LPWSTR pwszHtml;
if (NULL == pbyHtml || 0 == uicbHtml)
{
return;
}
IDispatch* pHtmlDoc;
pWebBrowser->get_Document(&pHtmlDoc);
if (!pHtmlDoc)
{
return;
}
CComPtr<IHTMLDocument2> doc2;
doc2.Attach((IHTMLDocument2*)pHtmlDoc);
if (!doc2)
{
return;
}
//
// Creates a new one-dimensional array.
//
SAFEARRAY* psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (!psaStrings)
{
return;
}
pwszHtml = (LPWSTR)malloc(sizeof(wchar_t) * uicbHtml);
if (NULL == pwszHtml)
{
return;
}
memset(pwszHtml, 0, (sizeof(wchar_t) * uicbHtml));
for (UINT uiLoop = 0; uiLoop < uicbHtml; uiLoop++)
{
iRet = mbtowc(&(pwszHtml[uiLoop]), (const char*)&(pbyHtml[uiLoop]), MB_CUR_MAX);
if ((0 == iRet) || (-1 == iRet))
{
return;
}
}
BSTR bstr = SysAllocStringLen((OLECHAR*)pwszHtml, uicbHtml);
if (bstr)
{
VARIANT* param;
HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
if (SUCCEEDED(hr))
{
param->vt = VT_BSTR;
param->bstrVal = bstr;
hr = SafeArrayUnaccessData(psaStrings);
if (SUCCEEDED(hr))
{
doc2->write(psaStrings);
doc2->close();
}
}
}
//
// SafeArrayDestroy calls SysFreeString for each BSTR!
//
if (psaStrings)
{
SafeArrayDestroy(psaStrings);
}
}
The HTML file is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML Strict//EN">
<html>
<head>
<script src="res:/JS/IDR_JS_MIN" type="text/javascript"></script>
<script src="res:/JS/IDR_JS_MOUSEWHEEL" type="text/javascript"></script>
<script>
$(function()
{
$(".scroller").mousewheel(function(event, delta)
{
this.scrollLeft -= (delta * 30);
event.preventDefault();
});
});
</script>
<style>
body {
margin: 0
}
.box {
width: 100%;
background-color: white;
scrollbar-face-color : #EBEBEB;
scrollbar-highlight-color : #CCC;
scrollbar-3dlight-color : #CCC;
scrollbar-darkshadow-color : #CCC;
scrollbar-arrow-color : #CCC;
scrollbar-shadow-color : #CCC;
scrollbar-track-color: #FFF;
}
.scroller {
overflow: auto;
*overflow-x: auto;
*overflow-y: hidden;
*padding-bottom: 50px;
padding-top: 50px
}
.combo {
white-space: nowrap;
width: 320px;
float: left;
height: 150px;
background: #ddd
}
</style>
</head>
<body>
<div class="box">
<div class="scroller">
<table>
<tr>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
<td><div> <span class="combo">TAB </div></td>
</tr>
</table>
</div>
</div>
</body>
</html>
The HTML and and JQuery are working fine using the 1st method. However for the 2nd method, I get a JQuery error. Is there something that is wrong with what I have done. Thanks in advance for any help.
Continue reading...