Skip to content

Commit b693c04

Browse files
committed
load png file from memory
Signed-off-by: fufesou <shuanglongchen@yeah.net>
1 parent 6c36727 commit b693c04

14 files changed

Lines changed: 1060 additions & 10 deletions

Img2Mem/Img2Mem.vcxproj

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{70f042f9-3993-4957-b3eb-18a146ac0d2b}</ProjectGuid>
25+
<RootNamespace>Img2Mem</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v142</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v142</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v142</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v142</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<LinkIncremental>true</LinkIncremental>
75+
</PropertyGroup>
76+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
77+
<LinkIncremental>false</LinkIncremental>
78+
</PropertyGroup>
79+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
80+
<LinkIncremental>true</LinkIncremental>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<LinkIncremental>false</LinkIncremental>
84+
</PropertyGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
86+
<ClCompile>
87+
<WarningLevel>Level3</WarningLevel>
88+
<SDLCheck>true</SDLCheck>
89+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
90+
<ConformanceMode>true</ConformanceMode>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Console</SubSystem>
94+
<GenerateDebugInformation>true</GenerateDebugInformation>
95+
</Link>
96+
</ItemDefinitionGroup>
97+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
98+
<ClCompile>
99+
<WarningLevel>Level3</WarningLevel>
100+
<FunctionLevelLinking>true</FunctionLevelLinking>
101+
<IntrinsicFunctions>true</IntrinsicFunctions>
102+
<SDLCheck>true</SDLCheck>
103+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
104+
<ConformanceMode>true</ConformanceMode>
105+
</ClCompile>
106+
<Link>
107+
<SubSystem>Console</SubSystem>
108+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
109+
<OptimizeReferences>true</OptimizeReferences>
110+
<GenerateDebugInformation>true</GenerateDebugInformation>
111+
</Link>
112+
</ItemDefinitionGroup>
113+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
114+
<ClCompile>
115+
<WarningLevel>Level3</WarningLevel>
116+
<SDLCheck>true</SDLCheck>
117+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
118+
<ConformanceMode>true</ConformanceMode>
119+
</ClCompile>
120+
<Link>
121+
<SubSystem>Console</SubSystem>
122+
<GenerateDebugInformation>true</GenerateDebugInformation>
123+
</Link>
124+
</ItemDefinitionGroup>
125+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
126+
<ClCompile>
127+
<WarningLevel>Level3</WarningLevel>
128+
<FunctionLevelLinking>true</FunctionLevelLinking>
129+
<IntrinsicFunctions>true</IntrinsicFunctions>
130+
<SDLCheck>true</SDLCheck>
131+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
132+
<ConformanceMode>true</ConformanceMode>
133+
</ClCompile>
134+
<Link>
135+
<SubSystem>Console</SubSystem>
136+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
137+
<OptimizeReferences>true</OptimizeReferences>
138+
<GenerateDebugInformation>true</GenerateDebugInformation>
139+
</Link>
140+
</ItemDefinitionGroup>
141+
<ItemGroup>
142+
<ClCompile Include="main.cpp" />
143+
</ItemGroup>
144+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
145+
<ImportGroup Label="ExtensionTargets">
146+
</ImportGroup>
147+
</Project>

Img2Mem/Img2Mem.vcxproj.filters

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="源文件">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="头文件">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="资源文件">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="main.cpp">
19+
<Filter>源文件</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

Img2Mem/main.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <stdio.h>
2+
#include <iostream>
3+
#include <vector>
4+
#include <fstream>
5+
#include <streambuf>
6+
7+
void print_usage(const char* exe)
8+
{
9+
printf("usage: %s <img file>\n", exe);
10+
}
11+
12+
int write_imgdata(const std::vector<char>& data, const char* outfile);
13+
14+
int main(int argc, char* argv[])
15+
{
16+
if (argc != 2)
17+
{
18+
print_usage(argv[0]);
19+
return 0;
20+
}
21+
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+
{
28+
printf("Failed to open %s\n", infile);
29+
return 0;
30+
}
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))
37+
{
38+
printf("Failed to write image data, %s -> %s\n", infile, outfile);
39+
}
40+
else
41+
{
42+
printf("Succeeded to write image data, %s -> %s\n", infile, outfile);
43+
}
44+
return 0;
45+
}
46+
47+
int write_imgdata(const std::vector<char>& data, const char* outfile)
48+
{
49+
std::ofstream ofs(outfile, std::ios::trunc);
50+
if (!ofs.is_open())
51+
{
52+
return 1;
53+
}
54+
55+
ofs << "#include \"pch.h\"\n";
56+
ofs << "#include \"./img.h\"\n\n";
57+
ofs << "std::vector<unsigned char> g_img{\n";
58+
59+
auto total = data.size();
60+
const unsigned char* pdata = reinterpret_cast<const unsigned char*>(data.data());
61+
62+
constexpr auto line_count = 20;
63+
// 0xaa,_
64+
char buf[6 * line_count];
65+
66+
auto c = 0;
67+
while (c < total)
68+
{
69+
memset(buf, 0, sizeof(buf));
70+
for (long long i = 0; i < line_count && c < total; ++i, ++c)
71+
{
72+
_snprintf_s(buf + i * 6, sizeof(buf) - i * 6, _TRUNCATE, "0x%02x, ", pdata[c]);
73+
}
74+
ofs << buf;
75+
ofs << "\n";
76+
}
77+
ofs << "};\n";
78+
79+
return 0;
80+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Run ```taskkill /F /IM MiniBroker.exe``` to close the white window.
1818

1919
## Projections
2020

21+
### Img2Mem
22+
23+
This project generates ```WindowInjection/img.cpp```, which is used as a memory png data.
24+
2125
### WindowInjection
2226

2327
The DLL project.

TempTopMostWindow.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WindowInjection", "WindowIn
99
EndProject
1010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestAppDebug", "TestAppDebug\TestAppDebug.vcxproj", "{46B8AE1F-E839-4847-B055-1606063FDD5F}"
1111
EndProject
12+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Img2Mem", "Img2Mem\Img2Mem.vcxproj", "{70F042F9-3993-4957-B3EB-18A146AC0D2B}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|x64 = Debug|x64
@@ -41,6 +43,14 @@ Global
4143
{46B8AE1F-E839-4847-B055-1606063FDD5F}.Release|x64.Build.0 = Release|x64
4244
{46B8AE1F-E839-4847-B055-1606063FDD5F}.Release|x86.ActiveCfg = Release|Win32
4345
{46B8AE1F-E839-4847-B055-1606063FDD5F}.Release|x86.Build.0 = Release|Win32
46+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Debug|x64.ActiveCfg = Debug|x64
47+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Debug|x64.Build.0 = Debug|x64
48+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Debug|x86.ActiveCfg = Debug|Win32
49+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Debug|x86.Build.0 = Debug|Win32
50+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Release|x64.ActiveCfg = Release|x64
51+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Release|x64.Build.0 = Release|x64
52+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Release|x86.ActiveCfg = Release|Win32
53+
{70F042F9-3993-4957-B3EB-18A146AC0D2B}.Release|x86.Build.0 = Release|Win32
4454
EndGlobalSection
4555
GlobalSection(SolutionProperties) = preSolution
4656
HideSolutionNode = FALSE

TestAppDebug/TestAppDebug.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@
141141
<ItemGroup>
142142
<ClInclude Include="..\WindowInjection\bitmap_loader.h" />
143143
<ClInclude Include="..\WindowInjection\framework.h" />
144+
<ClInclude Include="..\WindowInjection\img.h" />
144145
<ClInclude Include="..\WindowInjection\pch.h" />
145146
</ItemGroup>
146147
<ItemGroup>
147148
<ClCompile Include="..\WindowInjection\bitmap_loader.cpp" />
148149
<ClCompile Include="..\WindowInjection\dllmain.cpp" />
150+
<ClCompile Include="..\WindowInjection\img.cpp" />
149151
<ClCompile Include="..\WindowInjection\pch.cpp" />
150152
</ItemGroup>
151153
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

TestAppDebug/TestAppDebug.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClInclude Include="..\WindowInjection\bitmap_loader.h">
2525
<Filter>头文件</Filter>
2626
</ClInclude>
27+
<ClInclude Include="..\WindowInjection\img.h">
28+
<Filter>头文件</Filter>
29+
</ClInclude>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ClCompile Include="..\WindowInjection\pch.cpp">
@@ -35,5 +38,8 @@
3538
<ClCompile Include="..\WindowInjection\dllmain.cpp">
3639
<Filter>源文件</Filter>
3740
</ClCompile>
41+
<ClCompile Include="..\WindowInjection\img.cpp">
42+
<Filter>源文件</Filter>
43+
</ClCompile>
3844
</ItemGroup>
3945
</Project>

WindowInjection/WindowInjection.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@
153153
<ItemGroup>
154154
<ClInclude Include="bitmap_loader.h" />
155155
<ClInclude Include="framework.h" />
156+
<ClInclude Include="img.h" />
156157
<ClInclude Include="pch.h" />
157158
</ItemGroup>
158159
<ItemGroup>
159160
<ClCompile Include="bitmap_loader.cpp" />
160161
<ClCompile Include="dllmain.cpp" />
162+
<ClCompile Include="img.cpp" />
161163
<ClCompile Include="pch.cpp">
162164
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
163165
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

WindowInjection/WindowInjection.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClInclude Include="bitmap_loader.h">
2525
<Filter>头文件</Filter>
2626
</ClInclude>
27+
<ClInclude Include="img.h">
28+
<Filter>头文件</Filter>
29+
</ClInclude>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ClCompile Include="dllmain.cpp">
@@ -35,5 +38,8 @@
3538
<ClCompile Include="bitmap_loader.cpp">
3639
<Filter>源文件</Filter>
3740
</ClCompile>
41+
<ClCompile Include="img.cpp">
42+
<Filter>源文件</Filter>
43+
</ClCompile>
3844
</ItemGroup>
3945
</Project>

0 commit comments

Comments
 (0)