-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExeFileConsole.cpp
More file actions
93 lines (79 loc) · 2.27 KB
/
ExeFileConsole.cpp
File metadata and controls
93 lines (79 loc) · 2.27 KB
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
// Copyright © 2026 CCP ehf.
// This is a launcher for a windows application. It allows us to run it from a command shell
// without spawning a new process.
// It is actually name agnostic, so rename the first part of it to match the windows app
// and it will work.
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>
#include <locale>
std::wstring GetModuleName()
{
wchar_t buf[ MAX_PATH+1 ];
DWORD len = GetModuleFileName( 0, buf, _countof( buf ) );
return std::wstring( buf, len );
}
int _tmain( int argc, _TCHAR* argv[] )
{
std::wstring cmd = GetCommandLine();
std::wstring module = GetModuleName();
std::wstring exeName;
//strip off the console part of the name:
std::wstring::size_type ix = module.find( L"Console" );
if( ix != std::wstring::npos )
{
exeName = module.replace( ix, strlen("Console"), L"" );
}
//find basename minus the .exe
std::wstring::size_type found = exeName.rfind( L'\\' );
std::wstring baseName = exeName.substr( found+1, exeName.size()-(found+1)-4 );
//special signal for ExeFile.exe to go into inherit mode for console
SetEnvironmentVariable( L"EXEFILE_INHERIT", L"" );
PROCESS_INFORMATION pi;
STARTUPINFO si;
memset( &si, 0, sizeof( si ) );
si.cb = sizeof( si );
si.lpDesktop = (wchar_t*)L"";
si.lpTitle = (wchar_t*)baseName.c_str();
//pass on whatever it is we are using as stdandard IO
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
wchar_t *tmp = new wchar_t[ cmd.size()*2 ];
wcscpy_s( tmp, cmd.size()*2, cmd.c_str() );
BOOL ok = CreateProcess( exeName.c_str(),
tmp,
0,
0,
TRUE, //inherit handles (std IO)
0,
0,
0,
&si,
&pi );
delete [] tmp;
if( !ok )
{
return GetLastError();
}
CloseHandle( pi.hThread );
DWORD waited = WaitForSingleObject( pi.hProcess, INFINITE );
if( waited == WAIT_FAILED )
{
DWORD err = GetLastError();
CloseHandle( pi.hProcess );
return err;
}
DWORD exitCode;
ok = GetExitCodeProcess(pi.hProcess, &exitCode);
if( !ok )
{
DWORD err = GetLastError();
CloseHandle( pi.hProcess );
return err;
}
CloseHandle( pi.hProcess );
return exitCode;
}