Try
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
Try
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
Check an alternative:
ShellExecuteW( hwnd, L"open", file_name.c_str(), NULL, NULL, 1)
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.
Do not execute cmd.exe
. Use file_name
as the argument, as in Creating Processes.
You can also use CREATE_NO_WINDOW
sixth parameter for CreateProcess function.