`
mybwu_com
  • 浏览: 180170 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

在EXE和DLL中,FindResource的区别

 
阅读更多

以下的代码在EXE中,执行无误。可是在DLL中FindResource返回的值就为0了,我用::GetLastError(),得出的值是1813,也就是取不到资源,这是为什么?
BOOL ExtractFile(char *szZlibFile,DWORD dwRID)
{
HANDLE hFile = NULL ;
HRSRC hRsrc = NULL ;
HGLOBAL hGlobal = NULL ;

DWORD dwResSize = 0, dwBytesWrite = 0, dwBytesRead = 0;
PVOID pRsrc = NULL, pConfigEncrypt = NULL;
hRsrc = FindResource( NULL, MAKEINTRESOURCE(dwRID), "ZLIB");
if(hRsrc == NULL)
{
MyDbgPrint("[ExtractFile] Locate Resource Error: %d\n", GetLastError());
return FALSE;
}

dwResSize = SizeofResource(NULL, hRsrc);

hGlobal = LoadResource(NULL, hRsrc);
if(hGlobal == NULL)
{
MyDbgPrint("[ExtractFile] Load Resource Error: %d\n", GetLastError());
return FALSE;
}

pRsrc = LockResource(hGlobal);

hFile = CreateFile( szZlibFile , GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
MyDbgPrint("[ExtractFile] Create %s File Error: %d\n", szZlibFile , GetLastError());
return FALSE;
}

if( WriteFile( hFile, pRsrc, dwResSize, &dwBytesWrite, NULL) == FALSE)
{
MyDbgPrint("[ExtractFile] Write %s File Error: %d\n", szZlibFile , GetLastError());
return FALSE;
}
CloseHandle(hFile);
MyDbgPrint("[ExtractFile] Write %s File Success\n",szZlibFile);
return TRUE;

}
问题出在FindResource的第一个参数取得的HINSTANCE句柄是EXE的HINSTANCE句柄,自然在其中也找不到在DLL中引入的ZLIB这个位图ID了.同时SizeofResource,LoadResource中的第一个参数也应该是DLL的句柄.

解决方法如下:
BOOL ExtractFile(char *szZlibFile,DWORD dwRID)
{
HANDLE hFile = NULL ;
HRSRC hRsrc = NULL ;
HGLOBAL hGlobal = NULL ;

DWORD dwResSize = 0, dwBytesWrite = 0, dwBytesRead = 0;
PVOID pRsrc = NULL, pConfigEncrypt = NULL;
HINSTANCE hInst = GetModuleHandle( dll的路径 );
hRsrc = FindResource( hInst , MAKEINTRESOURCE(dwRID), (LPCTSTR)"ZLIB");
if(hRsrc == NULL)
{
return FALSE;
}

dwResSize = SizeofResource( hInst , hRsrc);

hGlobal = LoadResource( hInst , hRsrc);
if(hGlobal == NULL)
{
return FALSE;
}

pRsrc = LockResource(hGlobal);

hFile = CreateFile( szZlibFile , GENERIC_ALL, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}

if( WriteFile( hFile, pRsrc, dwResSize, &dwBytesWrite, NULL) == FALSE)
{
return FALSE;
}
CloseHandle(hFile);
return TRUE;
}



testcs_dn解决方案记录:

定义全局变量:

HINSTANCEg_hInstance;

CString g_szDllPath = "";

DLL构造函数中初始化全局变量:

DEFAULTSKIN_API CDefaultSkinApp::CDefaultSkinApp() // : CWinApp(NULL)
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	InitSkinLib(_T("blue"));
	CString appPath;
	::GetModuleFileName(AfxGetApp()->m_hInstance,  appPath.GetBuffer(MAX_PATH), MAX_PATH);
	appPath.ReleaseBuffer();
	int pos = appPath.ReverseFind('\\');
	if (pos != -1){
		g_szDllPath.Format(_T("%s%s"), appPath.Left(pos + 1), _T("DefaultSkin.dll"));
	}
	if (!g_szDllPath.IsEmpty()){
		g_hInstance = GetModuleHandle(g_szDllPath);
	}
}

FindResource中使用g_hInstance

OK,问题解决!

突然在搜索结果中发现:AFX_MANAGE_STATE(AfxGetStaticModuleState());

AFX_MANAGE_STATE(AfxGetStaticModuleState());
HINSTANCE hInst = AfxGetResourceHandle();//g_hInstance; //AfxGetInstanceHandle(); //
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nResID), _T("PNG"));

这才是上好的解决之道

分享到:
评论

相关推荐

    visual c++用代码 提取EXE或DLL中的图标ico

    vc提取EXE或DLL中的资源,比如 图标

    delphi 在内存中直接运行EXE类型的资源文件

    ResourceLocation := FindResource(HInstance, 'myexe', RT_RCDATA); if ResourceLocation <> 0 then begin Size := SizeofResource(HInstance, ResourceLocation); if Size <> 0 then begin ResDataHandle := Load...

    Microsoft Visual Studio

    Common\Graphics\bitmaps\tlbr_w95\find.bmp Common\Graphics\bitmaps\tlbr_w95\freeform.bmp Common\Graphics\bitmaps\tlbr_w95\front.bmp Common\Graphics\bitmaps\tlbr_w95\group.bmp Common\Graphics\bitmaps...

    程序中资源的操作示例演示

    本示例简单演示了如何对一个exe文件的资源操作,没有测试过dll和ocx,只在vista上测试过,其它系统上没有测试过 resattach_detech: 使用vs2008编译,使用了缺省的unicode编码。 作用:把选定的资源加入到指定的exe...

    Resource Tuner v2.01 CracKed By Hmily

    The usage is very simple, just run the program and then select the EXE or DLL file to read the data from. Once you try it, we think you will find it hard to go back to other resource hacking ...

    Visual C++ 编程资源大全(英文源码 DLL)

    1,01.zip Dialogs in DLL 在DLL中实现对话框(5KB)<END><br>2,02.zip Export dialogs in MFC Extension DLLs 在MFC扩充DLL中输出对话框(12KB)<END><br>3,03.zip Remapping resource script ID's ...

    Android的JSON利用案例源码

    Android的JSON利用案例源码 Android的JSON利用案例源码 Android的JSON利用案例源码 Android的JSON利用案例源码 我作为培训生在培训时候,接触和自己做的源码,分享一下,为了高薪...把DLL以资源的形式存放在EXE文件中

    Delphi7.1 Update

    * Using the Delphi 7 version of midas.dll to open an XML file that was saved with the Delphi 6 version of midas.dll results in an illegal operation. * Incorrect filter expression parsing occurs when ...

    实例md5加密

    present in a Dll or Exe. This problem may be overcome by including the library's .rc file into the parent project's .rc file. ////////////////////////////////////////////////////////////////////////...

    微软内部资料-SQL性能优化2

     Windows 2000 Resource Kit companion CD-ROM documentation. Chapter 15: Overview of Performance Monitoring  Inside Microsoft Windows 2000, Third Edition, David A. Solomon and Mark E. Russinovich ...

    蓝屏查看,主要用于查找蓝屏错误

    the crash, and find all drivers/modules that might be involved in the crash. * BlueScreenView also allows you to work with another instance of Windows, simply by choosing the right minidump folder ...

    Build Report Tool 3.0.19.rar

    In Windows, your resulting .exe file is also not considered in the Build Report as that is considered a "boilerplate" resource. The contents of your .exe file is largely standard among all Windows ...

    ehlib_vcl_src_9_3.26

    If you have executable installation program (for example, EhLibSetupD7Eval.exe) then you only need to run program and follow installation process. Setup automatically writes all units in necessary ...

    EhLib 9.1.024

    If you have executable installation program (for example, EhLibSetupD7Eval.exe) then you only need to run program and follow installation process. Setup automatically writes all units in necessary ...

    VB.NET Developer's Guide(4574).pdf

    Contents xvii Web Forms 133 Intellisense 134 Customizing the IDE 135 Customizing the Code Editor 135 Customizing Shortcut Keys ...Customizing the Toolbars ...Exercise 3.4 Adding a New Toolbar to ...

    ICS delphixe10源码版

    .\Samples\delphi\BroswerDemo\Resources Resource file, web pages and movie linked into browser demo .\Samples\delphi\FtpDemos Delphi Win32/Win64 FTP sample applications (all Delphi versions) .\Samples\...

    新思微POS连锁超市管理系统(delphi源码控件)

    │ 数据库备份、恢复系统.exe │ └─后台程序 │ brcc.bat │ Chat.ini │ DatBase.rc │ datbase.RES │ EMOTES.EMT │ hb1.~dfm │ hb1.~pa │ hb1.~pas │ HB1.DCU │ hb1.dfm │ hb1.pas │ kcgz1...

    一个win32下的ARM开源编译器

    fasmarm follows this principle: Find a way to encode it, the smaller the better What this means is that even though you may write a particular opcocde, fasmarm may assemble another as a replacement. ...

    Diskeeper 2008 v12.0.759.0

    manual and resource-intensive process. Frag Shield抯 MFT fragmentation prevention technology is now fully automatic and re-written to operate invisibly using InvisiTasking technology. o ...

Global site tag (gtag.js) - Google Analytics