Skip to content

Commit 59a235b

Browse files
committed
add save&load command for Img2Mem
Signed-off-by: fufesou <shuanglongchen@yeah.net>
1 parent b693c04 commit 59a235b

3 files changed

Lines changed: 80 additions & 18 deletions

File tree

Img2Mem/Img2Mem.vcxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@
139139
</Link>
140140
</ItemDefinitionGroup>
141141
<ItemGroup>
142+
<ClCompile Include="..\WindowInjection\img.cpp" />
143+
<ClCompile Include="..\WindowInjection\pch.cpp" />
142144
<ClCompile Include="main.cpp" />
143145
</ItemGroup>
146+
<ItemGroup>
147+
<ClInclude Include="..\WindowInjection\framework.h" />
148+
<ClInclude Include="..\WindowInjection\img.h" />
149+
<ClInclude Include="..\WindowInjection\pch.h" />
150+
</ItemGroup>
144151
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
145152
<ImportGroup Label="ExtensionTargets">
146153
</ImportGroup>

Img2Mem/Img2Mem.vcxproj.filters

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,22 @@
1818
<ClCompile Include="main.cpp">
1919
<Filter>源文件</Filter>
2020
</ClCompile>
21+
<ClCompile Include="..\WindowInjection\pch.cpp">
22+
<Filter>源文件</Filter>
23+
</ClCompile>
24+
<ClCompile Include="..\WindowInjection\img.cpp">
25+
<Filter>源文件</Filter>
26+
</ClCompile>
27+
</ItemGroup>
28+
<ItemGroup>
29+
<ClInclude Include="..\WindowInjection\img.h">
30+
<Filter>头文件</Filter>
31+
</ClInclude>
32+
<ClInclude Include="..\WindowInjection\pch.h">
33+
<Filter>头文件</Filter>
34+
</ClInclude>
35+
<ClInclude Include="..\WindowInjection\framework.h">
36+
<Filter>头文件</Filter>
37+
</ClInclude>
2138
</ItemGroup>
2239
</Project>

Img2Mem/main.cpp

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,42 @@
44
#include <fstream>
55
#include <streambuf>
66

7+
#include "../WindowInjection/img.h"
8+
79
void print_usage(const char* exe)
810
{
9-
printf("usage: %s <img file>\n", exe);
11+
printf("usage: %s <load|save> <img file>\n", exe);
1012
}
1113

14+
int load_img(const char* infile);
15+
int save_img(const char* outfile);
16+
1217
int write_imgdata(const std::vector<char>& data, const char* outfile);
1318

1419
int main(int argc, char* argv[])
1520
{
16-
if (argc != 2)
21+
if (argc != 3)
1722
{
1823
print_usage(argv[0]);
1924
return 0;
2025
}
2126

22-
const char* infile = argv[1];
23-
const char* outfile = "../WindowInjection/img.cpp";
24-
25-
std::ifstream ifs(infile, std::ios::binary);
26-
if (!ifs.is_open())
27+
std::string cmd(argv[1]);
28+
if (cmd == "load")
2729
{
28-
printf("Failed to open %s\n", infile);
29-
return 0;
30+
printf("begin load image\n");
31+
return load_img(argv[2]);
3032
}
31-
32-
std::vector<char> imgdata(
33-
(std::istreambuf_iterator<char>(ifs)),
34-
std::istreambuf_iterator<char>());
35-
36-
if (0 != write_imgdata(imgdata, outfile))
33+
else if (cmd == "save")
3734
{
38-
printf("Failed to write image data, %s -> %s\n", infile, outfile);
35+
printf("begin save image\n");
36+
return save_img(argv[2]);
3937
}
4038
else
4139
{
42-
printf("Succeeded to write image data, %s -> %s\n", infile, outfile);
40+
print_usage(argv[0]);
41+
return 0;
4342
}
44-
return 0;
4543
}
4644

4745
int write_imgdata(const std::vector<char>& data, const char* outfile)
@@ -78,3 +76,43 @@ int write_imgdata(const std::vector<char>& data, const char* outfile)
7876

7977
return 0;
8078
}
79+
80+
int load_img(const char* infile)
81+
{
82+
const char* outfile = "../WindowInjection/img.cpp";
83+
84+
std::ifstream ifs(infile, std::ios::binary);
85+
if (!ifs.is_open())
86+
{
87+
printf("Failed to open %s\n", infile);
88+
return 0;
89+
}
90+
91+
std::vector<char> imgdata(
92+
(std::istreambuf_iterator<char>(ifs)),
93+
std::istreambuf_iterator<char>());
94+
95+
if (0 != write_imgdata(imgdata, outfile))
96+
{
97+
printf("Failed to write image data, %s -> %s\n", infile, outfile);
98+
}
99+
else
100+
{
101+
printf("Succeeded to write image data, %s -> %s\n", infile, outfile);
102+
}
103+
return 0;
104+
}
105+
106+
int save_img(const char* outfile)
107+
{
108+
std::ofstream ofs(outfile, std::ios::binary|std::ios::trunc);
109+
if (!ofs.is_open())
110+
{
111+
printf("Failed to open %s\n", outfile);
112+
return 0;
113+
}
114+
115+
std::copy(g_img.begin(), g_img.end(), std::ostreambuf_iterator<char>(ofs));
116+
printf("Succeeded to write to %s\n", outfile);
117+
return 0;
118+
}

0 commit comments

Comments
 (0)