简体   繁体   English

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

[英]How Do I Compile a C Program to Run Without a Command Box?

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. 我需要一个非常简单的程序即可在任何版本的Windows上运行,例如> = Win 98,而无需任何诸如dotnet之类的预安装框架。 I thought C would be a great idea to do this. 我认为C是执行此操作的好主意。

The program should start a process from the parent directory by using a system command. 该程序应使用系统命令从父目录启动进程。

Start C program (invisible) > program starts process > program exits 启动C程序(不可见)>程序启动过程>程序退出

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. 我从Flash投影仪调用此程序,该程序仅允许在特定的子文件夹“ fscommand”中启动程序-但是我必须启动与投影仪位于同一目录中的进程。

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. 但是C程序会打开一个命令框,然后启动进程,并在进程运行时保持打开状态。 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)和4)
  3. Close the command box after starting the process (exit the C program) 启动该过程后关闭命令框(退出C程序)
  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. 我无法更改C可执行文件的任何Windows设置,也无法使用快捷方式,因为它将在以后直接从CD运行。

I use Open Watcom to compile my program. 我使用Open Watcom编译程序。 Both image types (target options) that produce an executable (Character-mode Executable / Windowed Executable) have the same result. 产生可执行文件(字符模式可执行文件/窗口可执行文件)的两种图像类型(目标选项)具有相同的结果。

I think the _exec and/or _spawn functions do what you need, though I'm not sure. 认为 _exec和/或_spawn功能做你需要什么,尽管我不知道。

If not, you can always use CreateProcess , though it can be a little more tedious in some ways. 如果没有,您可以始终使用CreateProcess ,尽管在某些方面它可能有点乏味。

You could (for example) use hstart instead of your own program to start that exe. 您可以(例如)使用hstart而不是您自己的程序来启动该exe。

(This would result in no black box at all.) (这将导致根本没有黑匣子。)

I did a google search and found http://www.ntwind.com/software/utilities/hstart.html 我做了一次Google搜索,发现http://www.ntwind.com/software/utilities/hstart.html

Your using a console app, you could change it to a windows app using winmain() 您使用控制台应用程序时,可以使用winmain()将其更改为Windows应用程序

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. start将为您提供帮助,以便您的中间应用程序可以关闭-不确定win98 tho。

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

Instead of system you can use createProcess to launch the app, theis will avoid the system commands console. 您可以使用createProcess来启动应用程序,而不必使用system ,而是避免使用system命令控制台。

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 );

CreateProcess with CREATE_NO_WINDOW flag is what you want, but I want to add something. 您想要的是带有CREATE_NO_WINDOW标志的CreateProcess ,但是我想添加一些东西。 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. 要还支持没有可执行文件且不能单独传递给CreateProcess cmd样式命令(例如DIRSET ,...),则应调用cmd.exe /C someprogram ,其中someprogram是可执行文件的名称,bat文件,或命令。

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. 我不知道如何在C语言中避免这种情况,但是在Delphi中,项目文件中只是一个简单的{$Console Off}编译指示。

GCC has a command line option -mwindows , which I think achieves the same, so you could search into this direction. GCC有一个命令行选项-mwindows ,我认为可以达到相同的效果,因此您可以搜索这个方向。

A friend came up with a completely different solution. 一个朋友想出了一个完全不同的解决方案。 Now I use AutoIt with a short compiled script to start the process. 现在,我将AutoIt与简短的编译脚本一起使用来启动该过程。 This is very simple and the launcher process is completely invisible. 这非常简单,并且启动程序过程完全不可见。 :) :)

filename = ..\someprogram.exe

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM