forked from black-square/nwlibs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestHelper.cpp
172 lines (134 loc) · 5.14 KB
/
TestHelper.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//Автор: Шестеркин Дмитрий(NW) 2005
#include "stdafx.h"
#include "TestHelper.h"
#include "CoutConvert.h"
#include <shellapi.h>
namespace NWLib {
ConsoleSynchronous Console;
///////////////////////////////////////////////////////////////////////////////
DWORD RunCommand( LPTSTR Command, LPCTSTR CurrentDirectory /*= NULL*/ )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
Command, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
CurrentDirectory, // Starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
APL_THROW(
_T("Error on executing command '") << ConvertToBuf<std::basic_string<TCHAR> >(Command)
<< _T("' with starting dir '") << (CurrentDirectory == NULL ? std::basic_string<TCHAR>(_T("NULL")) : ConvertToBuf<std::basic_string<TCHAR> >(CurrentDirectory))
<< _T("': ") << GetDWErrorInfo( GetLastError() )
);
}
DWORD ExitCode;
APL_ERROR( WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED );
APL_ERROR( GetExitCodeProcess(pi.hProcess, &ExitCode) );
APL_ERROR( CloseHandle(pi.hProcess) );
APL_ERROR( CloseHandle(pi.hThread) );
return ExitCode;
}
///////////////////////////////////////////////////////////////////////////////
void ExecutePreRunFile( const TCHAR *FilePath )
{
#if 0
SHELLEXECUTEINFO ShExecInfo;
TCHAR FullPath[MAX_PATH], FileDir[MAX_PATH];
LPTSTR FileName;
APL_ERROR( GetFullPathName(FilePath, MAX_PATH, FullPath, &FileName) != 0 );
memcpy( FileDir, FullPath, (FileName - FullPath) * sizeof(*FileDir) );
FileDir[FileName - FullPath] = _T('\0');
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL; o
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = FullPath;
#ifdef DEBUG
ShExecInfo.lpParameters = _T( "DEBUG" );
#else
ShExecInfo.lpParameters = _T( "RELEASE" );
#endif
ShExecInfo.lpDirectory = FileDir;
ShExecInfo.nShow = nShowCmd;
ShExecInfo.hInstApp = NULL;
cout << "Запуск файла " << FullPath;
if( ShellExecuteEx(&ShExecInfo) != TRUE )
{
cout << "ERROR: " << GetDWErrorInfo( GetLastError() );
throw TSilentException();
}
if( ShExecInfo.hProcess != NULL )
{
DWORD ExitCode;
APL_ERROR( WaitForSingleObject(ShExecInfo.hProcess, INFINITE) != WAIT_FAILED );
APL_ERROR( GetExitCodeProcess(ShExecInfo.hProcess, &ExitCode) );
APL_ERROR( CloseHandle(ShExecInfo.hProcess) != 0 );
if( ExitCode != 0 )
{
cout << " ERROR ExitCode != 0!"<< endl;
throw TSilentException();
}
else
cout << " OK!" << endl;
}
else
cout << " OK (no hProcess)!" << endl;
#else
#ifdef DEBUG
#define CMD_LINE _T( " DEBUG" )
#else
#define CMD_LINE _T( " RELEASE" )
#endif
TCHAR FullPath[MAX_PATH], FileDir[MAX_PATH];
LPTSTR FileName;
APL_ERROR( GetFullPathName(FilePath, MAX_PATH, FullPath, &FileName) != 0 );
memcpy( FileDir, FullPath, (FileName - FullPath) * sizeof(*FileDir) );
FileDir[FileName - FullPath] = _T('\0');
lstrcat( FullPath, CMD_LINE );
std::cout << "Запуск файла: " << FullPath << std::endl << APL_LINE;
DWORD ExitCode = RunCommand( FullPath, FileDir );
std::cout << APL_LINE;
if( ExitCode != 0)
{
std::cout << " ERROR ExitCode: " << ExitCode << std::endl;
throw TSilentException();
}
else
std::cout << "OK!" << std::endl << std::endl;
#endif
}
///////////////////////////////////////////////////////////////////////////////
void CopyLoadManager()
{
#ifdef DEBUG
std::basic_string<TCHAR> CopyFrom(GetExeDirPath() + _T("..\\..\\..\\..\\Bin.debug\\Core\\LoadManager.dll") );
#else
std::basic_string<TCHAR> CopyFrom(GetExeDirPath() + _T("..\\..\\..\\..\\Bin\\Core\\LoadManager.dll") );
#endif
std::basic_string<TCHAR> CopyTo(GetExeDirPath() + _T("LoadManager.dll") );
std::cout << "Копируем\n\t" << CopyFrom << "\n\t" << CopyTo << std::endl << std::endl;
if( CopyFile(CopyFrom.c_str(), CopyTo.c_str(), FALSE) == FALSE )
{
std::cout << GetDWErrorInfo(GetLastError());
throw TSilentException();
}
}
///////////////////////////////////////////////////////////////////////////////
// class ConsoleSynchronous
///////////////////////////////////////////////////////////////////////////////
void ConsoleSynchronous::StdData()
{
std::cout << GetTimeStampString() << " ";
}
} //namespace NWLib