2017-04-21 8 views
1

배치 프로젝트에 배치 파일을 포함시키고 콘솔에서 배치 스크립트를 실행하고 싶습니다.배치 스크립트 파일을 임베디드하고 C++ 콘솔 프로젝트에서 실행

포함 스크립트

#include "stdafx.h" 
#include "iostream" 
#include "conio.h" 
#define WINDOWS_LEAN_AND_MEAN 
#include <Windows.h> 

std::wstring GetEnvString() 
{ 
    wchar_t* env = GetEnvironmentStrings(); 
    if (!env) 
     abort(); 
    const wchar_t* var = env; 
    size_t totallen = 0; 
    size_t len; 
    while ((len = wcslen(var)) > 0) 
    { 
     totallen += len + 1; 
     var += len + 1; 
    } 
    std::wstring result(env, totallen); 
    FreeEnvironmentStrings(env); 
    return result; 
} 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 

    std::wstring env = GetEnvString(); 
    env += L"myvar=boo"; 
    env.push_back('\0'); // somewhat awkward way to embed a null-terminator 

    STARTUPINFO si = { sizeof(STARTUPINFO) }; 
    PROCESS_INFORMATION pi; 

    wchar_t cmdline[] = L"cmd.exe /C f.bat"; 

    if (!CreateProcess(NULL, cmdline, NULL, NULL, false, CREATE_UNICODE_ENVIRONMENT, 
     (LPVOID)env.c_str(), NULL, &si, &pi)) 
    { 
     std::cout << GetLastError(); 
     abort(); 
    } 

    CloseHandle(pi.hProcess); 
    CloseHandle(pi.hThread); 

    getch(); 
} 

embed batch script file in c++ console

내가 자원 프로젝트에서 실행하려는이 있습니다 위치에 배치 파일을 실행하는 방법을

wchar_t cmdline[] = L"cmd.exe /C f.bat"; 

답변

0

당신은 std::system 사용할 수 있습니다

#include <fstream> 
#include <cstdlib> 

int main() 
{ 
    std::ofstream fl("test.bat"); 
    fl << "@echo off\n" 
      "echo testing batch.\n" 
      "cd c:\\windows\n" 
      "dir"; 
    fl.close(); 
    system("test.bat"); 
} 

그러나 시스템을 사용하면 간단하게 명령을 실행할 수 있으며 출력을 얻을 수 없습니다. 출력을 얻으려면 .bat의 출력을 파일로 리디렉션하거나 popen을 사용할 수 있으며 일반 파일처럼 출력을 읽을 수 있습니다. 그는 popen 당신은 표준 출력 얻을 수 있지만, 표준 출력에 열려진 리디렉션 할 수 있습니다 참고 : Windows의 리소스로 .bat 파일을 저장하려면

01: 'missing_executable.exe' is not recognized as an internal or external command, 
02: operable program or batch file. 
process return: 1 
Press any key to continue . . . 

하는 당신에게 실행 : 여기

#include <cstdlib> 
#include <cstdio> 

int main() 
{ 
    FILE *p = _popen("missing_executable.exe 2>&1", "r"); 
    if (p) 
    { 
     char data[1024]; 
     int n = 0; 
     while (fgets(data, 1024, p)) 
      printf("%02d: %s", ++n, data); 
     int ret = _pclose(p); 
     printf("process return: %d\n", ret); 
    } 
    else 
    { 
     printf("failed to popen\n"); 
    } 
} 

하면 출력의 실행 파일에서 실제 박쥐 파일을 얻으려면 FindResource/LoadResource/LockResource을 사용할 수 있습니다. 이런 식으로 뭔가 :

HMODULE module = GetModuleHandle(NULL); 
// update with your RESOURCE_ID and RESOURCE_TYPE. 
HRSRC res = FindResource(module, MAKEINTRESOURCE(RESOURCE_ID), RESOURCE_TYPE); 
HGLOBAL resMem = LoadResource(module, res); 
DWORD resSize = SizeofResource(module, res); 
LPVOID resPtr = LockResource(resMem); 

char *bytes = new char[resSize]; 
memcpy(bytes, resPtr, resSize); 

지금 당신이 파일에 바이트를 저장하고 std::system 또는 popen

+0

덕분에 남자 만 삽입 자원에 대한 어떤 방법을 사용하여 실행할 수 있습니까? –

+0

당신은 임베디드 리소스를 가져 와서 어쨌든 (임시 위치로) 파일에 저장 한 다음 시스템 또는 popen을 사용하여 해당 bat 파일을 실행해야합니다. 또는 임시 파일을 사용하지 않으려면 리소스에서 데이터를 읽고 시스템을 사용하여 실행할 수있는'cmd/c param1 param2 ... '문자열을 만들어야합니다. – Pavel

+0

는 예제 코드 –