MFC 加载程序、解析资源

//解析资源类型
BOOL CALLBACK EnumResTypeProc(HANDLE hModule, LPTSTR lpszType, LONG lParam)
{
 EnumResourceNames((HINSTANCE)hModule, lpszType, (ENUMRESNAMEPROC)EnumResNameProc,lParam);
 DWORD dType = 0;
 switch((DWORD)lpszType) {
 case RT_ACCELERATOR:
  dType = (DWORD)lpszType;
  break;
 case RT_ANICURSOR:
  dType = (DWORD)lpszType;
  break;
 case RT_ANIICON:
  dType = (DWORD)lpszType;
  break;
 case RT_DIALOG:
  dType = (DWORD)lpszType;
  break;
 case RT_FONT:
  dType = (DWORD)lpszType;
  break;
 case RT_FONTDIR:
  dType = (DWORD)lpszType;
  break;
 case RT_GROUP_CURSOR:
  dType = (DWORD)lpszType;
  break;
 case RT_GROUP_ICON:
  dType = (DWORD)lpszType;
  break;
 case RT_HTML:
  dType = (DWORD)lpszType;
  break;
 case RT_MENU:
  dType = (DWORD)lpszType;
  break;
 case RT_MESSAGETABLE:
  dType = (DWORD)lpszType;
  break;
 case RT_RCDATA:
  dType = (DWORD)lpszType;
  break;
 case RT_STRING:
  dType = (DWORD)lpszType;
  break;
 case RT_VERSION:
  dType = (DWORD)lpszType;
  break;
 case RT_TOOLBAR:
  dType = (DWORD)lpszType;
  break;
 case RT_CURSOR:
  dType = (DWORD)lpszType;
  break;
 case RT_ICON:
  dType = (DWORD)lpszType;
  break;
 case RT_BITMAP:
  dType = (DWORD)lpszType;
  break;
 default:
  break;
 }
 
 return true;
}
//////////////////////////////////////////////////////////////////////////
//解析资源名称
BOOL CALLBACK EnumResNameProc(HANDLE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG lParam)
{
 CString str;
 if ((ULONG)lpszName & 0xFFFF0000)str.Format(_T("%s"),lpszName);
 else
  str.Format(_T("%u"),(USHORT)lpszName);
 AfxMessageBox(str);
 DLGTEMPLATE* pTemplate;      //定义在内存中指向资源的指针
 HRSRC hRsrc = ::FindResource((HMODULE)hModule, lpszName, lpszType);
 ASSERT(hRsrc != NULL);        
 HGLOBAL hTemplate =::LoadResource((HMODULE)hModule, hRsrc);
 ASSERT(hTemplate != NULL);
 pTemplate = (DLGTEMPLATE*)::LockResource(hTemplate);
 DWORD resSize;
 resSize = SizeofResource((HMODULE)hModule, hRsrc);     //读资源大小
 DWORD dType = 0;
 switch((DWORD)lpszType) {
 case RT_ACCELERATOR:
  dType = (DWORD)lpszType;
  break;
 case RT_ANICURSOR:
  dType = (DWORD)lpszType;
  break;
 case RT_ANIICON:
  dType = (DWORD)lpszType;
  break;
 case RT_DIALOG:
  dType = (DWORD)lpszType;
  break;
 case RT_FONT:
  dType = (DWORD)lpszType;
  break;
 case RT_FONTDIR:
  dType = (DWORD)lpszType;
  break;
 case RT_GROUP_CURSOR:
  dType = (DWORD)lpszType;
  break;
 case RT_GROUP_ICON:
  dType = (DWORD)lpszType;
  break;
 case RT_HTML:
  dType = (DWORD)lpszType;
  break;
 case RT_MENU:
  dType = (DWORD)lpszType;
  break;
 case RT_MESSAGETABLE:
  dType = (DWORD)lpszType;
  break;
 case RT_RCDATA:
  dType = (DWORD)lpszType;
  break;
 case RT_STRING:
  dType = (DWORD)lpszType;
  break;
 case RT_VERSION:
  dType = (DWORD)lpszType;
  break;
 case RT_TOOLBAR:
  dType = (DWORD)lpszType;
  break;
 case RT_CURSOR:
  dType = (DWORD)lpszType;
  break;
 case RT_ICON:
  dType = (DWORD)lpszType;
  break;
 case RT_BITMAP:
  dType = (DWORD)lpszType;
  break;
 default:
  break;
 }
 return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//加载调用程序
bool LoadApp(CString csAppPath)
{
 TCHAR* pFilePath = csAppPath.GetBuffer(0);
 HINSTANCE hInst = LoadLibrary(pFilePath);
 int e = GetLastError();
 if (hInst == NULL)
 {
  int ret = GetLastError();
  FreeLibrary(hInst);
  return false;
 }
 ASSERT(hInst != NULL);
 EnumResourceTypes((HMODULE)hInst, (ENUMRESTYPEPROC)EnumResTypeProc, 0);
 FreeLibrary(hInst);
 return true;
}

你可能感兴趣的:(MFC 加载程序、解析资源)