我有一个用于数据挖掘的BHO(浏览器帮助对象)。许多低级DOM操作被委托给javascript。直到现在我的应用程序从应用程序安装目录中提取它;但现在由于一些客户端的要求,我必须将JS捆绑在BHO dll本身。
现在我的问题是我还没弄明白如何在我的资源文件(a.k.a rc文件)中添加JS文件。我尝试添加HTML文件(visual studio 2008 IDE支持)。但是当我做这样的事情时,我找不到html资源(g_hInstance
是我BHO的HINSTANCE
):
if(!g_hInstance)
{
::MessageBox(NULL, L"Fail 0", L"", MB_OK);
return;
}
HRSRC hRsrc = FindResource( g_hInstance, MAKEINTRESOURCE(IDR_JS), RT_HTML );
if(!hRsrc)
{
::MessageBox(NULL, L"no point", L"", MB_OK);
return;
}
DWORD dwFSz = SizeofResource( g_hInstance, hRsrc );
HGLOBAL hHtml = LoadResource( g_hInstance, hRsrc );
LPVOID pHtml = LockResource( hHtml );
HANDLE hFHtm = CreateFile( L"c:\\temp\\Test1.htm", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
DWORD dwWr;
WriteFile( hFHtm, pHtml, dwFSz, &dwWr, NULL );
CloseHandle( hFHtm );
UnlockResource( hHtml );
ShellExecute( NULL, L"open", L"c:\\temp\\Test1.htm", NULL, NULL, 0 );
My questions are:
.rc
文件中允许使用html,那么为什么FindResource( g_hInstance, MAKEINTRESOURCE(IDR_JS), RT_HTML );
总是给我NULL?谢谢,
答案 0 :(得分:1)
搞定了。接下来的步骤:
Add Resource...
custom
资源按钮。RT_MYSCRIPT
<强> Code to access your resource
强>
void CTest::ReadResource()
{
if (NULL != g_hInstance) // g_hInstance is HINSTANCE of my DLL
{
HRSRC hRes = FindResource(g_hInstance, MAKEINTRESOURCE(IDR_SCRIPT), _T("RT_MYSCRIPT"));
if (NULL != hRes)
{
HGLOBAL hgbl = LoadResource(g_hInstance, hRes);
void * pScript = LockResource(hgbl);
UINT32 cbScript = SizeofResource(g_hInstance, hRes);
if(pScript)
{
// Do something
}
// pScript now points to the contents of your your .script file
// and cbScript is its size in bytes
}else
{
::MessageBox(NULL, L"Failed", L"", MB_OK);
}
/*
Don't free the library until you are done. And do it only if you
are loading the script from a resource dll or some other external
source !! Note: Also do a good amount of exception checking in your code!!
*/
// FreeLibrary(hMod);
}
}
注意:
我的问题是包括并从DLL访问我的javascript文件。我已经解决了。 HTML问题仍然存在,但与我的问题无关。如果我将来有机会使用它,我会更新它。