将文件作为资源放入VC工程中,在运行时从EXE文件中提取(释放)出这个文件。

来源:互联网 发布:浙江铁笼沉尸 知乎 编辑:程序博客网 时间:2024/06/05 09:32

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:

  1. BOOL Res2File( LPCTSTR lpName, LPCTSTR lpType, LPCTSTR filename )   
  2. {   
  3.  HRSRC hRes = ::FindResource( NULL, lpName, lpType );   
  4.  HGLOBAL gl =::LoadResource( NULL, hRes );   
  5.  LPVOID lp = ::LockResource( gl );   
  6.  HANDLE fp = ::CreateFile( filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL   
  7.   
  8. );   
  9.  if( fp == INVALID_HANDLE_VALUE )   
  10.  return FALSE;   
  11.   
  12.  DWORD a;   
  13.  if( !::WriteFile( fp, lp, SizeofResource( NULL, hRes ), &a, NULL ) )   
  14.  return FALSE;   
  15.   
  16.  CloseHandle( fp );   
  17.  FreeResource( gl );   
  18.  return TRUE;   
  19. }  

 

3.
Use this function such as this format:
Res2File( MAKEINTRESOURCE(ID_RES_DATA), "RES_DATA", "C://FontRes.TTF" );

原创粉丝点击