//////////////////////////////////////////////////////////// // ExportAviResources.cpp - // // Copyright (C) Alax.Info, 2006-2007 // http://alax.info // // A permission to use the source code is granted as long as reference to // source website http://alax.info is retained. #include "stdafx.h" #include #include class CExportAviResourcesModule { private: CStringReplacements m_ExportFileNameReplacements; CString m_sExportFileNameFormat; static CString StringFromResourceIdentifier(LPCTSTR pszIdentifier) { if(IS_INTRESOURCE(pszIdentifier)) return AtlFormatString(_T("#%d"), (USHORT) pszIdentifier); return pszIdentifier; } BOOL EnumerateModuleResourceLanguage(HMODULE hModule, LPCTSTR pszType, LPCTSTR pszName, WORD nLanguage) { HRSRC hResource = FindResourceEx(hModule, pszType, pszName, nLanguage); __E(hResource); VOID* pvData = LockResource(LoadResource(hModule, hResource)); ULONG nDataSize = SizeofResource(hModule, hResource); m_ExportFileNameReplacements.AddReplacement(_T("Type"), StringFromResourceIdentifier(pszType)); m_ExportFileNameReplacements.AddReplacement(_T("Name"), StringFromResourceIdentifier(pszName)); m_ExportFileNameReplacements.AddReplacement(_T("Language"), AtlFormatString(_T("%04X"), nLanguage)); CAtlFile File; __C(File.Create(m_ExportFileNameReplacements.Format(m_sExportFileNameFormat), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS)); __C(File.Write(pvData, (DWORD) nDataSize)); return TRUE; } static BOOL CALLBACK EnumerateModuleResourceLanguage(HMODULE hModule, LPCTSTR pszType, LPCTSTR pszName, WORD nLanguage, LONG_PTR lParam) throw() { _A(_tcscmp(pszType, _T("AVI")) == 0); _ATLTRY { if(!((CExportAviResourcesModule*) lParam)->EnumerateModuleResourceLanguage(hModule, pszType, pszName, nLanguage)) return FALSE; } _ATLCATCHALL() { _tprintf(_T("Error processing resource type %s, name %s, language 0x%x\n"), StringFromResourceIdentifier(pszType), StringFromResourceIdentifier(pszName), nLanguage); return FALSE; } return TRUE; } BOOL EnumerateModuleResourceName(HMODULE hModule, LPCTSTR pszType, LPTSTR pszName) { _W(EnumResourceLanguages(hModule, pszType, pszName, EnumerateModuleResourceLanguage, (LONG_PTR) this)); return TRUE; } static BOOL CALLBACK EnumerateModuleResourceName(HMODULE hModule, LPCTSTR pszType, LPTSTR pszName, LONG_PTR lParam) throw() { _A(_tcscmp(pszType, _T("AVI")) == 0); _ATLTRY { if(!((CExportAviResourcesModule*) lParam)->EnumerateModuleResourceName(hModule, pszType, pszName)) return FALSE; } _ATLCATCHALL() { _tprintf(_T("Error processing resource type %s, name %s\n"), StringFromResourceIdentifier(pszType), StringFromResourceIdentifier(pszName)); return FALSE; } return TRUE; } public: // CExportAviResourcesModule VOID SetExportFileNameFormat(LPCTSTR pszExportFileNameFormat) { m_sExportFileNameFormat = pszExportFileNameFormat; } VOID Process(const CPath sModulePath) { { CPath sModuleName = (LPCTSTR) sModulePath + sModulePath.FindFileName(); sModuleName.RemoveExtension(); m_ExportFileNameReplacements.AddReplacement(_T("ModuleName"), (LPCTSTR) sModuleName); } HMODULE hModule = LoadLibraryEx(sModulePath, NULL, LOAD_LIBRARY_AS_DATAFILE); _ATLTRY { _W(EnumResourceNames(hModule, _T("AVI"), EnumerateModuleResourceName, (LONG_PTR) this)); } _ATLCATCHALL() { _W(FreeLibrary(hModule)); _ATLRETHROW; } _W(FreeLibrary(hModule)); } }; int _tmain(int argc, _TCHAR* argv[]) { _ATLTRY { if(argc < 2) { _tprintf(_T("Syntax: ExportAviResources []\n")); return 1; } CExportAviResourcesModule Module; static LPCTSTR g_pszDefaultExportFileNameFormat = _T("$(ModuleName)-$(Name)-$(Language).avi"); Module.SetExportFileNameFormat((argc > 2) ? argv[2] : g_pszDefaultExportFileNameFormat); Module.Process(argv[1]); } _ATLCATCH(Exception) { _tprintf(_T("Fatal error 0x%08x, exiting..."), Exception); } _ATLCATCHALL() { _tprintf(_T("Fatal error, exiting...")); } return 0; }