如何编译 C 程序以在没有命令框的情况下运行?
我需要一个非常简单的程序来运行在任何版本的 Windows 上,比如说 >= Win 98,而不需要任何预安装的框架,如 dotnet。我认为用 C 来做这件事是个好主意。
程序应使用系统命令从父目录启动进程。
启动C程序(不可见)>程序启动过程>程序退出
它看起来是这样的:
#include <stdio.h>
#include <stdlib.h>
int main() {
system("..\\someprogram.exe");
return 0;
}
我从 Flash 投影仪调用该程序,它只允许启动特定子文件夹“fscommand”中的程序 - 但我必须启动与投影仪位于同一目录中的进程。
不管怎样,效果很好!但 C 程序会打开一个命令框,然后启动进程,并在进程运行时保持命令框打开。所以这就是它应该如何工作,以便我如何欣赏它:
- 根本不要打开命令框(我真的很喜欢这样;)
- 3)和4)
- 启动进程后关闭命令框(退出C 程序)
- 打开默认最小化的命令框
我无法更改 C 可执行文件的任何 Windows 设置或使用快捷方式,因为稍后将直接从 CD 运行。
我使用 Open Watcom 来编译我的程序。生成可执行文件(字符模式可执行文件/窗口可执行文件)的两种图像类型(目标选项)具有相同的结果。
I need a very simple program to run on any version of Windows, let's say >= Win 98, without requiring any pre-installed framework like dotnet. I thought C would be a great idea to do this.
The program should start a process from the parent directory by using a system command.
Start C program (invisible) > program starts process > program exits
This is how it looks:
#include <stdio.h>
#include <stdlib.h>
int main() {
system("..\\someprogram.exe");
return 0;
}
I call this program from a Flash projector, which only allows to start programs in a specific subfolder "fscommand" – but I have to start a process located in the same directory as the projector.
Anyway, it works fine! But the C program opens a command box, then starts the process and leaves the command box open as long as the process runs. So here is how it should work, in order how i would appreciate it:
- Do not open a command box at all (I'd like that, really ;)
- Both 3) and 4)
- Close the command box after starting the process (exit the C program)
- Open the command box minimized by default
I can't change any Windows settings for the C executable or use a shortcut, as this will run directly from a CD later.
I use Open Watcom to compile my program. Both image types (target options) that produce an executable (Character-mode Executable / Windowed Executable) have the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我进行了谷歌搜索,发现 http://www.ntwind.com/software/utilities/ hstart.html
您使用控制台应用程序,您可以使用
winmain()
将其更改为 Windows 应用程序您可以使用同一文件夹中文件的快捷方式,不知道为什么打折那个方法。
start
会给你一个分叉,这样你的中间应用程序就可以关闭 - 不确定win98。您可以使用
createProcess< 代替系统/code>
启动应用程序,这将避免使用
系统
命令控制台。I did a google search and found http://www.ntwind.com/software/utilities/hstart.html
Your using a console app, you could change it to a windows app using
winmain()
You can use a shortcut to a file in the same folder, not sure why your discounting that method.
start
will give you a fork so your intermediate app can close - not sure about win98 tho.Instead of system you can use
createProcess
to launch the app, theis will avoid thesystem
commands console.控制台窗口出现是因为您将程序构建为控制台应用程序。我不知道如何在 C 中避免这种情况,但在 Delphi 中,这是项目文件中的一个简单的
{$Console Off}
pragma。GCC 有一个命令行选项
-mwindows
,我认为它可以实现相同的效果,因此您可以搜索这个方向。The console window shows up because you built your program as a console application. I don't know how to avoid that in C, but in Delphi is was a simple
{$Console Off}
pragma in the project file.GCC has a command line option
-mwindows
, which I think achieves the same, so you could search into this direction.我认为 _exec 和/或 < a href="http://msdn.microsoft.com/en-us/library/20y988d2.aspx" rel="nofollow">_spawn 函数可以满足您的需求,但我不确定。
如果没有,您可以随时使用CreateProcess,尽管它可能有点在某些方面更乏味。
I think the _exec and/or _spawn functions do what you need, though I'm not sure.
If not, you can always use CreateProcess, though it can be a little more tedious in some ways.
您可以(例如)使用 hstart 而不是您自己的程序来启动该 exe 。
(这将导致根本没有黑匣子。)
You could (for example) use hstart instead of your own program to start that exe.
(This would result in no black box at all.)
带有
CREATE_NO_WINDOW
标志的CreateProcess
是您想要的,但我想添加一些内容。还支持 cmd 样式命令(例如DIR
、SET
...),这些命令没有可执行文件且无法传递给CreateProcess
单独而言,您应该调用cmd.exe /C someprogram
,其中someprogram
是可执行文件、bat 文件或命令的名称。CreateProcess
withCREATE_NO_WINDOW
flag is what you want, but I want to add something. To support also cmd style commands (such asDIR
,SET
, ... ) which have no executables and can't be passed toCreateProcess
alone, you should callcmd.exe /C someprogram
wheresomeprogram
is name of executable, bat file, or command.一位朋友想出了一个完全不同的解决方案。现在,我使用 AutoIt 和一个简短的编译脚本来启动该过程。这非常简单,启动器进程完全不可见。 :)
A friend came up with a completely different solution. Now I use AutoIt with a short compiled script to start the process. This is very simple and the launcher process is completely invisible. :)