天天看點

如何将自己的檔案作為資源檔案放入VC工程中?如何在運作時,從EXE檔案中提取(釋放)出這個檔案?

1. In VC IDE,add new resource type such as "RES_DATA",then add your file to the project

resource as the new type.

2. Fetch your file from executable file at runtime,can use follow function:

BOOL Res2File( LPCTSTR lpName, LPCTSTR lpType, LPCTSTR filename )

{

 HRSRC hRes = ::FindResource( NULL, lpName, lpType );

 HGLOBAL gl =::LoadResource( NULL, hRes );

 LPVOID lp = ::LockResource( gl );

 HANDLE fp = ::CreateFile( filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL

);

 if( fp == INVALID_HANDLE_VALUE )

 return FALSE;

 DWORD a;

 if( !::WriteFile( fp, lp, SizeofResource( NULL, hRes ), &a, NULL ) )

 return FALSE;

 CloseHandle( fp );

 FreeResource( gl );

 return TRUE;

}

Use this function such as this format:

Res2File( MAKEINTRESOURCE(ID_RES_DATA), "RES_DATA", "C://FontRes.TTF" );