How to stop displaying cmd when called from createproccess()

Elinor Ginzburg 106 Reputation points
2021-09-13T10:25:18.39+00:00

hello,

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;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box. 
GetOpenFileName(&ofn);

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
STARTUPINFO si;
PROCESS_INFORMATION pi;

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.

C++
C++
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,537 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Castorix31 81,741 Reputation points
    2021-09-13T11:03:14.807+00:00

    Try

    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    
    0 comments No comments

  2. Viorel 112.5K Reputation points
    2021-09-13T11:04:56.84+00:00

    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
    2021-09-13T11:05:40.33+00:00

    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,516 Reputation points
    2021-09-13T18:52:26.853+00:00

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

    0 comments No comments

  5. Flaviu_ 911 Reputation points
    2021-09-14T06:26:50.15+00:00

    You can also use CREATE_NO_WINDOW sixth parameter for CreateProcess function.

    0 comments No comments