我写了一个控制台程序,怎样让DOS窗口不显示?

Ljfeng 2003-05-30 03:29:15
我希望控制台程序在后台运行,但它总是有个DOS窗口,怎样让DOS窗口不显示?
...全文
240 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hucsdn 2003-05-31
  • 打赏
  • 举报
回复
用CreateProcess创建进程,将标准输出重定向到一个窗口,在参数设置中设置创建窗口的属性为隐藏。
dizzo 2003-05-31
  • 打赏
  • 举报
回复
老大来了,学习ing
GoogleGeek 2003-05-31
  • 打赏
  • 举报
回复
6.4: How do I run another program from my program? (top)

There are several functions that run other programs. The simplest is WinExec():

WinExec ( "C:\\path\\to\\program.exe", SW_SHOWNORMAL );
There is also ShellExecute(), which can run executables as well as files that are associated with a program. For example, you can "run" a text file, as shown here:

ShellExecute ( hwndYourWindow, "open",
"C:\\path\\to\\readme.txt",
NULL, NULL, SW_SHOWNORMAL );
In this example, ShellExecute() looks up the program associated with .TXT files and runs that program. ShellExecute() also lets you set the program's starting directory and additional command line parameters. See the MSDN docs on ShellExecute() for more info.

If you want complete control over every aspect of the program launching process, use CreateProcess(). CreateProcess() has a ton of options, so see MSDN for all the details. Here is a simple example:

STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = {0};
BOOL bSuccess;

bSuccess = CreateProcess ( NULL, "\"C:\\Program Files\\dir\\program.exe\"",
NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
NULL, NULL, &si, &pi );
Note that the program name should be enclosed in quotes, as shown above, if the path contains spaces.

If CreateProcess() succeeds, be sure to close the handles in the PROCESS_INFORMATION structure once you don't need them anymore.

CloseHandle ( pi.hThread );
CloseHandle ( pi.hProcess );
Of course, if all you need to do is just run a program, CreateProcess() is probably overkill, and ShellExecute() would be sufficient.

GoogleGeek 2003-05-31
  • 打赏
  • 举报
回复
Q:How can I run my console program without the console window popping up? (top)

A:Use CreateProcess() (as described in FAQ 6.4), and set some members in the STARTUPINFO struct to tell Windows to hide the console window.

STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = {0};
BOOL bSuccess;

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

// see FAQ 6.4 for all the parameters
bSuccess = CreateProcess ( ..., &si, &pi );
If the CreateProcess() call succeeds, the console window will be hidden because we specified SW_HIDE in the wShowWindow member.
windows3000 2003-05-31
  • 打赏
  • 举报
回复
up
masterz 2003-05-30
  • 打赏
  • 举报
回复
把main改成winmain

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧