1、插入资源的方法
insert-->resource-->import,在出现的对话框中选择一个要作为资源的文件,接着在出现的对话框中Resource type:下面的框中随便自己输入一个串(如123),查找资源的时候会用到,点击OK。
编译一下工程,资源插入完毕(在resource.h文件中会增加一条#define IDR_1231 130,在rc文件中会增加一条IDR_1231 123 DISCARDABLE "所选的文件名字")。
2、EXE中查找资源并释放
char rcFileName [ 256 ]; char rcFilePath [ 1024 ];
ZeroMemory ( rcFileName , 256 ); ZeroMemory ( rcFilePath , 1024 );
strncpy ( rcType , "123" , strlen ( "123" ) ); strncpy ( rcFileName , "所选的文件名" , strlen ( "所选的文件名" ) );
HMODULE hInstance = GetModuleHandle ( NULL );
HRSRC hRes = NULL; //resource handle HGLOBAL hgpt = NULL; //resource pointer LPVOID lpBuff = NULL; //resource buffer pointer DWORD rcSize = 0; //resource size DWORD dwByte; //byte size had been write HANDLE hFile = INVALID_HANDLE_VALUE; //file to write
hRes = ::FindResource ( hInstance , MAKEINTRESOURCE ( rcID ) , rcType ); if ( NULL == hRes ) { return FALSE; }
hgpt = ::LoadResource ( hInstance , hRes ); if ( NULL == hgpt ) { return FALSE; }
rcSize = ::SizeofResource ( hInstance , hRes ); lpBuff = ::LockResource ( hgpt );
//now i will read the resource and write it to an file
strcat ( rcFilePath , "c://windows//system32//" ); // 不同情况自己指定 strcat ( rcFilePath , rcFileName ); hFile = CreateFile ( rcFilePath , GENERIC_WRITE , 0 , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL );
if ( INVALID_HANDLE_VALUE == hFile ) { CloseHandle ( hFile ); return FALSE; }
WriteFile ( hFile , lpBuff , rcSize , &dwByte , NULL ); CloseHandle ( hFile ); if ( dwByte != rcSize ) return FALSE;
释放资源完毕
3、DLL中查找资源并释放
char rcFileName [ 256 ]; char rcFilePath [ 1024 ];
ZeroMemory ( rcFileName , 256 ); ZeroMemory ( rcFilePath , 1024 );
strncpy ( rcType , "123" , strlen ( "123" ) ); strncpy ( rcFileName , "所选的文件名" , strlen ( "所选的文件名" ) );
HMODULE hInstance = GetModuleHandle ( "abc.dll" );// abc.dll就是带有资源的dll,一定不能填NULL,那样会找不到资源的
HRSRC hRes = NULL; //resource handle HGLOBAL hgpt = NULL; //resource pointer LPVOID lpBuff = NULL; //resource buffer pointer DWORD rcSize = 0; //resource size DWORD dwByte; //byte size had been write HANDLE hFile = INVALID_HANDLE_VALUE; //file to write
hRes = ::FindResource ( hInstance/*不能填NULL*/ , MAKEINTRESOURCE ( rcID ) , rcType ); if ( NULL == hRes ) { return FALSE; }
hgpt = ::LoadResource ( hInstance/*不能填NULL*/ , hRes ); if ( NULL == hgpt ) { return FALSE; }
rcSize = ::SizeofResource ( hInstance/*不能填NULL*/ , hRes ); lpBuff = ::LockResource ( hgpt );
//now i will read the resource and write it to an file
strcat ( rcFilePath , "c://windows//system32//" ); // 不同情况自己指定 strcat ( rcFilePath , rcFileName ); hFile = CreateFile ( rcFilePath , GENERIC_WRITE , 0 , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL );
if ( INVALID_HANDLE_VALUE == hFile ) { CloseHandle ( hFile ); return FALSE; }
WriteFile ( hFile , lpBuff , rcSize , &dwByte , NULL ); CloseHandle ( hFile ); if ( dwByte != rcSize ) return FALSE;
释放资源完毕
这种封装常见于dll中,一个大型的软件,可以将一些配置文件,其他不希望用户看到的文件封装起来,比如某个Exe。
用户通过查找资源,获取资源的指针和大小就可以去读到exe资源,并把这exe保存到硬盘临时文件,然后使用ShellExecute或者CreateProcess调用。便达到了封装的目的。
转载于:https://www.cnblogs.com/Floki/p/7490049.html