1+ #include " Win32ResourceGenerator.h"
2+ #include < stdio.h>
3+ #include < stdlib.h>
4+ #include < string>
5+ #include < stdlib.h>
6+
7+ #include " common.h"
8+ #include " cppencoder.h"
9+ #include " stringfunc.h"
10+ #include " filesystemfunc.h"
11+
12+ using namespace stringfunc ;
13+ using namespace filesystem ;
14+
15+ namespace bin2cpp
16+ {
17+ Win32ResourceGenerator::Win32ResourceGenerator ()
18+ {
19+ }
20+
21+ Win32ResourceGenerator::~Win32ResourceGenerator ()
22+ {
23+ }
24+
25+ const char * Win32ResourceGenerator::getName () const
26+ {
27+ return " win32" ;
28+ }
29+
30+ bool Win32ResourceGenerator::createCppSourceFile (const char * iCppFilePath)
31+ {
32+ bool resourceFileSuccess = createResourceFile (iCppFilePath);
33+ if (!resourceFileSuccess)
34+ return false ;
35+
36+ // check if input file exists
37+ FILE * input = fopen (mInputFile .c_str (), " rb" );
38+ if (!input)
39+ return false ;
40+
41+ // Uppercase function identifier
42+ std::string functionIdentifier = capitalizeFirstCharacter (mFunctionIdentifier );
43+
44+ // Build header and cpp file path
45+ std::string headerPath = getHeaderFilePath (iCppFilePath);
46+ std::string cppPath = iCppFilePath;
47+ std::string headerFilename = getFilename (headerPath.c_str ());
48+ std::string cppFilename = getFilename (iCppFilePath);
49+
50+ // create cpp file
51+ FILE * cpp = fopen (cppPath.c_str (), " w" );
52+ if (!cpp)
53+ {
54+ fclose (input);
55+ return false ;
56+ }
57+
58+ // determine file properties
59+ // long fileSize = getFileSize(input);
60+ std::string filename = getFilename (mInputFile .c_str ());
61+
62+ // Build class name
63+ std::string className;
64+ className.append (functionIdentifier.c_str ());
65+ className.append (" File" );
66+
67+ // Build function
68+ std::string getterFunctionName = getGetterFunctionName ();
69+
70+ // write cpp file heading
71+ fprintf (cpp, " %s" , getHeaderTemplate ().c_str ());
72+ fprintf (cpp, " #include \" %s\"\n " , headerFilename.c_str () );
73+ fprintf (cpp, " #include <stdio.h> //for FILE\n " );
74+ fprintf (cpp, " #include <stdint.h>\n " );
75+ fprintf (cpp, " \n " );
76+ fprintf (cpp, " #ifndef WIN32_LEAN_AND_MEAN\n " );
77+ fprintf (cpp, " #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers\n " );
78+ fprintf (cpp, " #endif\n " );
79+ fprintf (cpp, " #include <windows.h>\n " );
80+ fprintf (cpp, " \n " );
81+ fprintf (cpp, " #include <psapi.h> //for EnumProcessModules()\n " );
82+ fprintf (cpp, " #pragma comment( lib, \" psapi.lib\" )\n " );
83+ fprintf (cpp, " \n " );
84+
85+ fprintf (cpp, " namespace %s\n " , mNamespace .c_str ());
86+ fprintf (cpp, " {\n " );
87+ fprintf (cpp, " class %s : public virtual %s::%s\n " , className.c_str (), mNamespace .c_str (), mBaseClass .c_str ());
88+ fprintf (cpp, " {\n " );
89+ fprintf (cpp, " public:\n " );
90+ fprintf (cpp, " %s() :\n " , className.c_str ());
91+ fprintf (cpp, " hProcess(NULL),\n " );
92+ fprintf (cpp, " hModule(NULL),\n " );
93+ fprintf (cpp, " hResourceInfoBlock(NULL),\n " );
94+ fprintf (cpp, " hResHandle(NULL),\n " );
95+ fprintf (cpp, " mBufferSize(0),\n " );
96+ fprintf (cpp, " mBuffer(NULL)\n " );
97+ fprintf (cpp, " {\n " );
98+ fprintf (cpp, " loadResource();\n " );
99+ fprintf (cpp, " }\n " );
100+ fprintf (cpp, " ~%s() { unloadResource(); }\n " , className.c_str ());
101+ fprintf (cpp, " virtual size_t getSize() const { return mBufferSize; }\n " );
102+ fprintf (cpp, " virtual const char * getFilename() const { return \" %s\" ; }\n " , getFilename (mInputFile .c_str ()).c_str ());
103+ fprintf (cpp, " virtual const char * getBuffer() const { return mBuffer; }\n " );
104+ fprintf (cpp, " void loadResource()\n " );
105+ fprintf (cpp, " {\n " );
106+ fprintf (cpp, " //Get a handle to this process\n " );
107+ fprintf (cpp, " hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId() );\n " );
108+ fprintf (cpp, " if (hProcess)\n " );
109+ fprintf (cpp, " {\n " );
110+ fprintf (cpp, " //Find the main HMODULE of the process\n " );
111+ fprintf (cpp, " DWORD cbNeeded;\n " );
112+ fprintf (cpp, " if ( EnumProcessModules( hProcess, &hModule, sizeof(hModule), &cbNeeded) )\n " );
113+ fprintf (cpp, " {\n " );
114+ fprintf (cpp, " //Retrieve the resource\n " );
115+ fprintf (cpp, " hResourceInfoBlock = FindResource(hModule, \" %s\" , \" CUSTOM\" );\n " , getRandomIdentifier (iCppFilePath).c_str ());
116+ fprintf (cpp, " if (hResourceInfoBlock)\n " );
117+ fprintf (cpp, " {\n " );
118+ fprintf (cpp, " hResHandle = LoadResource(hModule, hResourceInfoBlock);\n " );
119+ fprintf (cpp, " if (hResHandle)\n " );
120+ fprintf (cpp, " {\n " );
121+ fprintf (cpp, " mBuffer = (const char *)LockResource(hResHandle);\n " );
122+ fprintf (cpp, " mBufferSize = SizeofResource(hModule, hResourceInfoBlock);\n " );
123+ fprintf (cpp, " }\n " );
124+ fprintf (cpp, " }\n " );
125+ fprintf (cpp, " }\n " );
126+ fprintf (cpp, " }\n " );
127+ fprintf (cpp, " }\n " );
128+ fprintf (cpp, " virtual void unloadResource()\n " );
129+ fprintf (cpp, " {\n " );
130+ fprintf (cpp, " if (hResHandle)\n " );
131+ fprintf (cpp, " {\n " );
132+ fprintf (cpp, " FreeResource(hResHandle);\n " );
133+ fprintf (cpp, " hResHandle = NULL;\n " );
134+ fprintf (cpp, " mBuffer = NULL;\n " );
135+ fprintf (cpp, " mBufferSize = 0;\n " );
136+ fprintf (cpp, " }\n " );
137+ fprintf (cpp, " hResourceInfoBlock = NULL;\n " );
138+ fprintf (cpp, " hModule = NULL;\n " );
139+ fprintf (cpp, " if (hProcess)\n " );
140+ fprintf (cpp, " {\n " );
141+ fprintf (cpp, " CloseHandle(hProcess);\n " );
142+ fprintf (cpp, " hProcess = NULL;\n " );
143+ fprintf (cpp, " }\n " );
144+ fprintf (cpp, " }\n " );
145+ fprintf (cpp, " %s" , getSaveMethodTemplate ().c_str ());
146+ fprintf (cpp, " private:\n " );
147+ fprintf (cpp, " HANDLE hProcess;\n " );
148+ fprintf (cpp, " HMODULE hModule;\n " );
149+ fprintf (cpp, " HRSRC hResourceInfoBlock;\n " );
150+ fprintf (cpp, " HGLOBAL hResHandle;\n " );
151+ fprintf (cpp, " DWORD mBufferSize;\n " );
152+ fprintf (cpp, " const char * mBuffer;\n " );
153+ fprintf (cpp, " };\n " );
154+ fprintf (cpp, " const %s & %s() { static %s _instance; return _instance; }\n " , mBaseClass .c_str (), getterFunctionName.c_str (), className.c_str ());
155+ fprintf (cpp, " }; //%s\n " , mNamespace .c_str ());
156+
157+ fclose (input);
158+ fclose (cpp);
159+
160+ return true ;
161+ }
162+
163+ std::string Win32ResourceGenerator::getResourceFilePath (const char * iCppFilePath)
164+ {
165+ // Build header file path
166+ std::string resourcePath = iCppFilePath;
167+ strReplace (resourcePath, " .cpp" , " .rc" );
168+ return resourcePath;
169+ }
170+
171+ bool Win32ResourceGenerator::createResourceFile (const char * iCppFilePath)
172+ {
173+ // Build resource file path
174+ std::string resourceFilePath = getResourceFilePath (iCppFilePath);
175+
176+ // create resource file
177+ FILE * res = fopen (resourceFilePath.c_str (), " w" );
178+ if (!res)
179+ {
180+ return false ;
181+ }
182+
183+ std::string filePath = mInputFile ;
184+ strReplace (filePath, " \\ " , " \\\\ " );
185+
186+ // write res file heading
187+ fprintf (res, " %s" , getHeaderTemplate ().c_str ());
188+ fprintf (res, " #include <windows.h>\n " );
189+ fprintf (res, " %s CUSTOM \" %s\"\n " , getRandomIdentifier (iCppFilePath).c_str (), filePath.c_str ());
190+
191+ fclose (res);
192+
193+ return true ;
194+ }
195+
196+ std::string Win32ResourceGenerator::getRandomIdentifier (const char * /* iCppFilePath*/ )
197+ {
198+ return " html5skeletonAGE632H2D7" ;
199+ }
200+
201+ }; // bin2cpp
0 commit comments