Skip to content

Commit 7c8e816

Browse files
committed
add CmdEmulator
1 parent dd5577f commit 7c8e816

File tree

3 files changed

+323
-0
lines changed

3 files changed

+323
-0
lines changed

CmdEmulator/CmdEmulator.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// CmdEmulator.cpp : Defines the entry point for the console application.
2+
//
3+
#define _CRT_SECURE_NO_WARNINGS
4+
#include <stdio.h>
5+
#include <conio.h>
6+
#include <windows.h>
7+
8+
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
9+
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
10+
#endif
11+
12+
int unicode_to_utf8(char* out, wchar_t ch, int cp = 65001) {
13+
return WideCharToMultiByte(cp, 0, &ch, 1, out, 8, NULL, NULL);
14+
}
15+
16+
DWORD WINAPI ThreadRecv(LPVOID lpThreadParam) {
17+
HANDLE hPipe = (HANDLE)lpThreadParam;
18+
while (1) {
19+
char szReadBuffer[1024 + 1] = { 0 }; // 必须得加1,否则可能会将后面的乱码打出来
20+
DWORD cbBytesRead;
21+
BOOL fSuccess = ReadFile(
22+
hPipe, // handle to pipe
23+
szReadBuffer, // buffer to receive data
24+
1024, // size of buffer
25+
&cbBytesRead, // number of bytes read
26+
NULL); // not overlapped I/O
27+
28+
if (!fSuccess || cbBytesRead == 0) {
29+
DisconnectNamedPipe(hPipe);
30+
CloseHandle(hPipe);
31+
break;
32+
}
33+
printf(szReadBuffer);
34+
}
35+
return 0;
36+
}
37+
38+
BOOL PipeWrite(HANDLE hPipe, const char* buf, DWORD len) {
39+
DWORD cbWritten = 0;
40+
BOOL fSuccess = WriteFile(
41+
hPipe, // pipe handle
42+
buf, // message
43+
len, // message length
44+
&cbWritten, // bytes written
45+
NULL); // not overlapped
46+
if (!fSuccess || cbWritten != len) {
47+
return FALSE;
48+
// If failed, what should I do???
49+
}
50+
return TRUE;
51+
}
52+
53+
int main(int argc, char* argv[])
54+
{
55+
if (argc != 3) {
56+
printf("Param error!\r\n");
57+
return 0;
58+
}
59+
printf("Initializing...\r\n");
60+
SetConsoleTitle(argv[2]);
61+
62+
// 1. 设置输出模式
63+
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
64+
DWORD dwMode = 0;
65+
GetConsoleMode(hOut, &dwMode);
66+
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
67+
SetConsoleMode(hOut, dwMode);
68+
69+
// 2.连接管道
70+
char szPipeName[MAX_PATH] = { "\\\\.\\pipe\\" };
71+
strcat(szPipeName, argv[1]);
72+
73+
char szPipeInCmd[MAX_PATH] = { 0 };
74+
strcpy(szPipeInCmd, szPipeName);
75+
strcat(szPipeInCmd, "inCMD");
76+
char szPipeOutCmd[MAX_PATH] = { 0 };
77+
strcpy(szPipeOutCmd, szPipeName);
78+
strcat(szPipeOutCmd, "outCMD");
79+
HANDLE hPipeInCmd = CreateFileA(
80+
szPipeInCmd, // pipe name
81+
GENERIC_READ | // read and write access
82+
GENERIC_WRITE,
83+
0, // no sharing
84+
NULL, // default security attributes
85+
OPEN_EXISTING, // opens existing pipe
86+
0, // default attributes
87+
NULL); // no template file
88+
89+
if (hPipeInCmd == INVALID_HANDLE_VALUE) {
90+
printf("Create Pipe Failed!\r\n");
91+
return 0;
92+
}
93+
94+
HANDLE hPipeOutCmd = CreateFileA(
95+
szPipeOutCmd, // pipe name
96+
GENERIC_READ | // read and write access
97+
GENERIC_WRITE,
98+
0, // no sharing
99+
NULL, // default security attributes
100+
OPEN_EXISTING, // opens existing pipe
101+
0, // default attributes
102+
NULL); // no template file
103+
104+
if (hPipeOutCmd == INVALID_HANDLE_VALUE) {
105+
printf("Create Pipe Failed!\r\n");
106+
return 0;
107+
}
108+
109+
// 3. 收发数据
110+
system("cls");
111+
DWORD dwThreadId;
112+
HANDLE hRecvThread = CreateThread(
113+
NULL, // default security attributes
114+
0, // use default stack size
115+
ThreadRecv, // thread function name
116+
hPipeInCmd, // argument to thread function
117+
0, // use default creation flags
118+
&dwThreadId); // returns the thread identifier
119+
if (hRecvThread == NULL) {
120+
printf("Create Thread Failed!\r\n");
121+
return 0;
122+
}
123+
124+
while (true) {
125+
wchar_t ch = (wchar_t)_getwch();
126+
if (ch == (wchar_t)0xe0) {
127+
ch = (wchar_t)_getwch();
128+
if (ch == (wchar_t)0x48) {
129+
PipeWrite(hPipeOutCmd, "\x1B[A", 3);
130+
continue;
131+
}
132+
if (ch == (wchar_t)0x50) {
133+
PipeWrite(hPipeOutCmd, "\x1B[B", 3);
134+
continue;
135+
}
136+
if (ch == (wchar_t)0x4B) {
137+
PipeWrite(hPipeOutCmd, "\x1B[D", 3);
138+
continue;
139+
}
140+
if (ch == (wchar_t)0x4D) {
141+
PipeWrite(hPipeOutCmd, "\x1B[C", 3);
142+
continue;
143+
}
144+
}
145+
char out[8] = { 0 };
146+
int len = unicode_to_utf8(out, ch);
147+
PipeWrite(hPipeOutCmd, out, len);
148+
}
149+
150+
CloseHandle(hPipeInCmd);
151+
CloseHandle(hPipeOutCmd);
152+
CloseHandle(hRecvThread);
153+
return 0;
154+
}

CmdEmulator/CmdEmulator.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.1169
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CmdEmulator", "CmdEmulator.vcxproj", "{DA8823CD-C162-467C-BD62-15940C4DE04B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x86 = Debug|x86
11+
Release|x86 = Release|x86
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{DA8823CD-C162-467C-BD62-15940C4DE04B}.Debug|x86.ActiveCfg = Debug|Win32
15+
{DA8823CD-C162-467C-BD62-15940C4DE04B}.Debug|x86.Build.0 = Debug|Win32
16+
{DA8823CD-C162-467C-BD62-15940C4DE04B}.Release|x86.ActiveCfg = Release|Win32
17+
{DA8823CD-C162-467C-BD62-15940C4DE04B}.Release|x86.Build.0 = Release|Win32
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {04DCB1F7-3404-4DB2-9DBA-B06B6FF05AD2}
24+
EndGlobalSection
25+
EndGlobal

CmdEmulator/CmdEmulator.vcxproj

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="15.0" 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+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<SccProjectName />
15+
<SccLocalPath />
16+
<ProjectGuid>{DA8823CD-C162-467C-BD62-15940C4DE04B}</ProjectGuid>
17+
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
18+
</PropertyGroup>
19+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
20+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
21+
<ConfigurationType>Application</ConfigurationType>
22+
<PlatformToolset>v140_xp</PlatformToolset>
23+
<UseOfMfc>false</UseOfMfc>
24+
<CharacterSet>MultiByte</CharacterSet>
25+
</PropertyGroup>
26+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
27+
<ConfigurationType>Application</ConfigurationType>
28+
<PlatformToolset>v140_xp</PlatformToolset>
29+
<UseOfMfc>false</UseOfMfc>
30+
<CharacterSet>MultiByte</CharacterSet>
31+
</PropertyGroup>
32+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
33+
<ImportGroup Label="ExtensionSettings">
34+
</ImportGroup>
35+
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
36+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
37+
<Import Project="$(VCTargetsPath)Microsoft.Cpp.UpgradeFromVC60.props" />
38+
</ImportGroup>
39+
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
40+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
41+
<Import Project="$(VCTargetsPath)Microsoft.Cpp.UpgradeFromVC60.props" />
42+
</ImportGroup>
43+
<PropertyGroup Label="UserMacros" />
44+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
45+
<OutDir>.\Release\</OutDir>
46+
<IntDir>.\Release\</IntDir>
47+
<LinkIncremental>false</LinkIncremental>
48+
<GenerateManifest>false</GenerateManifest>
49+
</PropertyGroup>
50+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
51+
<OutDir>.\Debug\</OutDir>
52+
<IntDir>.\Debug\</IntDir>
53+
<LinkIncremental>true</LinkIncremental>
54+
</PropertyGroup>
55+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
56+
<ClCompile>
57+
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
58+
<InlineFunctionExpansion>Default</InlineFunctionExpansion>
59+
<StringPooling>true</StringPooling>
60+
<FunctionLevelLinking>true</FunctionLevelLinking>
61+
<Optimization>MinSpace</Optimization>
62+
<SuppressStartupBanner>true</SuppressStartupBanner>
63+
<WarningLevel>Level3</WarningLevel>
64+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
65+
<AssemblerListingLocation>.\Release\</AssemblerListingLocation>
66+
<PrecompiledHeaderOutputFile>.\Release\CmdEmulator.pch</PrecompiledHeaderOutputFile>
67+
<PrecompiledHeader>Use</PrecompiledHeader>
68+
<PrecompiledHeaderFile>stdafx.h</PrecompiledHeaderFile>
69+
<ObjectFileName>.\Release\</ObjectFileName>
70+
<ProgramDataBaseFileName>.\Release\</ProgramDataBaseFileName>
71+
</ClCompile>
72+
<Midl>
73+
<TypeLibraryName>.\Release\CmdEmulator.tlb</TypeLibraryName>
74+
</Midl>
75+
<ResourceCompile>
76+
<Culture>0x0804</Culture>
77+
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
78+
</ResourceCompile>
79+
<Bscmake>
80+
<SuppressStartupBanner>true</SuppressStartupBanner>
81+
<OutputFile>.\Release\CmdEmulator.bsc</OutputFile>
82+
</Bscmake>
83+
<Link>
84+
<SuppressStartupBanner>true</SuppressStartupBanner>
85+
<SubSystem>Console</SubSystem>
86+
<OutputFile>.\Release\CmdEmulator.exe</OutputFile>
87+
<AdditionalDependencies>odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
88+
<ManifestFile />
89+
<GenerateDebugInformation>false</GenerateDebugInformation>
90+
<ProgramDatabaseFile />
91+
<FullProgramDatabaseFile>false</FullProgramDatabaseFile>
92+
</Link>
93+
</ItemDefinitionGroup>
94+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
95+
<ClCompile>
96+
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
97+
<InlineFunctionExpansion>Default</InlineFunctionExpansion>
98+
<FunctionLevelLinking>
99+
</FunctionLevelLinking>
100+
<Optimization>Disabled</Optimization>
101+
<SuppressStartupBanner>true</SuppressStartupBanner>
102+
<WarningLevel>Level3</WarningLevel>
103+
<MinimalRebuild>false</MinimalRebuild>
104+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
105+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106+
<AssemblerListingLocation>.\Debug\</AssemblerListingLocation>
107+
<PrecompiledHeaderOutputFile>.\Debug\CmdEmulator.pch</PrecompiledHeaderOutputFile>
108+
<PrecompiledHeader>Use</PrecompiledHeader>
109+
<PrecompiledHeaderFile>stdafx.h</PrecompiledHeaderFile>
110+
<ObjectFileName>.\Debug\</ObjectFileName>
111+
<ProgramDataBaseFileName>.\Debug\</ProgramDataBaseFileName>
112+
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
113+
</ClCompile>
114+
<Midl>
115+
<TypeLibraryName>.\Debug\CmdEmulator.tlb</TypeLibraryName>
116+
</Midl>
117+
<ResourceCompile>
118+
<Culture>0x0804</Culture>
119+
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
120+
</ResourceCompile>
121+
<Bscmake>
122+
<SuppressStartupBanner>true</SuppressStartupBanner>
123+
<OutputFile>.\Debug\CmdEmulator.bsc</OutputFile>
124+
</Bscmake>
125+
<Link>
126+
<SuppressStartupBanner>true</SuppressStartupBanner>
127+
<GenerateDebugInformation>true</GenerateDebugInformation>
128+
<SubSystem>Console</SubSystem>
129+
<OutputFile>.\Debug\CmdEmulator.exe</OutputFile>
130+
<AdditionalDependencies>odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
131+
</Link>
132+
</ItemDefinitionGroup>
133+
<ItemGroup>
134+
<ClCompile Include="CmdEmulator.cpp">
135+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
136+
</PrecompiledHeader>
137+
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
138+
</PrecompiledHeader>
139+
</ClCompile>
140+
</ItemGroup>
141+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
142+
<ImportGroup Label="ExtensionTargets">
143+
</ImportGroup>
144+
</Project>

0 commit comments

Comments
 (0)