如何编译 C 程序以在没有命令框的情况下运行?

发布于 2024-11-02 07:46:12 字数 751 浏览 2 评论 0原文

我需要一个非常简单的程序来运行在任何版本的 Windows 上,比如说 >= Win 98,而不需要任何预安装的框架,如 dotnet。我认为用 C 来做这件事是个好主意。

程序应使用系统命令从父目录启动进程。

启动C程序(不可见)>程序启动过程>程序退出

它看起来是这样的:

#include <stdio.h>
#include <stdlib.h>

int main() {
    system("..\\someprogram.exe");
    return 0;
}

我从 Flash 投影仪调用该程序,它只允许启动特定子文件夹“fscommand”中的程序 - 但我必须启动与投影仪位于同一目录中的进程。

不管怎样,效果很好!但 C 程序会打开一个命令框,然后启动进程,并在进程运行时保持命令框打开。所以这就是它应该如何工作,以便我如何欣赏它:

  1. 根本不要打开命令框(我真的很喜欢这样;)
  2. 3)和4)
  3. 启动进程后关闭命令框(退出C 程序)
  4. 打开默认最小化的命令框

我无法更改 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:

  1. Do not open a command box at all (I'd like that, really ;)
  2. Both 3) and 4)
  3. Close the command box after starting the process (exit the C program)
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谈情不如逗狗 2024-11-09 07:46:12

我进行了谷歌搜索,发现 http://www.ntwind.com/software/utilities/ hstart.html

您使用控制台应用程序,您可以使用 winmain() 将其更改为 Windows 应用程序

您可以使用同一文件夹中文件的快捷方式,不知道为什么打折那个方法。

start 会给你一个分叉,这样你的中间应用程序就可以关闭 - 不确定win98。

system("start ..\\someprogram.exe");

您可以使用 createProcess< 代替系统/code>启动应用程序,这将避免使用系统命令控制台。

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

// Start the child process. 
if( !CreateProcess( "..\\someprogram.exe",   // module name 
    NULL,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
) 
{
    printf( "CreateProcess failed (%d).\n", GetLastError() );
    return;
}

// Wait until child process exits.  In your case you don't care to wait anyway
// WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

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.

system("start ..\\someprogram.exe");

Instead of system you can use createProcess to launch the app, theis will avoid the system commands console.

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

// Start the child process. 
if( !CreateProcess( "..\\someprogram.exe",   // module name 
    NULL,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
) 
{
    printf( "CreateProcess failed (%d).\n", GetLastError() );
    return;
}

// Wait until child process exits.  In your case you don't care to wait anyway
// WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
痴者 2024-11-09 07:46:12

控制台窗口出现是因为您将程序构建为控制台应用程序。我不知道如何在 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.

稀香 2024-11-09 07:46:12

我认为 _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.

旧时光的容颜 2024-11-09 07:46:12

您可以(例如)使用 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.)

So要识趣 2024-11-09 07:46:12

带有 CREATE_NO_WINDOW 标志的 CreateProcess 是您想要的,但我想添加一些内容。还支持 cmd 样式命令(例如 DIRSET...),这些命令没有可执行文件且无法传递给 CreateProcess单独而言,您应该调用 cmd.exe /C someprogram,其中 someprogram 是可执行文件、bat 文件或命令的名称。

CreateProcess with CREATE_NO_WINDOW flag is what you want, but I want to add something. To support also cmd style commands (such as DIR, SET, ... ) which have no executables and can't be passed to CreateProcess alone, you should call cmd.exe /C someprogram where someprogram is name of executable, bat file, or command.

不喜欢何必死缠烂打 2024-11-09 07:46:12

一位朋友想出了一个完全不同的解决方案。现在,我使用 AutoIt 和一个简短的编译脚本来启动该过程。这非常简单,启动器进程完全不可见。 :)

filename = ..\someprogram.exe

if FileExist(filename) {    
    Run, open %filename%
}

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. :)

filename = ..\someprogram.exe

if FileExist(filename) {    
    Run, open %filename%
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文