Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I run an executable program from a c program? 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have an DOS executable program, that I would like to run from a windows C program. I'm not that good at writing windows based progams in C. i was wondering if someone can help me out. I just need a windows look to run my already working DOS based C application. PLEASE HELP!! (Either some source, or a tip to a command that will run an exe will be great).
 
One way is to call the function "CreateProcess" (Working with C++ Builder). Check the Online Help for parameters.

An other possibility is the function "spawnl".

But all the functions are working on 32-Bit Systems only.
[sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
i was going to give you a concise answer with header files and examples. there is a function called [tt]execl[\tt] and [tt]execvp[/tt] but the book i have at home doesn't have them in it. if you ahve access to a (u/li)nux system try looking up the man pages on them. if nobody else replies with them before i get to uni i'll post the info
[sig]<p>Chris Packham<br><a href=mailto:kriz@i4free.co.nz>kriz@i4free.co.nz</a><br><a href= > </a><br>A thousand mokeys at a thousand machines, it happened, it got called the internet.[/sig]
 
From the solaris man pages:
NAME
exec, execl, execv, execle, execve, execlp, execvp, - exe-
cute a file

SYNOPSIS
#include <unistd.h>

int execl(const char *path, const char *arg0, ..., const
char *argn, char * /*NULL*/);

int execv(const char *path, char *const argv[]);

int execle(const char *path, char *const arg0[], ..., const
char *argn, char * /*NULL*/, char *const envp[]);

int execve(const char *path, char *const argv[], char *const
envp[]);

int execlp(const char *file, const char *arg0, ..., const
char *argn, char * /*NULL*/);

int execvp(const char *file, char *const argv[]);

DESCRIPTION
Each of the functions in the exec family overlays a new pro-
cess image on an old process. The new process image is con-
structed from an ordinary, executable file. This file is
either an executable object file, or a file of data for an
interpreter. There can be no return from a successful call
to one of these functions because the calling process image
is overlaid by the new process image.

An interpreter file begins with a line of the form

#! pathname [arg]

where pathname is the path of the interpreter, and arg is an
optional argument. When an interpreter file is executed, the
system invokes the specified interpreter. The pathname
specified in the interpreter file is passed as arg0 to the
interpreter. If arg was specified in the interpreter file,
it is passed as arg1 to the interpreter. The remaining argu-
ments to the interpreter are arg0 through argn of the origi-
nally exec'd file. The interpreter named by pathname must
not be an interpreter file.

When a C program is executed, it is called as follows:

int main (int argc, char *argv[], char *envp[]);

where argc is the argument count, argv is an array of char-
acter pointers to the arguments themselves, and envp is an
array of character pointers to the environment strings. As
indicated, argc is at least one, and the first member of the
array points to a string containing the name of the file.

The arguments arg0, ..., argn point to null-terminated char-
acter strings. These strings constitute the argument list
available to the new process image. Conventionally at least
arg0 should be present. It will become the name of the pro-
cess, as displayed by the ps(1) command. The arg0 argument
points to a string that is the same as path (or the last
component of path). The list of argument strings is ter-
minated by a (char *)0 argument.

The argv argument is an array of character pointers to
null-terminated strings. These strings constitute the argu-
ment list available to the new process image. By convention,
argv must have at least one member, and it should point to a
string that is the same as path (or its last component).
The argv argument is terminated by a null pointer.

The envp argument is an array of character pointers to
null-terminated strings. These strings constitute the
environment for the new process image. The envp argument is
terminated by a null pointer. For execl(), execv(),
execvp(), and execlp(), the C run-time start-off routine
places a pointer to the environment of the calling process
in the global object extern char **environ, and it is used
to pass the environment of the calling process to the new
process.

The path argument points to a path name that identifies the
new process file.

The file argument points to the new process file. If file
does not contain a slash character, the path prefix for this
file is obtained by a search of the directories passed in
the PATH environment variable (see environ(5)). The environ-
ment is supplied typically by the shell. If the new process
file is not an executable object file, execlp() and execvp()
use the contents of that file as standard input to the
shell. In a standard-conforming application (see stan-
dards(5)), the exec family of functions use /usr/bin/ksh
(see ksh(1)); otherwise, they use /usr/bin/sh (see sh(1)).

File descriptors open in the calling process remain open in
the new process, except for those whose close-on-exec flag
is set; (see fcntl(2)). For those file descriptors that
remain open, the file pointer is unchanged.

Signals that are being caught by the calling process are set
to the default disposition in the new process image (see
signal(3C)). Otherwise, the new process image inherits the
signal dispositions of the calling process.

The saved resource limits in the new process image are set
to be a copy of the process's corresponding hard and soft
resource limits.

If the set-user-ID mode bit of the new process file is set
(see chmod(2)), the effective user ID of the new process is
set to the owner ID of the new process file. Similarly, if
the set-group-ID mode bit of the new process file is set,
the effective group ID of the new process is set to the
group ID of the new process file. The real user ID and real
group ID of the new process remain the same as those of the
calling process.

If the effective user-ID is root or super-user, the set-
user-ID and set-group-ID bits will be honored when the pro-
cess is being controlled by ptrace.

The shared memory segments attached to the calling process
will not be attached to the new process (see shmop(2)).
Memory mappings in the calling process are unmapped before
the new process begins execution (see mmap(2)).

Profiling is disabled for the new process; see profil(2).

Timers created by timer_create(3R) are deleted before the
new process begins execution.

Any outstanding asynchronous I/O operations may be can-
celled.

The new process also inherits the following attributes from
the calling process:

o nice value (see nice(2))

o scheduler class and priority (see priocntl(2))

o process ID

o parent process ID

o process group ID

o supplementary group IDs

o semadj values (see semop(2))

o session ID (see exit(2) and signal(3C))

o trace flag (see ptrace(2) request 0)
o time left until an alarm (see alarm(2))

o current working directory

o root directory

o file mode creation mask (see umask(2))

o resource limits (see getrlimit(2))

o utime, stime, cutime, and cstime (see times(2))

o file-locks (see fcntl(2) and lockf(3C))

o controlling terminal

o process signal mask (see sigprocmask(2))

o pending signals (see sigpending(2))

Upon successful completion, each of the functions in the
exec family marks for update the st_atime field of the file,
unless the file is on a read-only file system. Should the
function succeed, the process image file is considered to
have been opened by the open(2) system called. The
corresponding close() is considered to occur at a time after
this open, but before process termination or successful com-
pletion of a subsequent call to one of the functions in the
exec family.

RETURN VALUES
If a function in the exec family returns to the calling pro-
cess, an error has occurred; the return value is -1 and
errno is set to indicate the error.
[sig]<p>Chris Packham<br><a href=mailto:kriz@i4free.co.nz>kriz@i4free.co.nz</a><br><a href= > </a><br>A thousand mokeys at a thousand machines, it happened, it got called the internet.[/sig]
 
If you want to Start a Dos Application from a Windows program you have to Create a Dos Window and Start the Dos Application in this Window. This Job will be performed by the &quot;CreateProcess&quot; function. I did it some times before.

Remember: We are talking about a Windows 9x/NT environment and C++ Borland Compiler. [sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top