I need the functionality of C++ function "System()" in my Pascal program. Is there any possible way of using it or sth similar?
For example I want to imitate the C++ function:
System("COLOR fc");
I need the functionality of C++ function "System()" in my Pascal program. Is there any possible way of using it or sth similar?
For example I want to imitate the C++ function:
System("COLOR fc");
The C++ library function system
starts a new process. It looks to me like the FreePascal function SysUtils.ExecuteProcess
will do what you want.
Your other alternative, if all you really want to do is change the background color on the console, is to call the Windows API function SetConsoleTextAttribute.
The problem is a bit complicated. There are several ways to approach this.
On *nix, system is available as unix.fpsystem(). (though not strictly 100% the same as the libc system function)
On Windows, FPC is native, and doesn't work through a POSIX or C library layer. Therefore there is no system() emulation on Windows.
sysutils.Executeprocess abstracts the Windows "createprocess(ex)" and the nix Exec() functions. This is not for shell expansion though, and needs full path to the binary to execute (e.g. by using fsearch or the equivalent Delphi function)
Process.TProcess is another, more involved such abstraction, accessing more features (like piping). New in the process unit in 2.6.2 are Runcommand functions which are simple tprocess wrappers for common cases.
Further, the problem is that there are many different ways to do something like this on Windows, so what to pick?
In general I would use shellexecute unless it really is commandline stuff (like this specific case), then I would use method 5.2.
Added later, verified code for executeprocess (method 5.2)
uses
Sysutils;
begin
ExecuteProcess(getenvironmentvariable('COMSPEC'), ['/C','COLOR fd']);
end.
Note that I pass the command (color fd) as one parameter, though afaik that is more important in the *nix case.
system()
invokes the platform-specific command-line processor. On Windows, for example, system("command")
calls CreateProcess()
to invoke %COMSPEC% /C "command"
, where %COMSPEC%
is either command.com
or cmd.exe
depending on Windows version.