tophermiller
asked on
Creating a console process without an MS-DOS window
I want to use the CreateProcess API to create a Windows NT/95/98 process that does not have any UI, and does not pop up an MS-DOS window. How do I do this?
I used the VC++ AppWizard for console process to create my process, but it is not doing any I/O so I do not want the window visible.
I used the VC++ AppWizard for console process to create my process, but it is not doing any I/O so I do not want the window visible.
Use DETACHED_PROCESS as creation flag in CreateProcess call.
ASKER
I tried using DETACHED_PROCESS and it doesn't have the desired effect. I still get an MS-DOS window popping up.
Try the following code.
STARTUPINFO StartupInfo =
{
sizeof(STARTUPINFO),
NULL,
NULL,
NULL,
0, 0,
0, 0,
0, 0,
0,
STARTF_USESHOWWINDOW,
SW_HIDE,
0,
NULL,
NULL,
NULL,
NULL
};
PROCESS_INFORMATION ProcessInformation;
::CreateProcess(NULL,
_T("app.exe"),
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&StartupInfo,
&ProcessInformation);
STARTUPINFO StartupInfo =
{
sizeof(STARTUPINFO),
NULL,
NULL,
NULL,
0, 0,
0, 0,
0, 0,
0,
STARTF_USESHOWWINDOW,
SW_HIDE,
0,
NULL,
NULL,
NULL,
NULL
};
PROCESS_INFORMATION ProcessInformation;
::CreateProcess(NULL,
_T("app.exe"),
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&StartupInfo,
&ProcessInformation);
chensu, your solution w/o DETACHED_PROCESS will display console.
STARTUPINFO doesn't matter.
It seems that tophermiller just doesn't want to pay his points.
Here is working sample
//---------------------
// b.exe parent process (window)
//---------------------
#include <windows.h>
int WINAPI WinMain(HINSTANCE,HINSTANC E,LPSTR,in t)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(si)) ;
si.cb = sizeof(si);
CreateProcess(NULL,"a.exe" ,NULL,NULL ,FALSE,
DETACHED_PROCESS,NULL,NULL ,&si,&pi);
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}#include <windows.h>
//------------------
// a.exe child process (console)
//------------------
void main()
{
for(;;) {
MessageBeep(-1);
Sleep(2000);
}
}
STARTUPINFO doesn't matter.
It seems that tophermiller just doesn't want to pay his points.
Here is working sample
//---------------------
// b.exe parent process (window)
//---------------------
#include <windows.h>
int WINAPI WinMain(HINSTANCE,HINSTANC
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(si))
si.cb = sizeof(si);
CreateProcess(NULL,"a.exe"
DETACHED_PROCESS,NULL,NULL
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return 0;
}#include <windows.h>
//------------------
// a.exe child process (console)
//------------------
void main()
{
for(;;) {
MessageBeep(-1);
Sleep(2000);
}
}
ASKER
Thanks NickRepin.
I have no problem paying my points. I just wanted to be sure to get to the bottom of it. It turned out to be my bad, in that I was putting DETACHED_PROCESS into the STARTUP_INFORMATION flags instead of the creation flags in the CreateProcess call. Your sample code alerted me to that.
Anyway, I'm rejecting chensu's answer, in the hopes that it will allow you to get the points instead.
I have no problem paying my points. I just wanted to be sure to get to the bottom of it. It turned out to be my bad, in that I was putting DETACHED_PROCESS into the STARTUP_INFORMATION flags instead of the creation flags in the CreateProcess call. Your sample code alerted me to that.
Anyway, I'm rejecting chensu's answer, in the hopes that it will allow you to get the points instead.
Have you tried my code? The following is a complete version. ipconfig.exe is a Win32 console application in the Windows directory.
STARTUPINFO StartupInfo =
{
sizeof(STARTUPINFO),
NULL,
NULL,
NULL,
0, 0,
0, 0,
0, 0,
0,
STARTF_USESHOWWINDOW,
SW_HIDE,
0,
NULL,
NULL,
NULL,
NULL
};
PROCESS_INFORMATION ProcessInformation;
if (::CreateProcess(NULL,
_T("ipconfig.exe"),
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&StartupInfo,
&ProcessInformation))
{
::CloseHandle(ProcessInfor mation.hPr ocess);
::CloseHandle(ProcessInfor mation.hTh read);
}
else
{
LPVOID lpMsgBuf;
::FormatMessage(FORMAT_MES SAGE_ALLOC ATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM ,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
// Display the string.
::AfxMessageBox((LPCTSTR)l pMsgBuf);
// Free the buffer.
::LocalFree(lpMsgBuf);
}
STARTUPINFO StartupInfo =
{
sizeof(STARTUPINFO),
NULL,
NULL,
NULL,
0, 0,
0, 0,
0, 0,
0,
STARTF_USESHOWWINDOW,
SW_HIDE,
0,
NULL,
NULL,
NULL,
NULL
};
PROCESS_INFORMATION ProcessInformation;
if (::CreateProcess(NULL,
_T("ipconfig.exe"),
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&StartupInfo,
&ProcessInformation))
{
::CloseHandle(ProcessInfor
::CloseHandle(ProcessInfor
}
else
{
LPVOID lpMsgBuf;
::FormatMessage(FORMAT_MES
FORMAT_MESSAGE_FROM_SYSTEM
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL);
// Display the string.
::AfxMessageBox((LPCTSTR)l
// Free the buffer.
::LocalFree(lpMsgBuf);
}
Try it. It does work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.