繁体   English   中英

winapi:CreateProcess但隐藏进程的窗口?

[英]winapi: CreateProcess but hide the process' window?

我正在使用CreateProcess创建一个cmd.exe进程,该进程传递一个它执行并退出的参数,这会使命令提示符在屏幕上闪烁。

我试图通过将STARTUPINFO struct wShowWindow设置为SW_HIDE来避免这种情况,但此参数似乎会影响调用窗口,而不会影响执行进程的窗口。

无论如何,您是否可以使用createprocess来启动隐藏在视图中的程序?

获取环境变量的正确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都可以吗? 我不确定你在这方面要问的是什么。

在dwFlags中设置STARTF_USESHOWWINDOW

由sharptooth

这可能对您的需求来说太过分了,但您可以挂钩ShowWindow API并且永远不会显示该进程的任何窗口

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM