C++ WinApi CreateProcess Unable To Create Child Process With Environment Block

D13

I need help with creating child process inheriting the new temporary Path environment variable then using the child process, it will run the process inside new folder specified in the Path.

Example, I add C:\Test into Path environment variable,and then i wan't to run a program by using cmd.exe as the child process

I'm having problem to create process by using the line below,it will pop up message unable to run the child process

Utilities::createProcess(_T("C:\\Windows\\system32\\cmd.exe"),_T(""),txtbuff);
// txtbuff is a WCHAR with size of 4096
// it contains the concatenation of _T("Path=C:\\Test;\0"); and pszOldPath
// pszOldPath get its value from GetEnvironmentVariable(_T("PATH"), pszOldPath,4096);
// The concantenated string will have the form of _T("Path=path1\0path2\0....\0\0");

If i pass NULL as my environment block,I'll be able to execute my child process but it won't inherit the new Path environment variable thus cmd.exe cannot run a program that is not specified in current path environment

Utilities::createProcess(_T("C:\\Windows\\system32\\cmd.exe"),_T(""),NULL);

This is my code:

// Utilities.h
namespace Utilities
{
    bool createProcess(LPCWSTR filename,LPWSTR arg,LPTSTR envpath)
    {
        DWORD dwRet;
        LPTSTR pszOldVal;
        TCHAR pszDest[BUFSIZE] = _T("");
        pszOldVal = (LPTSTR) malloc(BUFSIZE*sizeof(TCHAR));
        if(envpath != NULL)
        {
            dwRet = GetEnvironmentVariable(_T("PATH"), pszOldVal, BUFSIZE);
            if(!dwRet)
            {
                MessageBox(NULL,_T("Get environment variables failed."),_T("Error"),MB_OK);
                return false;
            }
            else
            {
                StringCchCat(pszDest,BUFSIZE,_T("Path="));
                StringCchCat(pszDest,BUFSIZE,envpath);
                StringCchCat(pszDest,BUFSIZE,_T(";\0"));
                StringCchCat(pszDest,BUFSIZE,pszOldVal);
                //MessageBox(NULL,pszDest,_T("Environtment Variables"),MB_OK);
            }
        }
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        ZeroMemory(&si, sizeof(si));
        si.cb= sizeof(si);
        ZeroMemory(&pi, sizeof(pi));
        if(!CreateProcess(filename,arg,NULL,NULL,NULL,NULL,pszDest,NULL,&si,&pi))
        {
            MessageBox(NULL,_T("Unable to create process."),_T("Error"),MB_OK);
            return false;
        }
        //WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        free(pszOldVal);
        return true;
    }
}
// Main.cpp
// At Wnd Proc
LRESULT CALLBACK(......)
{
case WM_COMMAND:
    switch(wParam)
    {
        case ID_TEST:
            Utilities::getDlgText(hWnd,ID_INPUT_CPP,txtbuff);
            if(_tcscmp(txtbuff, _T("")) == 0)
            {
                MessageBox(NULL,_T("Please make sure you select folder."),_T("Error"),MB_OK);
                break;
            }
            // Environtment variable"MyVar=MyValue\0MyOtheVar=MyOtherValue\0\0"

// This is where Im having problem right now
            Utilities::createProcess(_T("C:\\Windows\\system32\\cmd.exe"),_T(""),txtbuff,NULL);
            break;
    }
    return true;
}

Really need someone to enlighten me with the answer

Jichao
// Utilities.h
namespace Utilities
{
    bool createProcess(LPCWSTR filename, LPWSTR arg, LPWSTR envpath)
    {
        std::vector<WCHAR> oldPath;
        std::vector<WCHAR> newPath;
        DWORD dwOldLen = 0;

        if(envpath != NULL)
        {
            DWORD dwLen = GetEnvironmentVariable(L"PATH", NULL, 0);
            if (dwLen) {
                oldPath.resize(dwLen);
                dwOldLen = GetEnvironmentVariable(L"PATH", oldPath.data(), dwLen);
                if(dwOldLen + 1 != dwLen)
                {
                    MessageBox(NULL,_T("Get environment variables failed."),_T("Error"),MB_OK);
                    return false;
                }
            }
            size_t newLen = dwOldLen + wcslen(envpath) + 8; //8 for "path=" ";" and double null terminating
            newPath.resize(newLen);
            std::fill(newPath.begin(), newPath.end(), 0);
            memcpy(newPath.data(), L"Path=", 5 * 2);
            memcpy(newPath.data() + 5, oldPath.data(), dwOldLen * 2);
            memcpy(newPath.data() + 5 + dwOldLen, L";", 2);
            memcpy(newPath.data() + 6 + dwOldLen, envpath, wcslen(envpath) * 2);
        }
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        ZeroMemory(&si, sizeof(si));
        si.cb= sizeof(si);
        ZeroMemory(&pi, sizeof(pi));
        if(!CreateProcess(filename,arg,NULL,NULL,NULL,CREATE_UNICODE_ENVIRONMENT,newPath.data(),NULL,&si,&pi))
        {
            MessageBox(NULL,_T("Unable to create process."),_T("Error"),MB_OK);
            return false;
        }
        //WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
        return true;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to set child process' environment variable in Makefile

From Dev

Hide process window with 'CreateProcess'

From Dev

C Programing : Parent & Child Process

From Dev

CreateProcess: redirect child stdout to parent stdout in C++/Windows

From Dev

Block signals for complete process in a multithreaded environment

From Dev

Get Environment Variables of Child Process in VC++

From Dev

Waiting for child process to terminate, or not - C

From Dev

Reading file in child process in C

From Dev

I/O redirection from child process using pipes - winapi

From Dev

How to create a Child process as a specific user with Node?

From Dev

(C++, Windows) Spawn child process without console window (using CreateProcess)

From Dev

setuid create one child process

From Dev

c++ winapi window process inside of a class not working

From Dev

C++ WinApi CreateProcess Unable To Create Child Process With Environment Block

From Dev

Block signals for complete process in a multithreaded environment

From Dev

Get Environment Variables of Child Process in VC++

From Dev

What is the name of child process after posix fork() & win32 CreateProcess()?

From Dev

Reading file in child process in C

From Dev

CreateProcess creating process, but game is not working

From Dev

Memory usage of child process in C

From Dev

How to create a Child process as a specific user with Node?

From Dev

Create child process and kill it after the process is invoked

From Dev

(C++, Windows) Spawn child process without console window (using CreateProcess)

From Dev

Malloc kills child process in Linux environment

From Dev

PEB (Process Environment Block) invalid DllBase address

From Dev

C++ WinAPI - cleanup before windows shutdown terminates the process?

From Dev

Send Ctrl+C to child process when host was created with CREATE_NEW_PROCESS_GROUP

From Dev

unable to create `Environment` in `Silhouette`

From Dev

Creating child process in C linux

Related Related

  1. 1

    How to set child process' environment variable in Makefile

  2. 2

    Hide process window with 'CreateProcess'

  3. 3

    C Programing : Parent & Child Process

  4. 4

    CreateProcess: redirect child stdout to parent stdout in C++/Windows

  5. 5

    Block signals for complete process in a multithreaded environment

  6. 6

    Get Environment Variables of Child Process in VC++

  7. 7

    Waiting for child process to terminate, or not - C

  8. 8

    Reading file in child process in C

  9. 9

    I/O redirection from child process using pipes - winapi

  10. 10

    How to create a Child process as a specific user with Node?

  11. 11

    (C++, Windows) Spawn child process without console window (using CreateProcess)

  12. 12

    setuid create one child process

  13. 13

    c++ winapi window process inside of a class not working

  14. 14

    C++ WinApi CreateProcess Unable To Create Child Process With Environment Block

  15. 15

    Block signals for complete process in a multithreaded environment

  16. 16

    Get Environment Variables of Child Process in VC++

  17. 17

    What is the name of child process after posix fork() & win32 CreateProcess()?

  18. 18

    Reading file in child process in C

  19. 19

    CreateProcess creating process, but game is not working

  20. 20

    Memory usage of child process in C

  21. 21

    How to create a Child process as a specific user with Node?

  22. 22

    Create child process and kill it after the process is invoked

  23. 23

    (C++, Windows) Spawn child process without console window (using CreateProcess)

  24. 24

    Malloc kills child process in Linux environment

  25. 25

    PEB (Process Environment Block) invalid DllBase address

  26. 26

    C++ WinAPI - cleanup before windows shutdown terminates the process?

  27. 27

    Send Ctrl+C to child process when host was created with CREATE_NEW_PROCESS_GROUP

  28. 28

    unable to create `Environment` in `Silhouette`

  29. 29

    Creating child process in C linux

HotTag

Archive