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;
释放资源完毕
有关资源操作的总结完毕,具体的api可以查看msdn