programing

winapi: 프로세스를 생성하지만 프로세스의 창을 숨기시겠습니까?

telecom 2023. 6. 29. 19:47
반응형

winapi: 프로세스를 생성하지만 프로세스의 창을 숨기시겠습니까?

CreateProcess를 사용하여 실행하는 매개 변수를 전달하고 종료하는 cmd.exe 프로세스를 생성하고 있습니다. 그러면 명령 프롬프트가 화면에 깜박입니다.

스타트업인포 구조 wShowWindow를 SW_HIDE로 설정하여 이를 피하려고 했지만 이 매개 변수는 실행되는 프로세스의 창이 아니라 호출 창에 영향을 미치는 것 같습니다.

생성 프로세스를 사용하여 보이지 않는 프로그램을 실행할 수 있는 방법이 있습니까?

또한 환경 변수를 가져오는 적절한 winapi 표준 방법은 무엇입니까?

단순히 콘솔 앱인 경우에는CREATE_NO_WINDOW의 일부로 플래그 지정CreateProcess예를 들어, 스스로를 부릅니다.

CreateProcess(NULL, lpszCommandLine, NULL, NULL, FALSE, 
              CREATE_NO_WINDOW, NULL, NULL, &si, &pi);

또한 환경 변수에 대한 자세한 내용은 이 페이지를 참조하십시오.

다음 링크에서는 창을 자동으로 만드는 방법에 대해 설명합니다.

DWORD RunSilent(char* strFunct, char* strstrParams)
{
    STARTUPINFO StartupInfo;
    PROCESS_INFORMATION ProcessInfo;
    char Args[4096];
    char *pEnvCMD = NULL;
    char *pDefaultCMD = "CMD.EXE";
    ULONG rc;

    memset(&StartupInfo, 0, sizeof(StartupInfo));
    StartupInfo.cb = sizeof(STARTUPINFO);
    StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
    StartupInfo.wShowWindow = SW_HIDE;

    Args[0] = 0;

    pEnvCMD = getenv("COMSPEC");

    if(pEnvCMD){

        strcpy(Args, pEnvCMD);
    }
    else{
        strcpy(Args, pDefaultCMD);
    }

    // "/c" option - Do the command then terminate the command window
    strcat(Args, " /c "); 
    //the application you would like to run from the command window
    strcat(Args, strFunct);  
    strcat(Args, " "); 
    //the parameters passed to the application being run from the command window.
    strcat(Args, strstrParams); 

    if (!CreateProcess( NULL, Args, NULL, NULL, FALSE,
        CREATE_NEW_CONSOLE, 
        NULL, 
        NULL,
        &StartupInfo,
        &ProcessInfo))
    {
        return GetLastError();      
    }

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
        rc = 0;

    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ProcessInfo.hProcess);

    return rc;

}

getenv와 setenv는 괜찮은 것 같아요?저는 당신이 그 점에 대해 무엇을 묻고 있는지 잘 모르겠습니다.

STARTF_USESHOW WINDOW를 dwFlags로 설정합니다.

날카로운 방법으로

이것은 당신의 요구에 너무 과한 것일 수 있지만, 당신은 ShowWindow API를 후크할 수 있고 그 프로세스를 위한 어떤 창도 보여주지 않을 수 있습니다.

언급URL : https://stackoverflow.com/questions/780465/winapi-createprocess-but-hide-the-process-window

반응형