Alax.Info ATL/WTL Utilities is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

/trunkExportAviResources
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
////////////////////////////////////////////////////////////
// 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 <atlpath.h>
#include <atlfile.h>

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 <filename> [<avi-filename-format>]\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;
}
Ajax-loader Loading, please wait...