[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1062
  • Last Modified:

spawnlp

Hello,
Well I'm on my last leg. I've been trying for months to get my C++ program using CreateProcess to open a Dos program and let it use the COm ports. Well nothing works as they should. I have been told that spawnlp works fine with a Dos program and the com ports. I don't have much experenice on this and would like someone to help me with some code? What I need is when my C++ program calls the spawnlp, it should open the DOs program and have nothing to do with the parent "C++" program. Once I close the DOs program it should go back to windows. The OS I use is winodws 95\98.

// The .exe path
"C:\\Hi-Tech Software\\Cba\\CbaTool.exe",

The Dos program is setup to "close on exit".

Thanks
Clinton
0
Clinton112299
Asked:
Clinton112299
  • 14
  • 11
  • 2
1 Solution
 
nietodCommented:
I assure you you can do this with CreateProcess(), in fact spawnlp() uses CreateProcess() itself.

But if you want...

_spawnlp(_P_WAIT,"C:\\Hi-Tech Software\\Cba\\CbaTool.exe",NULL);

Let me know if you have questions or problems with it.
0
 
Clinton112299Author Commented:
nietod,
THis is what I tried but nothing opens? This is the exact path for the Dos program and I had to remove the _ from spawn and P_Wait. What have I done wrong?
Thanks
Clinton

void __fastcall TFormMain::Button15Click(TObject *Sender)
{
spawnlp(P_WAIT,"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOOL\\CBATOOL.EXE",NULL);
}
//---------------------------------------------------------------------------
0
 
nietodCommented:
The Undersrcores ("_") are needed if you do this from VC.   (These are non-standard library functions and they should, technically, have the underscores.  But Borland does not do that--yet.

What does spawnlp() return?  If it is -1, what is the error number (errno)?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Clinton112299Author Commented:
nietod,
Yes there is an error but I'm trying to figure out how to tell me which error it is. I don't have any documation on spawn.

Thanks
Clinton
0
 
nietodCommented:
The VC docs are:
**********************
_spawnlp, _wspawnlp
Create and execute a new process.

int _spawnlp( int mode, const char *cmdname, const char *arg0, const char *arg1, ... const char *argn, NULL );

int _wspawnlp( int mode, const wchar_t *cmdname, const wchar_t *arg0, const wchar_t *arg1, ... const wchar_t *argn, NULL );

Routine Required Header Compatibility
_spawnlp <process.h> Win 95, Win NT
_wspawnlp <stdio.h> or <wchar.h> Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

The return value from a synchronous _spawnlp or _wspawnlp (_P_WAIT specified for mode) is the exit status of the new process. The return value from an asynchronous _spawnlp or _wspawnlp (_P_NOWAIT or _P_NOWAITO specified for mode) is the process handle. The exit status is 0 if the process terminated normally. You can set the exit status to a nonzero value if the spawned process specifically calls the exit routine with a nonzero argument. If the new process did not explicitly set a positive exit status, a positive exit status indicates an abnormal exit with an abort or an interrupt. A return value of -1 indicates an error (the new process is not started). In this case, errno is set to one of the following values:

E2BIG

Argument list exceeds 1024 bytes

EINVAL

mode argument is invalid

ENOENT

File or path is not found

ENOEXEC

Specified file is not executable or has invalid executable-file format

ENOMEM

Not enough memory is available to execute new process

Parameters

mode

Execution mode for calling process

cmdname

Path of file to be executed

arg0, ... argn

List of pointers to arguments

Remarks

Each of these functions creates and executes a new process, passing each command-line argument as a separate parameter and using the PATH environment variable to find the file to execute.

0
 
KangaRooCommented:
Borland (BC5) docs:

spawnl, spawnle, spawnlp, spawnlpe, spawnv, spawnve, spawnvp, spawnvpe

Syntax

#include <process.h>
#include <stdio.h>
int spawnl(int mode, char *path, char *arg0, arg1, ..., argn, NULL);
int spawnle(int mode, char *path, char *arg0, arg1, ..., argn, NULL, char *envp[]);
int spawnlp(int mode, char *path, char *arg0, arg1, ..., argn, NULL);
int spawnlpe(int mode, char *path, char *arg0, arg1, ..., argn, NULL, char *envp[]);
int spawnv(int mode, char *path, char *argv[]);
int spawnve(int mode, char *path, char *argv[], char *envp[]);
int spawnvp(int mode, char *path, char *argv[]);

int spawnvpe(int mode, char *path, char *argv[], char *envp[]);


Note:      In spawnle, spawnlpe, spawnv, spawnve, spawnvp, and spawnvpe, the last string must be NULL.

Description

The functions in the spawn... family create and run (execute) other files, known as child processes. There must be sufficient memory available for loading and executing a child process.
The value of mode determines what action the calling function (the parent process) takes after the spawn... call. The possible values of mode are

P_WAIT      Puts parent process on hold until child process completes execution.
P_NOWAIT      Continues to run parent process while child process runs. The child process ID is returned, so that the parent can wait for completion using cwait or wait. This mode is currently not available for 16-bit Windows or 16-bit DOS; using it generates an error value.
P_NOWAITO      Identical to P_NOWAIT except that the child process ID isn't saved by the operating system, so the parent process can't wait for it using cwait or wait.

P_DETACH      Identical to P_NOWAITO, except that the child process is executed in the background with no access to the keyboard or the display.
P_OVERLAY      Overlays child process in memory location formerly occupied by parent. Same as an exec... call.

path is the file name of the called child process. The spawn... function calls search for path using the standard operating system search algorithm:

      If there is no extension or no period, they search for an exact file name. If the file is not found, they search for files first with the extension EXE, then COM, and finally BAT.
      If an extension is given, they search only for the exact file name.
      If only a period is given, they search only for the file name with no extension.
      If path does not contain an explicit directory, spawn... functions that have the p suffix search the current directory, then the directories set with the operating system PATH environment variable.

The suffixes p, l, and v, and e added to the spawn... "family name" specify that the named function operates with certain capabilities.

p      The function searches for the file in those directories specified by the PATH environment variable. Without the p suffix, the function searches only the current working directory.
l      The argument pointers arg0, arg1, ..., argn are passed as separate arguments. Typically, the l suffix is used when you know in advance the number of arguments to be passed.
v      The argument pointers argv[0], ..., arg[n] are passed as an array of pointers. Typically, the v suffix is used when a variable number of arguments is to be passed.

e      The argument envp can be passed to the child process, letting you alter the environment for the child process. Without the e suffix, child processes inherit the environment of the parent process.

Each function in the spawn... family must have one of the two argument-specifying suffixes (either l or v). The path search and environment inheritance suffixes (p and e) are optional.
For example:

      spawnl takes separate arguments, searches only the current directory for the child, and passes on the parent's environment to the child.
      spawnvpe takes an array of argument pointers, incorporates PATH in its search for the child process, and accepts the envp argument for altering the child's environment.

The spawn... functions must pass at least one argument to the child process (arg0 or argv[0]). This argument is, by convention, a copy of path
.. (Using a different value for this 0   argument won't produce an error.) If you want to pass an empty argument list to the child process, then arg0 or argv[0] must be NULL.
Under DOS 3.x, path is available for the child process; under earlier versions, the child process cannot use the passed value of the 0   argument (arg0 or argv[0]).

When the l suffix is used, arg0 usually points to path, and arg1, ...., argn point to character strings that form the new list of arguments. A mandatory null following argn marks the end of the list.
When the e suffix is used, you pass a list of new environment settings through the argument envp. This environment argument is an array of character pointers. Each element points to a null-terminated character string of the form

envvar = value

where envvar is the name of an environment variable, and value is the string value to which envvar is set. The last element in envp[] is null. When envp is null, the child inherits the parents' environment settings.
The combined length of arg0 + arg1 + ... + argn (or of argv[0] + argv[1] + ... + argv[n]), including space characters that separate the arguments, must be less than 260 bytes for Windows (128 for DOS). Null-terminators are not counted.

When a spawn... function call is made, any open files remain open in the child process.

Return Value

When successful, the spawn... functions, where mode is P_WAIT, return the child process' exit status (0 for a normal termination). If the child specifically calls exit with a nonzero argument, its exit status can be set to a nonzero value.
If mode is P_NOWAIT or P_NOWAITO, the spawn functions return the process ID of the child process. The ID obtained when using P_NOWAIT can be passed to cwait.
On error, the spawn... functions return -1, and the global variable errno is set to one of the following values:

E2BIG      Arg list too long
EINVAL      Invalid argument
ENOENT      Path or file name not found
ENOEXEC      Exec format error
ENOMEM      Not enough memory
0
 
nietodCommented:
Thanks.  Pretty mmuch the same though.  Same error codes, which is what he needs to be looking at.
0
 
Clinton112299Author Commented:
Nietod and Kangaroo,

This is what I have which now opens the Dos program;

void __fastcall TFormMain::Button15Click(TObject *Sender)
{
spawnlp(P_WAIT, "C:\\Hi-Tec~1\\JCM\\CBA\\CBATOOL\\CBATOOL.EXE",
"C:\\Hi-Tec~1\\JCM\\CBA\\CBATOOL\\CBATOOL.EXE", NULL);
}

My orginal problem was that the path name was to long. BUT I have the same problem as before with the DOs program not opeing the COm ports once launched with spwanlp. Why is this?? What do I have to do to sovle this problem?

Thanks
Clinton
0
 
nietodCommented:
Does the program open the COM ports okay when launched directly?  Could some other program have the COM port open?  Like the program that does the launching?  Only one program can have the COM port open at a time.   Did you write this DOS program, if so can you get the DOS error code that occurs when it fails?

As I indicted at the start, spawnlp() is just going to "convert" the parameters and then call CreateProcess(), so you really haven't changed much.
0
 
Clinton112299Author Commented:
nietod,
I didn't write any of the Dos programs. And yes, if I open the Dos program from Windows Explorer it works great no problems. The problem comes into play when I use CreateProcess and Borland C++ that calls the Dos .exe. I only get bad data this way. But if I call a Batch File from CreateProcess which in turns opens the DOs program it works? But the down side of this is that once the DOs program closes, the com port stays open to any Win32 app. I can't believe that no one has ever came across this? This was my CreateProcess that called the DOs .exe. Can you see if I have missed anything? By what I have read, to me I should be able to launch the DOs program and have no ties to windows.

Thanks
Clinton

void __fastcall TFormMain::ButtonCBAToolClick(TObject *Sender)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

 memset (&pi, 0, sizeof(pi));
 memset (&si, 0, sizeof(si));

 si.cb = sizeof(si);
 si.dwFlags |=STARTF_USESHOWWINDOW;
 si.wShowWindow|=SW_SHOWNORMAL;


 if ( !CreateProcess (
            NULL,
            "C:\\Hi-Tech Software\\JCM\\CBA\\CBATOOL\\CBATOOL.EXE",
            NULL,
            NULL,
            false,
            CREATE_NEW_CONSOLE,
            NULL,
            NULL,
            &si,
            &pi))
            {
          MessageBox(0, "Could not execte program \n Please contact software author.", "Error", MB_ICONSTOP);
            }
    {
    CloseHandle(&pi.hProcess);
    CloseHandle(&pi.hThread);
    }

}

//---------------------------------------------------------------------------
0
 
nietodCommented:
Try specifying CREATE_NEW_PROCESS_GROUP and if on NT also CREATE_SEPERATE_WOW_VDM
0
 
Clinton112299Author Commented:
Nietod,
Just to understand this I would replace the current line of CREATE_NEW_CONSOLE with CREATE_NEW_PROCESS_GROUP ? I'm only runnign this on Windows 95\98.

Thanks
Clinton
0
 
nietodCommented:
No, specify both flags.   (This is just a guess, by-the-way.)
0
 
Clinton112299Author Commented:
nietod,

THis is what I've tried:
CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,

Does this look correct? The bar in between is the pipe sign. I still don't know why it doesn't work! But I have been trying differetn things and it seems to work with a batch file. Do you have any experenice in batch file? I would like to know if what I have is correct or not.

Thanks
Clinton

REM  Fill in size
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);  
 
REM  Get OS version info
GetVersionEx( &osvi );                                
                                 
REM  Start a DOS box
if( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT )            

REM  If it's WinNT
sprintf(commandLine,"cmd /C %s", commandString);

REM  Use CMD.EXE
else                                  

REM  Use command.com for Win95
sprintf(commandLine,"c:\\command.com /C %s",commandString);
bStatus = CreateProcess( NULL, commandLine, NULL, NULL, TRUE,0,
NULL, NULL, &si, &pi );

@echo off
cls
C:
CD  "\Hi-Tech Software\JCM\CBA\CBATOOL"
CBATOOL.exe

0
 
nietodCommented:
>> Does this look correct? The bar in
>> between is the pipe sign.
That is correct.  it s the C/C++ bitwise or operator.

>> I still don't know why it doesn't work!
Neither do I.  This is strange.

>> Do you have any experenice in batch file?
Yes, but I don't see how that is going to help.


In the code above you are creating a command process that runs another process.  is that the case that works or doesn't work?  Have you tried to make sure that that the correct command interpretter is used.  For example, in one case you are not specifying a path.  in another case you are specifying the root path.  But often the command interpretter is located in the windows directories.
0
 
Clinton112299Author Commented:
nietod,
The Batch file was given to me for this problem, but I don't know if it works or not. What I have been told was try and open the Batch file with CreateProcess and then open the Dos program from the Batch file? I really don't know much about batch files.My OS is 95\98.

Thanks
Clinton
0
 
nietodCommented:
>> I don't know if it works or not
Lets start with that issue.  Test it by opeing a DOS box (command prompt) and typing the name of the batch file (with path if necessary) and pressing enter.  That should execture the batch file which should then exectute the program.  Does that work okay?
0
 
Clinton112299Author Commented:
Nietod,
Yes, it opens the program but gives me bad command on all of it until I reach this part:

@echo off
cls
C:
CD  "\Hi-Tech Software\JCM\CBA\CBATOOL"
CBATOOL.exe

Clinton
0
 
nietodCommented:
Opps.  I thought it was weird that you had presented the info in that way.

That code you posted is not entirely a batch file.  The end of it is a batch file.  The beginning of it is VB code that is supposed to run that batch file (or at least is supposed to run something, it is missing some details.)

The @echo off is the start of the batch file.

Above that is code to run the command interpretter and pass it the name of a batch file to be run.

Perhaps a better approach is to run the command interpretter but pass it the name of the DOS program to be run.  This would be done by running command.exe (or cmd.exe in NT) and passing it a command line that begins with the parameter "/C" followed by the path and name of the DOS program to be run.
0
 
KangaRooCommented:
Would that not run async?
0
 
Clinton112299Author Commented:
I'm looking for any suggestion "examples" on this. I guess what would you have for code?

Thanks
Clinton
0
 
nietodCommented:
>> Would that not run async?
Yes it would run asynch, but that can be dealt with easily once the other problems are solved.

CreateProcess(NULL,"c:\\command.com /C \\Hi-Tech Software\\JCM\CBA\\CBATOOL\\CBATOOL.EXE",NULL,NULL,FALSE,CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP | CREATE_SEPERATE_WOW_VDM,NULL,"\\Hi-Tech Software\\JCM\\CBA\\CBATOOL\\CBATOOL.EXE",&StartUpInfo,&ProcessInfo);

Obvipusly the strings passed here would not be hardcoded, you would have to form them depending on the name and location of the command interpretter and the name and location of the program to be run.  But you might wan to use hardcoded names etc at first until this is working,
0
 
Clinton112299Author Commented:
nietod,
Thanks for the example. Just to understand what's happing; the example will open the Dos program from my C++ without the Batch file, and in a new lpenviroment? I thought that VDM is only for NT? Once I have this working "I'll try later tonight" what is asynch?

Thanks
Clinton
0
 
nietodCommented:
>> will open the Dos program from my C++
>> without the Batch file,
Yes.

>>  and in a new lpenviroment?
The example uses the same environment.  But you can specify a new one.  Does the DOS program use the environment options in any way?

>> I thought that VDM is only for NT
It is only used by NT, 95 and 98 will ignore it.   But you might was well specify it for when you run under NT.  And 2000 will probably use it too.
0
 
nietodCommented:
If you don't use COM, then name decoration will be an issue.  Here is a description of name decoration I wrote earlier.  It discusses calling C++ from C, but the issue is the same as calling it from VB

************************
C++ "decorates" or "mangles" function names by appending a "code" to the end of the name that expresses the parameters passed to the function.  This is used to impliment function overloading, where 2 or more distinct functions have the same name and different parameters.  (Apparently they have the same name--C++ mangles the names so they don't actually have the same name).  

Ordinarily the C++ function is exported with the mangeled name.  C however doesn't do any name mangeling.  So when the C program tries to import the function it looks for the unmangled name.  So C cannot find the function written in C++.  

To fix this problem, you declare the function as "extern "C" ".  This dissables name mangeling on the function.

************************

There is another form of decoration to be dealt with too, that is standard call decoration.  But lets not worry about that yet, or ever ifyou will be using COM.
0
 
nietodCommented:
Opps, that was meant to a different question.
0
 
Clinton112299Author Commented:
nietod's

Thanks for all your help, the only way I can get this @#$$% thing to work was by using a Batch file which than opened the Dos program.

Thanks agian for all your help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 14
  • 11
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now