How to stop displaying cmd when called from createproccess()

Elinor Ginzburg 106 Reputation points


I'm using windows 10, visual studio 2019 community, and I'm building an MFC application using cpp. I'm trying to add a button which will allow the user to choose a file and then will open the file for the user to view and edit. for this, I've used GetOpenFileName to get the directory and filename that the user chooses, and then I'm using CreateProcess function to open a cmd window and to pass it the full path to open the file.

I don't want the cmd to pop up, is there any way to prevent it from displaying in a new window? here's my code:

void CMFCApplication3Dlg::OnBnClickedButton2()
OPENFILENAME ofn;       // common dialog box structure
char szFile[260];       // buffer for file name
HWND hwnd = m_dialog.m_hWnd;              // owner window
HANDLE hf;              // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = (LPWSTR)szFile;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;

// Display the Open dialog box. 

std::wstring wlpstrFile = ofn.lpstrFile;

const wchar_t* ptr;
int ch = '\\';
size_t max_count = MAX_PATH;
ptr = wcsrchr(wlpstrFile.c_str(), ch);

std::wstring file_name;
file_name = wlpstrFile.substr(wcsnlen_s(wlpstrFile.c_str(), max_count) - wcsnlen_s(ptr, max_count)+1, wcsnlen_s(wlpstrFile.c_str(), max_count));

// Initialize structures for cmd activation

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

LPTSTR cmdPath = _T("C:\\Windows\\System32\\cmd.exe");
TCHAR cmdArgs[MAX_PATH] = _T("C:\\Windows\\System32\\cmd.exe /c ");
_stprintf_s(cmdArgs, _T("%s%s"), cmdArgs, file_name.c_str());

if (!CreateProcessW(cmdPath, cmdArgs, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
MessageBox(L"Create Process Failed!");


thank you very much.

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,601 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Castorix31 82,501 Reputation points


    si.wShowWindow = SW_HIDE;
    0 comments No comments

  2. Viorel 114K Reputation points

    Check an alternative:

    ShellExecuteW( hwnd, L"open", file_name.c_str(), NULL, NULL, 1)

    0 comments No comments

  3. David Lowndes 4,711 Reputation points

    I'm not sure why you're opening a cmd prompt that you don't want.
    If you want to open a document in the application registered to open that document type, just use the ShellExecute(Ex) API with the document path.

    0 comments No comments

  4. Sam of Simple Samples 5,521 Reputation points

    Do not execute cmd.exe. Use file_name as the argument, as in Creating Processes.

    0 comments No comments

  5. Flaviu_ 951 Reputation points

    You can also use CREATE_NO_WINDOW sixth parameter for CreateProcess function.

    0 comments No comments